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

汽車租賃系統,汽車租賃管理系統

編輯:C#入門知識

汽車租賃系統,汽車租賃管理系統


  又到項目階段了,總是感覺有點興奮,每次著手到一個項目時,總是很激動,想馬上把這個項目完成,終歸夢想是美好的,現實是殘酷的,

還是老套路,先來看看這個項目主要完成哪些功能。

一.主要功能:

1.實現租車功能。

選中一輛租車信息,輸入租車者姓名,即可租車成功!在未租列表中點擊刷新該車輛顯示,在租車列表中會出現對應的租車信息將會消失。

2.實現還車功能。

選中一輛還車信息,輸入使用天數,進行結算。點擊租車列表中的刷新按鈕該車輛信息會顯示,在未租列表中該車輛會消失。

3.實現新車入庫功能。

選擇入庫的汽車類型,填寫對應車輛的信息,進行汽車入庫,在未租列表中點擊刷新按鈕就會顯示剛才添加的車輛信息。

2.思路:

  根據日常生活中的租車案例,咋們都知道租車是分種類的,在這裡呢,轎車和卡車屬於一種交通工具的,所以要在我們的程序中就要抽象出一個交通工具類(Vechile)作為父類,

抽象出對應的子類就是轎車類(Car)和卡車類(Truck),到了這裡,還別忘還有一個工廠類(VechileFactory),是用來示例化子類的對象,在這裡需要注意的是,簡單工廠類裡的方法是靜態的,

返回值是父類類型,也就是Vechile類,需要傳入參數,在方法體中,通過switch選擇結構進行選擇,到底實例化哪個子類對象。

首先有這麼幾個類:

01.Vehicle類:

是一個交通工具類(抽象類,也是父類),在他的底層統領著兩個子類,分別是Car類,和Truck類,在這個類中有一個計算價格的方法。

 

 //父類:交通工具類
   public abstract class Vehicle
    {
        public string Color { get; set; }//汽車顏色
        public int 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; }//使用時間
       //無參構造
        public Vehicle() { }
      //帶參構造用於給屬性賦值
        public Vehicle(string color, int dailyrent, string licenseno, string name, int YearsOfService) 
        {
            this.Color = color;
            this.DailyRent = dailyrent;
            this.LicenseNo = licenseno;
            this.Name = name;
            this.YearsOfService = YearsOfService;
        }
       //計算價格的方法
        public abstract double CalculatePrice();
    }

 

02.Car類(轎車):Vechile的子類。

//小汽車類繼承自交通工具類
    public class Car:Vehicle
    {
        //無參構造
        public Car() { }
        //帶參構造
        public Car(string color, int dailyrent, string licenseno, string name, int YearsOfService) :
            base( color,dailyrent,licenseno,name,YearsOfService)
        {
          
        }
        //重寫計算價格的方法
        public override double CalculatePrice()
        {
            //定義變量保存價格
            double SumPrice = this.DailyRent * RentDate;
            return SumPrice;
        }

    }

