Lập trình C# - Quản lý xe gắn máy

Quản lý xe gắn máy

Công ty Yamaha Việt Nam là một công ty chuyên sản xuất xe máy với các model khác nhau như Sirius, Exciter và cung cấp cho thị trường Việt Nam. Công ty có nhu cầu làm một phần mềm quản lý hệ thống phân phối các loại xe trên. Bạn là một thành viên trong nhóm phát triển phần mềm này và bạn được giao phải thực hiện các công việc sau:

Bước 1: Kích chuột phải vào Solution chọn Add -> New Project ->Nhập tên (InterfaceMotorBike)-> Chọn OK

Bước 2Tạo interface có tên IMotor chứa các phương thức

using System;
using System.Collections.Generic;
using System.Text;

namespace InterfaceMotorBike
{
    interface IMotor
    {
        //phương thức nhập thông tin
        void Input();
        //phương thức hiển thị thông tin
        void Display();
    }
}

Bước 3Tạo lớp MyMotor cài đặt IMotor và có các thuộc tính sau

using System;
using System.Collections.Generic;
using System.Text;

namespace InterfaceMotorBike
{
    class MyMotor:IMotor
    {
        
       
        public string Code { get; set; } //Tối thiểu 4 ký tự và không trùng

        public string Name { get; set; }  //Tối thiểu 6 ký tự
        public int Capacity { get; set; } //Capacity >= 50
        public string Type { get; set; }  //Xe số hoặc xe ga
        public MyMotor()
        {

        }

        public MyMotor(string code, string name, int capacity, string type)
        {
            this.Code = code;
            this.Name = name;
            this.Capacity = capacity;
            this.Type = type;
        }
        
        public void Input()
        {
            
            do
            {
                Console.WriteLine("Nhập code tối thiểu 4 ký tự");
                this.Code = Console.ReadLine();
            } while (this.Code.Trim().Length < 4);

            do
            {
                Console.WriteLine("Nhập Name tối thiểu 6 ký tự");
                this.Name = Console.ReadLine();
            } while (this.Name.Trim().Length < 6);

            do
            {
                Console.WriteLine("Nhập Capacity>=50 ");
                this.Capacity = Convert.ToInt32(Console.ReadLine());
            } while (this.Capacity < 50);

            do
            {
                Console.WriteLine("Nhập loại xe( tay ga hoặc xe so)");
                this.Type = Console.ReadLine();
            } while (!this.Type.Equals("tay ga") && !this.Type.Equals("xe so"));
        }

        public void Display()
        {
            Console.WriteLine("-------------------------");
            Console.WriteLine("Code :{0}", Code);
            Console.WriteLine("Name :{0}", Name);
            Console.WriteLine("Capacity :{0}", Capacity);
            Console.WriteLine("Type :{0}", Type);
            
        }
    }
}

Bước 4Tạo lớp Exciterkế thừa từ lớp MyMotor. Trong lớp Exciterbổ sung thêm thuộc tính warranty (thời gian bảo hành) có kiểu int và giá trị >= 1 (đơn vị năm)

using System;
using System.Collections.Generic;
using System.Text;

namespace InterfaceMotorBike
{
    class Exciter : MyMotor
    {
        public int Warranty { get; set; }//warranty (thời gian bảo hành) có kiểu int và giá trị >= 1

        public Exciter()
        {
            Warranty = 0;
        }

        public Exciter(int warranty, string code, string name, int capacity, string type) :base(code, name, capacity, type)
        {
            this.Warranty = warranty;
        }
        public void Input()
        {
            base.Input();
            do
            {
                Console.WriteLine("Nhập năm bảo hành tối thiểu 1 năm");
                this.Warranty = int.Parse( Console.ReadLine());
            } while (this.Code.Trim().Length < 1);
        }
        public void Display()
        {
            base.Display();
            Console.WriteLine("Warranty :{0}", Warranty);
            Console.WriteLine("-------------------------");
        }
    }
}

