程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 深入.NET和C#的小型汽車租賃系統的框架,

深入.NET和C#的小型汽車租賃系統的框架,

編輯:C#入門知識

深入.NET和C#的小型汽車租賃系統的框架,


前言:寫這個小型系統之前呢,我們應該要猜測可能要用到哪些知識點。

那麼對於這個小型系統:主要用到了如下的知識:

 封裝,集合(ArrayList和HashTable)和泛型和非泛型集合(泛型:List<>和非泛型:Dictioanry<>),

構造函數,方法重載,多態(繼承,抽象:抽象類和抽象方法)

效果圖:

 

第一步.

創建幾個類

01:工具類(vehicle)(父類,超類,基類),此類為抽象類

有方法重載,構造函數

該類主要負責提供車類的屬性

屬性:

  public string Color { get; set; }
        public double DailyRent { get; set; }
        public string LicenseNO { get; set; }
        public string Name { get; set; }
        public int RentDate { get; set; }
        public string RentUser { get; set; }
        public int YearsOfService { get; set; }

02:創建一個汽車類(Car)(子類)

構造函數,方法重載。

03:創建一個卡車 類(Truck)(子類)

構造函數,方法重載

屬性:

  public int Load

        {

            get { return load; }

            set { load = value; }

        }

04:創建一個工具(工廠)類(VehicleUtil)

第二步.

代碼實現思路:

01:工具,父類(vehicle):

代碼如下:

 public Vehicle()
        {
        }
        public Vehicle(string color, double dailyrent, string licenseno, string name, int yearsofservice)
        {
            Color = color;
            DailyRent = dailyrent;
            LicenseNO = licenseno;
            Name = name;
            YearsOfService = yearsofservice;
        }
        public abstract double CalcPrice();

02:汽車類(Car)

代碼如下:

public Car()
        {
      }
        public Car(string licenseno, string name, string color, int yearsofservice, double dailyrent)
        {
           Color = color;
            DailyRent = dailyrent;
            LicenseNO = licenseno;
            Name = name;
            YearsOfService = yearsofservice;
        }
        public override double CalcPrice()
        {
            double totalPrice = 0;
            totalPrice = RentDate * DailyRent;
            return totalPrice;
        }

03:卡車 類(Truck)

代碼如下:

  public Truck(string licenseno, string name, string color, int yearsofservice, double dailyrent, int load)
        {
           Color = color;
            DailyRent = dailyrent;
            LicenseNO = licenseno;
            Name = name;
            YearsOfService = yearsofservice;
            Load = load;
        }
        public override double CalcPrice()
        {
            double totalPrice = 0;
            totalPrice = RentDate * DailyRent;
           return totalPrice;
}

04:工具(工廠)類(VehicleUtil

代碼如下:

 public static Vehicle CreateVehicle(string licenseno, string name, string color, int yearsofservice, double dailyrent, int load, string type)
        {
            Vehicle vehicle = null;
            switch (type)
            {
                case "Car":
                    vehicle = new Car(licenseno, name, color, yearsofservice, dailyrent);
                    break;
                case "Truck":
                    vehicle = new Truck(licenseno, name, color, yearsofservice, dailyrent, load);
                    break;
                default:
                    break;
            }
            return vehicle;
        }

第三步.

出租事件:(btnRent_Click(object sender, EventArgs e))

關鍵代碼如下:

 if (lvlist.SelectedItems.Count == 0)
            {
                MessageBox.Show("請選擇一行");
                return;
            }
            if (txtUser.Text == "")
            {
                MessageBox.Show("請輸入客戶名字!");
                return;
            }
            string key = lvlist.SelectedItems[0].Text;
            Vehicle vehicle = vehicles[key];
            vehicles.Remove(key);
            rentvehicles.Add(key, vehicle);
            PrintVehicles(vehicles, lvlist);
            MessageBox.Show("出租成功!!");
//PrintVehicles綁定數據到listview上
        public void PrintVehicles(Dictionary<string, Vehicle> listprirnt, ListView lvinfo)
        {
            ListViewItem lv=null;
            lvinfo.Items.Clear();
            foreach (Vehicle item in listprirnt.Values)
            {        
                if (item is Car)
                {
                    lv = new ListViewItem(item.LicenseNO);
                    lv.SubItems.Add(item.Name);
                    lv.SubItems.Add(item.Color.ToString());
                    lv.SubItems.Add(item.YearsOfService.ToString());
                    lv.SubItems.Add(item.DailyRent.ToString());
                    lvinfo.Items.Add(lv);
                }
               else if(item is Truck)
                {
                    lv = new ListViewItem(item.LicenseNO);
                    lv.SubItems.Add(item.Name);
                    lv.SubItems.Add(item.Color.ToString());
                    lv.SubItems.Add(item.YearsOfService.ToString());
                    lv.SubItems.Add(item.DailyRent.ToString());
                    lv.SubItems.Add(((Truck)item).Load.ToString());
                    lvinfo.Items.Add(lv);
                }

  第四步

還車.在btnGiveMoney_Click事件中寫   

  if (txtDayNum.Text == string.Empty)
            {
                MessageBox.Show("請輸入天數!");
                return;
            }
            string addNum = lvlistGive.SelectedItems[0].Text;
            Vehicle vechile = rentvehicles[addNum];
            rentvehicles.Remove(addNum);
            vechile.RentDate = Convert.ToInt32(txtDayNum.Text);
            double money = 0;
            //調用計算價格方法
            money = vechile.CalcPrice();
            MessageBox.Show("你一共要福le" + money);
            vehicles.Add(addNum, vechile);
             //刷新
            PrintVehicles(rentvehicles, lvlistGive);
            MessageBox.Show("成功!!");

第五步.

添加車輛在 btnIncAR_Click事件中寫

if (txtWeight.Text == "" || txtUseTime.Text == "" || txtType.Text == "" || txtID.Text == "" || txtDayRentMoney.Text == "" || cboColor.Text == "")

            {
                MessageBox.Show("請您填寫完整!");
            }
            else
            {
                string licon = txtID.Text;
                string color = cboColor.Text;
                double mon = Convert.ToDouble(txtDayRentMoney.Text);
                int tine = Convert.ToInt32(txtUseTime.Text);
                string ty = txtType.Text;
               if (radJiaoCar.Checked)
               {
                    Car ca = new Car(licon, ty, color, tine, mon);
                    vehicles.Add(licon, ca);
                }
               if (radKaCar.Checked)
                {
                   int load = Convert.ToInt32(txtWeight.Text);
                    Truck tr = new Truck(licon, ty, color, tine, mon, load);
                    vehicles.Add(licon, tr);
                }
                MessageBox.Show("添加成功!");
            }

注:此文沒有代碼的注釋,是因為想在看代碼的任何情況下,

也能看懂代碼的含義。

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved