C#汽车租赁系统 完整版
Truck.cs类
//卡车类 public class Truck : Vehicle1 { //重载 public int Load { get; set; } //构造函数 public Truck(string color, double dailyrent, string licenseNO, string name, int rentDate, string rentUser, int yearsOfService, int load) :base(color, dailyrent, licenseNO, name, rentDate, rentUser, yearsOfService) { this.Load = load; } //计算价格 public override double CalcPrice() { double Price = 0; Price = this.DailyRent * this.RentDate; return Price; } } }
Vehicle1.cs类
//交通工具类 public abstract class Vehicle1 { //颜色 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; } //构造函数 public Vehicle1(string color, double dailyrent, string licenseNO, string name, int rentDate, string rentUser, int yearsOfService) { this.Color = color; this.DailyRent = dailyrent; this.LicenseNO = licenseNO; this.Name = name; this.RentDate = rentDate; this.RentUser = rentUser; this.YearsOfService = yearsOfService; } //方法重写 public Vehicle1() { } //执行 public abstract double CalcPrice(); } }
主窗体代码:
//可以出租车的集合 Dictionary<string, Vehicle1> dy = new Dictionary<string, Vehicle1>(); //已出租车的集合 Dictionary<string, Vehicle1> doy = new Dictionary<string, Vehicle1>(); private void Form1_Load(object sender, EventArgs e) { //调用方法 Init(); //绑定下拉框 cmba.Text = "请选择"; cmba.Text = "白色"; //卡车载重的文本框不可用 textBox6.Enabled = false; } public void Init() { //初始化租车 Vehicle1 c = new Car("白色", 600, "京A666666", "兰博基尼", 3, "xs", 20); //添加车 dy.Add(c.LicenseNO, c); Vehicle1 car = new Car("红色", 500, "鲁A999999", "法拉利", 3, "zs", 10); dy.Add(car.LicenseNO, car); Vehicle1 t = new Truck("黑色", 300, "鲁A333333", "本田思域", 3, "hs", 30, 2000); doy.Add(t.LicenseNO, t); Vehicle1 tk = new Truck("金色", 300, "鲁A888888", "东风雪铁龙", 3, "ps", 10, 1000); doy.Add(tk.LicenseNO, tk); } private void button3_Click(object sender, EventArgs e) { //退出 this.Close(); } // listView1绑定数据(租车) public void show() { //清空租车的ListView1 listzc.Items.Clear(); //循环遍历(租车的双列集合) foreach (var item in dy) { // ListViewItem cc = new ListViewItem(item.Key); cc.SubItems.Add(item.Value.Name); cc.SubItems.Add(item.Value.Color); // cc.SubItems.Add(item.Value.RentDate.ToString()); cc.SubItems.Add(item.Value.DailyRent.ToString()); if (item.Value is Truck) { // cc.SubItems.Add(((item.Value) as Truck).Load.ToString()); } else { cc.SubItems.Add(""); } listzc.Items.Add(cc); } }
// listView2绑定数据(还车) public void showw() { listhc.Items.Clear(); foreach (var item in doy) { //创建对象 并取K键,值 ListViewItem ccc = new ListViewItem(item.Key); // ccc.SubItems.Add(item.Value.Name); ccc.SubItems.Add(item.Value.Color); ccc.SubItems.Add(item.Value.RentDate.ToString()); ccc.SubItems.Add(item.Value.DailyRent.ToString()); if (item.Value is Truck) { ccc.SubItems.Add(((item.Value) as Truck).Load.ToString()); } else { ccc.SubItems.Add(""); } listhc.Items.Add(ccc); } } //刷新listView (zc) private void button1_Click(object sender, EventArgs e) { showw(); } //刷新listView(hc) private void button5_Click(object sender, EventArgs e) { show(); } // 租车 private void button4_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox2.Text.Trim())) { MessageBox.Show("请填写租车姓名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { if (listhc.SelectedItems.Count < 0) { MessageBox.Show("请选择租车", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { string key = listzc.SelectedItems[0].Text; doy.Add(dy[key].LicenseNO, dy[key]); if (dy.ContainsKey(key)) { dy.Remove(key); show(); } } } }
// 结算 private void button2_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox1.Text.Trim())) { MessageBox.Show("请输入租车天数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { if (listhc.SelectedItems.Count < 0) { MessageBox.Show("请选择还车", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { string key = listhc.SelectedItems[0].Text; doy[key].RentDate = int.Parse(this.textBox1.Text); double a = doy[key].DailyRent; double totalPrice = doy[key].CalcPrice(); string msg = string.Format("您的总价是:" + totalPrice.ToString()); MessageBox.Show(msg, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); dy.Add(doy[key].LicenseNO, doy[key]); if (doy.ContainsKey(key)) { doy.Remove(key); this.showw(); } } } } //入库 private void button6_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtchep.Text.Trim()) || string.IsNullOrEmpty(txtchex.Text.Trim()) || string.IsNullOrEmpty(cmba.Text.Trim()) || string.IsNullOrEmpty(txtje.Text.Trim()) || string.IsNullOrEmpty(txtsj.Text.Trim())) { MessageBox.Show("请完善新车入库信息","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } else { Vehicle1 vehicle = null; if (radioButton1.Checked == true) { vehicle = new Car(cmba.Text, int.Parse(txtje.Text), txtchep.Text, txtchex.Text, 0, "s", int.Parse(txtsj.Text)); } else { vehicle = new Truck(cmba.Text, int.Parse(txtje.Text), txtchep.Text, txtchex.Text, int.Parse(txtsj.Text), txtsj.Text, 2, int.Parse(textBox6.Text)); } try { dy.Add(vehicle.LicenseNO, vehicle); MessageBox.Show("添加成功"); } catch (Exception) { MessageBox.Show("车牌号重复"); } } } // 轿车 private void radioButton1_CheckedChanged(object sender, EventArgs e) { // radioButton1.Enabled = false; } //卡车 private void radioButton2_CheckedChanged(object sender, EventArgs e) { radioButton2.Enabled = true; }