汽车租凭系统
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CarSys.Entity 8 { 9 /// <summary> 10 /// 汽车类(父类) 11 /// </summary> 12 public abstract class Vehicle 13 { 14 /// <summary> 15 /// 颜色 16 /// </summary> 17 public string Color { get; set; } 18 19 /// <summary> 20 /// 日租金额 21 /// </summary> 22 public double DailyRent { get; set; } 23 24 /// <summary> 25 /// 车牌号 26 /// </summary> 27 public string LicenseNo { get; set; } 28 29 /// <summary> 30 /// 租车人姓名 31 /// </summary> 32 public string Name { get; set; } 33 34 /// <summary> 35 /// 租借日期 36 /// </summary> 37 public int RentDate { get; set; } 38 39 /// <summary> 40 /// 租借人 41 /// </summary> 42 public string RentUser { get; set; } 43 44 /// <summary> 45 /// 服务年限 46 /// </summary> 47 public string YearOfService { get; set; } 48 49 /// <summary> 50 /// 构造方法 51 /// </summary> 52 public Vehicle(string color, double dailyRent, string licenseNo, string name, int rentDate, string rentUser, string yearOfService) 53 { 54 this.Color = color; 55 this.DailyRent = dailyRent; 56 this.LicenseNo = licenseNo; 57 this.Name = name; 58 this.RentDate = rentDate; 59 this.RentUser = rentUser; 60 this.YearOfService = yearOfService; 61 } 62 63 public Vehicle() { } 64 65 /// <summary> 66 /// 计算租金(抽象方法) 67 /// </summary> 68 /// <returns></returns> 69 public abstract double TotalPrice(); 70 } 71 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CarSys.Entity 8 { 9 /// <summary> 10 /// 小汽车类(子类) 11 /// </summary> 12 public class Car:Vehicle 13 { 14 15 /// <summary> 16 /// 构造函数 17 /// </summary> 18 /// <param name="color">车颜色</param> 19 /// <param name="dailyRent">日租金</param> 20 /// <param name="licenseNo">车牌号</param> 21 /// <param name="name">车品名</param> 22 /// <param name="rentDate">租借日期</param> 23 /// <param name="rentUser">租借人</param> 24 /// <param name="yearOfService">服务年限</param> 25 public Car(string color, double dailyRent, string licenseNo, string name, int rentDate, string rentUser, string yearOfService) 26 : base(color, dailyRent, licenseNo, name, rentDate, rentUser,yearOfService) 27 { 28 } 29 30 /// <summary> 31 /// 重写Vehicle计算总价格的抽象方法 32 /// </summary> 33 /// <returns></returns> 34 public override double TotalPrice() 35 { 36 double price = this.RentDate * this.DailyRent; 37 return price; 38 } 39 } 40 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CarSys.Entity 8 { 9 /// <summary> 10 /// 货车类(子类) 11 /// </summary> 12 public class Truck:Vehicle 13 { 14 /// <summary> 15 /// 载重量 16 /// </summary> 17 public int Load { get; set; } 18 19 /// <summary> 20 /// 构造函数 21 /// </summary> 22 /// <param name="color">车颜色</param> 23 /// <param name="dailyRent">日租金</param> 24 /// <param name="licenseNo">车牌号</param> 25 /// <param name="name">车品名</param> 26 /// <param name="rentDate">租借日期</param> 27 /// <param name="rentUser">租借人</param> 28 /// <param name="yearOfService">服务年限</param> 29 /// <param name="load">载重量</param> 30 public Truck(string color, double dailyRent, string licenseNo, string name, int rentDate, string rentUser, string yearOfService,int load) 31 : base(color, dailyRent, licenseNo, name, rentDate, rentUser,yearOfService) 32 { 33 this.Load = load; 34 } 35 36 /// <summary> 37 /// 重写Vehicle计算总价格的抽象方法 38 /// </summary> 39 /// <returns></returns> 40 public override double TotalPrice() 41 { 42 double price = this.RentDate * this.DailyRent; 43 return price; 44 } 45 } 46 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CarSys.Entity 8 { 9 /// <summary> 10 /// 创建新车(工具类) 11 /// </summary> 12 public class VehicleUtil 13 { 14 /// <summary> 15 /// 创建车 16 /// </summary> 17 /// <param name="licenseNo"></param> 18 /// <param name="name"></param> 19 /// <param name="color"></param> 20 /// <param name="yearOfExp"></param> 21 /// <param name="dailyRent"></param> 22 /// <param name="load"></param> 23 /// <param name="type"></param> 24 /// <param name="rentDate"></param> 25 /// <param name="rentUser"></param> 26 /// <param name="yearOfSer"></param> 27 /// <returns></returns> 28 public static Vehicle CreateVehicle(string licenseNo, string name, string color, int yearOfExp, double dailyRent, int load, string type, int rentDate, string rentUser, string yearOfSer) 29 { 30 Vehicle vehicle = null; 31 switch (type) 32 { 33 case "Car": 34 vehicle = new Car(color,dailyRent,licenseNo,name,rentDate,rentUser,yearOfSer); 35 break; 36 case "Truck": 37 vehicle = new Truck(color, dailyRent, licenseNo, name, rentDate, rentUser, yearOfSer, load); 38 break; 39 } 40 return vehicle; 41 } 42 } 43 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using CarSys.Entity; 11 12 namespace CarSys 13 { 14 public partial class FrmMain : Form 15 { 16 public FrmMain() 17 { 18 InitializeComponent(); 19 } 20 21 VehicleUtil util; 22 23 /// <summary> 24 /// 保存了可租用车的集合 25 /// </summary> 26 public Dictionary<string, Vehicle> vehicle = new Dictionary<string,Vehicle>(); 27 28 /// <summary> 29 /// 保存已租出车的集合 30 /// </summary> 31 public Dictionary<string, Vehicle> rentVehicle = new Dictionary<string,Vehicle>(); 32 33 /// <summary> 34 /// 初始化数据 35 /// </summary> 36 public void Inital() 37 { 38 Vehicle car1 = new Car("蓝色", 300, "粤A111111", "AE86", 3 ,"A路人","3"); 39 Vehicle car2 = new Car("红色", 600, "粤A222222", "雪佛兰", 3,"B路人","3"); 40 Vehicle car3 = new Car("黑色", 1000, "粤A333333", "奥迪", 3,"C路人","3"); 41 42 Vehicle truck1 = new Truck("黑色", 200, "粤A000000", "水泥车", 3,"D路人","3", 500); 43 Vehicle truck2 = new Truck("黄色", 250, "粤A123456", "货车", 3,"E路人","3", 450); 44 Vehicle truck3 = new Truck("灰色", 300, "粤A654321", "水泥车", 3,"F路人","3", 300); 45 46 vehicle.Add(car1.LicenseNo, car1); 47 vehicle.Add(car2.LicenseNo, car2); 48 vehicle.Add(car3.LicenseNo, car3); 49 vehicle.Add(truck1.LicenseNo, truck1); 50 vehicle.Add(truck2.LicenseNo, truck2); 51 vehicle.Add(truck3.LicenseNo, truck3); 52 } 53 54 /// <summary> 55 /// 绑定租车的数据源 56 /// </summary> 57 public void BindList() 58 { 59 lvShow.Items.Clear(); 60 foreach (var item in vehicle.Values) 61 { 62 ListViewItem lvitem = new ListViewItem(item.LicenseNo); 63 lvitem.SubItems.Add(item.Name); 64 lvitem.SubItems.Add(item.Color.ToString()); 65 lvitem.SubItems.Add(item.RentDate.ToString()); 66 lvitem.SubItems.Add(item.DailyRent.ToString()); 67 if (item is Truck) 68 { 69 lvitem.SubItems.Add((item as Truck).Load.ToString()); 70 } 71 else 72 { 73 lvitem.SubItems.Add("无"); 74 } 75 lvShow.Items.Add(lvitem); 76 } 77 } 78 79 /// <summary> 80 /// 窗体加载 81 /// </summary> 82 /// <param name="sender"></param> 83 /// <param name="e"></param> 84 private void FrmMain_Load(object sender, EventArgs e) 85 { 86 Inital(); 87 } 88 89 /// <summary> 90 /// 点击租车的刷新 91 /// </summary> 92 /// <param name="sender"></param> 93 /// <param name="e"></param> 94 private void btnRefalsh_Click(object sender, EventArgs e) 95 { 96 BindList(); 97 } 98 99 /// <summary> 100 /// 退出程序 101 /// </summary> 102 /// <param name="sender"></param> 103 /// <param name="e"></param> 104 private void btnExit_Click(object sender, EventArgs e) 105 { 106 Application.Exit(); 107 } 108 109 /// <summary> 110 /// 点击租车 111 /// </summary> 112 /// <param name="sender"></param> 113 /// <param name="e"></param> 114 private void btnLend_Click(object sender, EventArgs e) 115 { 116 if (lvShow.SelectedItems.Count == 0) 117 { 118 MessageBox.Show("请选中车辆","提示"); 119 return; 120 } 121 if (string.IsNullOrEmpty(txtUser.Text.Trim())) 122 { 123 MessageBox.Show("请填写租用者姓名!","提示"); 124 return; 125 } 126 //获取选中项的第一列(车牌号) 127 string LicenseNo = lvShow.SelectedItems[0].Text; 128 //根据 车牌号 填写 租用者 129 vehicle[LicenseNo].RentUser = txtUser.Text.Trim(); 130 //为已租借车的集合添加数据 131 rentVehicle.Add(vehicle[LicenseNo].LicenseNo, vehicle[LicenseNo]); 132 if (vehicle.ContainsKey(LicenseNo)) 133 { 134 //根据车牌号移除数据 135 vehicle.Remove(LicenseNo); 136 } 137 BindList(); 138 MessageBox.Show("成功出租!","提示"); 139 } 140 141 /// <summary> 142 /// 绑定还车的数据源 143 /// </summary> 144 public void BindReturn() 145 { 146 lvReturn.Items.Clear(); 147 foreach (var item in rentVehicle.Values) 148 { 149 ListViewItem lvitem = new ListViewItem(item.LicenseNo); 150 lvitem.SubItems.Add(item.Name); 151 lvitem.SubItems.Add(item.Color.ToString()); 152 lvitem.SubItems.Add(item.RentDate.ToString()); 153 lvitem.SubItems.Add(item.DailyRent.ToString()); 154 if (item is Truck) 155 { 156 lvitem.SubItems.Add((item as Truck).Load.ToString()); 157 } 158 else 159 { 160 lvitem.SubItems.Add("无"); 161 } 162 lvReturn.Items.Add(lvitem); 163 } 164 } 165 166 /// <summary> 167 /// 点击还车的刷新 168 /// </summary> 169 /// <param name="sender"></param> 170 /// <param name="e"></param> 171 private void btnSumRefalsh_Click(object sender, EventArgs e) 172 { 173 BindReturn(); 174 } 175 176 /// <summary> 177 /// 点击选择结算(算租车费用) 178 /// </summary> 179 /// <param name="sender"></param> 180 /// <param name="e"></param> 181 private void btnSubmit_Click(object sender, EventArgs e) 182 { 183 if (lvReturn.SelectedItems.Count == 0) 184 { 185 MessageBox.Show("请选中车辆", "提示"); 186 return; 187 } 188 if (string.IsNullOrEmpty(txtLendDay.Text.Trim())) 189 { 190 MessageBox.Show("请填写租用天数!", "提示"); 191 return; 192 } 193 string CardId = lvReturn.SelectedItems[0].Text; 194 rentVehicle[CardId].RentDate = int.Parse(txtLendDay.Text.Trim()); 195 //计算价格 196 double price = rentVehicle[CardId].TotalPrice(); 197 string message = "总价格为: " + price.ToString(); 198 MessageBox.Show(message,"提示"); 199 //将已归还车辆添加至可租车当中 200 vehicle.Add(rentVehicle[CardId].LicenseNo, rentVehicle[CardId]); 201 if (rentVehicle.ContainsKey(CardId)) 202 { 203 rentVehicle.Remove(CardId); 204 } 205 MessageBox.Show("还车成功!", "提示"); 206 //刷新绑定 207 BindReturn(); 208 } 209 210 /// <summary> 211 /// 点击入库 212 /// </summary> 213 /// <param name="sender"></param> 214 /// <param name="e"></param> 215 private void btnIn_Click(object sender, EventArgs e) 216 { 217 if (string.IsNullOrEmpty(txtColor.Text) || string.IsNullOrEmpty(txtLicenseNo.Text) || string.IsNullOrEmpty(txtName.Text) ||string.IsNullOrEmpty(txtRentDate.Text) || string.IsNullOrEmpty(txtDailyRent.Text)) 218 { 219 MessageBox.Show("请填写信息!"); 220 return; 221 } 222 string vehicleType = null; 223 //选中卡车时,卡车载重可用 224 if (rbtnTruck.Checked) 225 { 226 vehicleType = "Truck"; 227 } 228 else if (rbtnCar.Checked) 229 { 230 vehicleType = "Car"; 231 } 232 else 233 { 234 MessageBox.Show("请选择车类型!"); 235 return; 236 } 237 string cardID = txtLicenseNo.Text.Trim(); 238 string type = txtName.Text.Trim(); 239 string color = txtColor.Text.Trim(); 240 int rendTime = int.Parse(txtRentDate.Text.Trim()); 241 int dailyMoney = int.Parse(txtDailyRent.Text.Trim()); 242 int load; 243 if (txtLoad.Enabled == false) 244 { 245 load = 0; 246 } 247 else 248 { 249 load = int.Parse(txtLoad.Text.Trim()); 250 } 251 252 Vehicle vehicle1 = VehicleUtil.CreateVehicle(cardID, type, color, 3, dailyMoney, load, vehicleType, rendTime, "", "3"); 253 254 vehicle.Add(vehicle1.LicenseNo, vehicle1); 255 MessageBox.Show("添加成功!"); 256 } 257 258 /// <summary> 259 /// 卡车选中项,改变卡车载重能否填写 260 /// </summary> 261 /// <param name="sender"></param> 262 /// <param name="e"></param> 263 private void rbtnTruck_CheckedChanged(object sender, EventArgs e) 264 { 265 if (rbtnTruck.Checked) 266 { 267 txtLoad.Enabled = true; 268 } 269 } 270 271 /// <summary> 272 /// 汽车选中项,改变卡车载重能否填写 273 /// </summary> 274 /// <param name="sender"></param> 275 /// <param name="e"></param> 276 private void rbtnCar_CheckedChanged(object sender, EventArgs e) 277 { 278 if (rbtnCar.Checked) 279 { 280 txtLoad.Enabled = false; 281 } 282 } 283 284 } 285 }
程序运行效果视频:
请移步至 -->http://www.bilibili.com/video/av9819927/
程序界面图: