using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RentCar
{
    public partial class FrmRentCar : Form
    {
        public FrmRentCar()
        {
            InitializeComponent();
            Init();
        }
        //保存可租用车的集合
        Dictionary<string, Vehicle> vehicles = new Dictionary<string,Vehicle>();
        //保存租出车的集合
        Dictionary<string, Vehicle> rentvehicles = new Dictionary<string,Vehicle>();
        //初始化车辆
        public void Init()
        {
            //可租车辆
            Car ao = new Car();
            ao.Name = "奥迪A6";
            ao.LicenseNo = "京R5655";
            ao.Color = "黑色";
            ao.YearOfService = 3;
            ao.DailyRent = 240;
            Car ao2 = new Car();
            ao2.Name = "奥迪A8";
            ao2.LicenseNo = "赣A2333";
            ao2.Color = "黄色";
            ao2.YearOfService = 3;
            ao2.DailyRent = 250;
            vehicles.Add(ao.LicenseNo, ao);
            vehicles.Add(ao2.LicenseNo, ao2);
            //要还车辆
            Car fen = new Car();
            fen.Name = "火箭狂雷";
            fen.LicenseNo = "沪A2333";
            fen.Color = "白色";
            fen.YearOfService = 3;
            fen.DailyRent = 250;
            fen.RentUser = "++";
            fen.RentDate = 6;
            Car fen2 = new Car();
            fen2.Name = "兰博基尼";
            fen2.LicenseNo = "粤A2333";
            fen2.Color = "金色";
            fen2.YearOfService = 1;
            fen2.DailyRent = 5000;
            fen2.RentUser = "sha强";
            fen2.RentDate = 2;
            rentvehicles.Add(fen.LicenseNo, fen);
            rentvehicles.Add(fen2.LicenseNo, fen2);
        }
        //绑定lvRCar数据源
        public void BindLv()
        {
            lvRCar.Items.Clear();
            foreach (var item in vehicles.Values)
            {
                ListViewItem lv = new ListViewItem(item.LicenseNo);
                lv.SubItems.Add(item.Name);
                lv.SubItems.Add(item.Color);
                lv.SubItems.Add(item.YearOfService.ToString());
                lv.SubItems.Add(item.DailyRent.ToString());
                lvRCar.Items.Add(lv);
            }
            
        }
        //绑定lvRRar数据源
        public void BindLvG()
        {
            lvGCar.Items.Clear();
            foreach (var item in rentvehicles.Values)
            {
                ListViewItem lv = new ListViewItem(item.LicenseNo);
                lv.SubItems.Add(item.Name);
                lv.SubItems.Add(item.Color);
                lv.SubItems.Add(item.YearOfService.ToString());
                lv.SubItems.Add(item.DailyRent.ToString());
                lvGCar.Items.Add(lv);
            }

        }
        //单击刷新可租车辆
        private void btnRefurbish_Click(object sender, EventArgs e)
        {
            BindLv();
        }
        //单击刷新已租出车辆
        private void btnRefurbishs_Click(object sender, EventArgs e)
        {
            BindLvG();
        }

        private void btnRCar_Click(object sender, EventArgs e)
        {
            if (lvRCar.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择要租的车!");
            }
            else if (string.IsNullOrEmpty(txtRName.Text.Trim()))
            {
                MessageBox.Show("请输入租赁人姓名!");
            }
            else
            {
                string key = lvRCar.SelectedItems[0].Text;
                vehicles[key].RentUser = this.txtRName.Text;
                rentvehicles.Add(vehicles[key].LicenseNo,vehicles[key]);
                if (vehicles.ContainsKey(key))
                {
                    vehicles.Remove(key);
                }
                MessageBox.Show("已租出!");
                BindLv();
                BindLvG();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnChoice_Click(object sender, EventArgs e)
        {
            if (lvRCar.SelectedItems.Count < 0)
            {
                MessageBox.Show("请选择要还的车!");
            }
            else if (string.IsNullOrEmpty(txtRDay.Text.Trim()))
            {
                MessageBox.Show("请输入天数!");
            }
            else
            {
                string key = lvGCar.SelectedItems[0].Text;
                rentvehicles[key].RentDate = int.Parse(this.txtRDay.Text);
                //调用抽象方法
                double totprice = rentvehicles[key].CalcPrice();
                string msg = string.Format("您的总价是{0}", totprice.ToString());
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //加入可租车辆集合
                vehicles.Add(rentvehicles[key].LicenseNo, rentvehicles[key]);
                //还车后从已租车辆集合中移除
                if (rentvehicles.ContainsKey(key))
                {
                    rentvehicles.Remove(key);
                }
                BindLv();
                BindLvG();
            }
        }

        //选中卡车类型后,卡车载重可用
        private void rbtnCamion_CheckedChanged(object sender, EventArgs e)
        {
            label11.Enabled = true;
            txtZai.Enabled = true;
        }
        //选中轿车类型后,卡车载重不可用
        private void rbtnSaloonCar_CheckedChanged(object sender, EventArgs e)
        {
            label11.Enabled = false;
            txtZai.Enabled = false;
        }
        //添加车
        public void Adds()
        {
            bool sa = true;
            if (string.IsNullOrEmpty(txtCId.Text) || string.IsNullOrEmpty(this.txtColor.Text) || string.IsNullOrEmpty(this.txtCType.Text) || string.IsNullOrEmpty(this.txtDailyRent.Text) || string.IsNullOrEmpty(this.txtRentDay.Text))
            {
                MessageBox.Show("请完善车辆信息");
                sa = false;
            }
            else
            {
                foreach (string item in vehicles.Keys)
                {
                    if (txtCId.Text.Trim().Equals(item))
                    {
                        MessageBox.Show("已有该车,入库失败!!");
                        sa = false;
                    }
                }
                foreach (string item in rentvehicles.Keys)
                {
                    if (txtCId.Text.Trim().Equals(item))
                    {
                        MessageBox.Show("已有该车,入库失败!!");
                        sa = false;
                    }
                }
                if (sa)
                {
                    vehicles.Add(txtCId.Text,new Car(txtCId.Text,txtRName.Text,txtColor.Text,int.Parse(txtRentDay.Text),double.Parse(txtDailyRent.Text)));
                    MessageBox.Show("入库成功!!");
                    BindLv();
                    BindLvG();
                }
            }
        }

        private void btnRuKu_Click(object sender, EventArgs e)
        {
            Adds();
        }
    }
}
主窗体
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace RentCar
 8 {
 9     public class Car:Vehicle
10     {
11         public Car()
12         {
13         }
14         public Car(string licenseNo, string name, string color, int yearOfService, double dailyRent)
15             : base(licenseNo, name, color, yearOfService, dailyRent)
16         {
17 
18         }
19 
20         public override double CalcPrice()
21         {
22             double pirce = 0;
23             pirce = this.DailyRent * this.RentDate;
24             return pirce;
25         }
26     }
27 }
Car类
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace RentCar
 8 {
 9     public class Truck:Vehicle
10     {
11         //载重
12         public int Load { get; set; }
13         public Truck():base()
14         {
15 
16         }
17         public Truck(string licenseNo, string name, string color, int yearOfService, double dailyRent, int load)
18             : base(licenseNo, name, color, yearOfService, dailyRent)
19         {
20             this.Load = load;
21         }
22 
23         public override double CalcPrice()
24         {
25             double pirce = 0;
26             pirce = this.DailyRent * this.RentDate;
27             return pirce;
28         }
29     }
30 }
Truck类
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace RentCar
 8 {
 9     public abstract class Vehicle
10     {
11         //颜色
12         public string Color { get; set; }
13         //日租金
14         public double DailyRent { get; set; }
15         //车牌号
16         public string LicenseNo { get; set; }
17         //名字
18         public string Name { get; set; }
19         //租用时间
20         public int RentDate { get; set; }
21         //租用者
22         public string RentUser { get; set; }
23         //最少租用时长
24         public int YearOfService { get; set; }
25 
26         public Vehicle(string color, double dailyRent, string licenseNo, string name, int rentDate, string rentUser, int yearsOfService)
27         {
28             this.Color = color;
29             this.DailyRent = dailyRent;
30             this.LicenseNo = licenseNo;
31             this.Name = name;
32             this.RentDate = rentDate;
33             this.RentUser = rentUser;
34             this.YearOfService = yearsOfService;
35         }
36         public Vehicle(string licenseNo, string name, string color, int yearOfService, double dailyRent)
37         {
38             this.Color = color;
39             this.DailyRent = dailyRent;
40             this.LicenseNo = licenseNo;
41             this.Name = name;
42             this.YearOfService = yearOfService;
43         }
44         public Vehicle()
45         {
46         }
47         //抽象方法,计算总金额
48         public abstract double CalcPrice();
49     }
50 }
Vehicle类(父类)

 

posted on 2017-04-13 22:51  Crazy丶忠祥  阅读(196)  评论(0编辑  收藏  举报