03.Truck類(卡車):Vechile的子類

   

  //卡車類繼承自交通工具類
    public class Truck:Vehicle
    {
        public int weight { get; set; }//卡車的重量
        public Truck() { }
        public Truck(string color, int dailyrent, string licenseno, string name, int YearsOfService,int weight):
            base( color,dailyrent,licenseno,name,YearsOfService)
        {
            this.weight = weight;
        }
       //計算價格的方法
        public override double CalculatePrice()
        {
           //定義變量保存價格
            double SumPrice = this.DailyRent * RentDate;
            return SumPrice;
        }

04.工廠類VechileFactory

 //工廠
    public class VehicleFactory
    {
        //第一一個靜態方法,返回值類型是父類類型,傳入參數
        public static Vehicle CreateVehicle(string color, int dailyrent, string licenseno, string name, int YearsOfService, int weight,string type) 
        {
            //給對象賦null值
            Vehicle vehicle = null;
            switch (type)
            {
                case"轎車":
                    vehicle = new Car( color,  dailyrent,  licenseno,  name, YearsOfService);
                    break;
                case"卡車":
                    vehicle = new Truck(color, dailyrent, licenseno, name, YearsOfService, weight);
                    break;
           
            }
            return vehicle;
        
        }
    }

FrmMain窗體:

 //定義一個字典集合保存租車(還未租出的車輛)信息k:車牌號,V父類對象
        public Dictionary<string, Vehicle> dic = new Dictionary<string, Vehicle>();
        //第一一個集合保存已租車輛的信息
        public Dictionary<string, Vehicle> outdic = new Dictionary<string, Vehicle>();

        //點擊退出觸發的事件
        private void btnrefurbish_Click(object sender, EventArgs e)
        {
            Application.Exit();    
        }

     
        //load事件
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //01.初始化泛型集合(添加數據到集合中去)
            Car car = new Car("紅色", 500, "京P-34566", "奔馳", 3);
            Car car1 = new Car("白色", 1000, "京Q-XH456", "保時捷", 2);
            Truck truck = new Truck("藍色", 200, "貴-B300掛", "變形金剛",5,100);
            dic.Add(car.LicenseNo, car);
            dic.Add(car1.LicenseNo, car1);
            dic.Add(truck.LicenseNo, truck);
            //02寫一個方法顯示到窗體的listview空間中
            dictolvlist(dic,lvlist);
            //03,給新車入庫的顏色下拉框綁定值
            comcolor.Items.Add("紅色");
            comcolor.Items.Add("藍色");
            comcolor.Items.Add("白色");
            comcolor.Items.Add("黑色");
            comcolor.Items.Add("灰色");

        }
        public void dictolvlist(Dictionary<string,Vehicle> dic,ListView lv)
        {
            //創建一個listviewitem對象,賦值為null
            ListViewItem item = null;
            //顯示數據之前,清除數據
            lv.Items.Clear();
            foreach (Vehicle itemdic in dic.Values)
            {
                if (itemdic is Car)
                {
                    //實例化對象
                    item = new ListViewItem();
                    item.Text = itemdic.LicenseNo;
                    item.SubItems.Add(itemdic.Name);
                    item.SubItems.Add(itemdic.Color);
                    item.SubItems.Add(itemdic.YearsOfService.ToString());
                    item.SubItems.Add(itemdic.DailyRent.ToString());
                }
                else 
                {

                    //實例化對象
                    item = new ListViewItem();
                    item.Text = itemdic.LicenseNo;
                    item.SubItems.Add(itemdic.Name);
                    item.SubItems.Add(itemdic.Color);
                    item.SubItems.Add(itemdic.YearsOfService.ToString());
                    item.SubItems.Add(itemdic.DailyRent.ToString());
                    //as等同於類型轉換
                    item.SubItems.Add((itemdic as Truck).weight.ToString());
                }
                //讓游離得Listviewitem對象和lvlsiit空間產生關系
                lv.Items.Add(item);
            }
        
        
        }
        //點擊租車觸發的事件
        private void btnrentcar_Click(object sender, EventArgs e)
        {
            //01確保選中了一個車倆
            if(lvlist.SelectedItems.Count==0)
            {
                MessageBox.Show("請選中你要租的車輛!");
                return;
            }
            //02確保填寫了租用值名字
            if (txtrent.Text=="")
            {
                MessageBox.Show("請填寫姓名!");
                return;
            }
            //03驗證信息完成!開始租車過程
            //獲取lvlist第一項的值車號
            string carnum = lvlist.SelectedItems[0].Text;
            //通過dic集合的key獲取整個汽車對象
             Vehicle vehicle= dic[carnum];
            //在集合中刪除該項記錄
             dic.Remove(carnum);
            //重新綁定數據,調用方法dictolvlist即可
             dictolvlist(dic,lvlist);
            //將已租車輛放入到已租集合中
             outdic.Add(carnum, vehicle);
             MessageBox.Show("租車成功!");
            //清空文本框中的值
             txtrent.Text = "";
            

        }

        //在還車界面點擊刷新觸發的事件
        private void btnrefurbish11_Click(object sender, EventArgs e)
        {
            dictolvlist(outdic,lvlisttwo);
        }

        //點擊選擇結算觸發的事件
        private void btnselectsettleaccounts_Click(object sender, EventArgs e)
        {
            if(lvlisttwo.SelectedItems.Count==0)
            {
                MessageBox.Show("請選擇要退還的車輛!");
                return;
            }
             //確保用戶填寫了租用天數
            if (txtrentday.Text=="")
            {
                MessageBox.Show("請填寫租用天數!");
                return;
            }
            //執行還車步驟
           //獲取listviewtow中的選中項的車牌號
            string carnum = lvlisttwo.SelectedItems[0].Text;
            //從已租集合中通過key值找到汽車完整對象
            Vehicle vehicle=   outdic[carnum];
            //給還車日期屬性賦值
            vehicle.RentDate = Convert.ToInt32(txtrentday.Text);
            //計算價格
            double summoney=vehicle.CalculatePrice();
            //添加到未租車輛中
            dic.Add(carnum, vehicle);
            //刪除已租車輛的該車輛信息
            outdic.Remove(carnum);
            //重新綁定數據
            dictolvlist(outdic, lvlisttwo);
            MessageBox.Show("需支付金額"+summoney.ToString());
            //清空文本框中的值
            txtrentday.Text = "";

        }
        //點擊刷新觸發的事件
        private void btnrefurbish_Click_1(object sender, EventArgs e)
        {
            dictolvlist(dic, lvlist);
        }
        //點擊入庫觸發的事件
        private void btnok_Click(object sender, EventArgs e)
        {
                try
                {


                    //01獲取對應文本框中的值,
                    string carnum = txt.Text;//車號
                    string cartype = txtcartype.Text;//車型
                    string color = comcolor.Text;//顏色
                    int endtime = Convert.ToInt32(txttime.Text);//使用時間
                    int money = Convert.ToInt32(txtdaymoney.Text);//每日租金
                    int weight = 0;
                    if (rbcar.Checked == true)
                    {
                        //01寫一個方法判斷用戶信息填寫是否填寫完整
                        if (isnulltwo() == true)
                        {
                            //執行到這證明信息已經驗證完成
                            //調用工廠類,傳入對應類型汽車,獲取對應子類對象
                            //獲取對應子類對象
                            Vehicle vehicle = VehicleFactory.CreateVehicle(color, money, carnum, cartype, endtime, weight, rbcar.Text);
                            //添加到集合中去
                            dic.Add(carnum, vehicle);
                            MessageBox.Show("添加成功!");
                        }
                    }
                    else if (rbtruck.Checked == true)
                    {
                        if (isnull() == true)
                        {
                            weight = Convert.ToInt32(txttruckweight.Text);//卡車載重
                            //獲取對應子類對象
                            Vehicle vehicle = VehicleFactory.CreateVehicle(color, money, carnum, cartype, endtime, weight, rbtruck.Text);
                            //添加到集合中去
                            dic.Add(carnum, vehicle);
                            MessageBox.Show("添加成功!");
                        }
                    }
                    //調用清空文本框中的值的方法
                    infoclear();
                   
                }
                catch (Exception)
                {
                    MessageBox.Show("輸入格式有誤!");
                }
           

        }
        //判斷新車入庫中填寫信息是否完整
        public bool isnull() 
        {

            if (rbcar.Checked==false&&rbtruck.Checked==false)
             {
            MessageBox.Show("請選擇添加的車輛類型!");
            return false;
             }
            else if (txt.Text == "" || txtcartype.Text == "" || comcolor.Text == "" || txttime.Text == "" || txtdaymoney.Text == "" || txttruckweight.Text == "")
            {

                MessageBox.Show("請填寫完整的信息!");
                return false;

            }
            else 
            {
                return true;
            }
        
        }
        public bool isnulltwo()
        {

            if (rbcar.Checked == false && rbtruck.Checked == false)
            {
                MessageBox.Show("請選擇添加的車輛類型!");
                return false;
            }
            else if (txt.Text == "" || txtcartype.Text == "" || comcolor.Text == "" || txttime.Text == "" || txtdaymoney.Text == "")
            {

                MessageBox.Show("請填寫完整的信息!");
                return false;

            }
            else
            {
                return true;
            }

        }
        //單機卡車觸發的事件
        private void rbtruck_Click(object sender, EventArgs e)
        {
            if (rbtruck.Checked == true)
            {
                txttruckweight.Enabled = true;
                lbltruckweight.ForeColor = Color.Blue;
            }
 
        }
        //單機轎車觸發的事件
        private void rbcar_Click(object sender, EventArgs e)
        {
            if (rbcar.Checked == true)
            {
                txttruckweight.Enabled = false;
                lbltruckweight.ForeColor = Color.Red;
               
            }
        }
        //清空文本框中的值的方法
        public void infoclear() 
        {
        txt.Text = "";
        txtcartype.Text = "";
         comcolor.Text = "" ;
         txttime.Text = "";
           txtdaymoney.Text = "";
           txttruckweight.Text = "";
        }

   如有感覺和我意見不同的可以盡情發表意見,謝謝!

 

                 

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