Bước 5 Tạo lớp Sirius kế thừa từ lớp MyMotor. Trong lớp Sirius bổ sung thêm thuộc tính color có kiểu string (màu xe) và giá trị của nó hoặc là xanh, vàng, trắng.

using System;
using System.Collections.Generic;
using System.Text;

namespace InterfaceMotorBike
{
    class Sirius:MyMotor
    {
        public string Color { get; set; }//(màu xe) và giá trị của nó hoặc là xanh, vàng, trắng.
        public Sirius()
        {

        }
        public Sirius(string color, string name, string code, int capacity, string type):base(code, name, capacity, type)
        {
            this.Color = color; ;
        }
        public void Input()
        {
            base.Input();
            do
            {
                Console.WriteLine("Nhập màu xe (xanh, vang, trang)");
                this.Color = Console.ReadLine();
            } while (!this.Color.Trim().Equals("xanh")&&!this.Color.Equals("vang")&&!this.Color.Equals("trang"));
        }
        public void Display()
        {
            base.Display();
            Console.WriteLine("color :{0}", Color);
            Console.WriteLine("-------------------------");
        }
    }
}

Bước 6Trong Program code test như sau:

using System;
using System.Collections.Generic;
using System.Text;

namespace InterfaceMotorBike
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            string option;
            List<Exciter> exLst = new List<Exciter>();
            List<Sirius> silst = new List<Sirius>();
            do
            {
                ShowMenu();
                option = Console.ReadLine();
                switch (option)
                {
                    case "iei":
                        Exciter _Exciter = new Exciter();
                        _Exciter.Input();
                        exLst.Add(_Exciter);
                        break;
                    case "isi":
                        Sirius _Sirius = new Sirius();
                        _Sirius.Input();
                        silst.Add(_Sirius);
                        break;
                    case "sei":
                        foreach(Exciter ex in exLst)
                        {
                            ex.Display();
                            Console.WriteLine();
                        }
                        break;
                    case "ssi":
                        foreach (Sirius si in silst)
                        {
                            si.Display();
                            Console.WriteLine();
                        }
                        break;
                    case "se":
                        string code;
                        Console.WriteLine("Nhập Code");
                        code = Console.ReadLine();
                        foreach(Exciter ex in exLst)
                        {
                            if (ex.Equals(code))
                            {
                                ex.Display();
                                break;
                            }
                        }
                        break;
                    case "ss":                        
                        Console.WriteLine("Nhập Code");
                        code = Console.ReadLine();
                        foreach (Sirius si in silst)
                        {
                            if (si.Equals(code))
                            {
                                si.Display();
                                break;
                            }
                        }
                        break;

                    default:
                        Console.WriteLine("Nhập sai chức năng");
                        break;
                }

            } while (!option.Equals("ea"));
        }
        static void ShowMenu()
        {
            Console.WriteLine("\nCHƯƠNG TRÌNH QUẢN LÝ XE GẮN MÁY YAMAHA");
            Console.WriteLine("*********************************MENU************************************");
            Console.WriteLine("**       1.  Nhập thông tin xe Exciter (nhập: iei)                     **");
            Console.WriteLine("**       2.  Nhập thông tin xe Sirius (nhập: isi)                      **");
            Console.WriteLine("**       3.  Hiển thị thông tin xe Exciter (nhập: sei)                 **");
            Console.WriteLine("**       4.  Hiển thị thông tin xe Sirius (nhập: ssi)                  **");
            Console.WriteLine("**       5.  Tìm kiếm xe Exciter (nhập: se)                            **");
            Console.WriteLine("**       6.  Tìm kiếm xe Sirius (nhập: ss)                             **");
            Console.WriteLine("**       7.  Thoát chương trình (nhập: ea)                             **");
            Console.WriteLine("**       Lựa chọn của bạn:                                             **");
            Console.WriteLine("*************************************************************************");
        }
    }
}

Xem ví dụ

 

Bước 7: Chạy chương trình

 

Nhập xe Exciter

Nhập xe Sirius

Hiển thị xe Exciter

Hiển thị xe Sirius

Tìm kiếm xe Exciter

Tìm kiếm xe Sirius