一:汽车类
public abstract class Vehicle { public Vehicle() { } public Vehicle(string licenseNO,string name,string color,int yearOfService,double dailyRent) { this.licenseNO = licenseNO; this.name = name; this.color = color; this.yearOfService = yearOfService;a this.dailyRent = dailyRent; } //租用日期 private int rentDate; public int RentDate { get { return rentDate; } set { rentDate = value; } } //租用者 private string rentUser; public string RentUser { get { return rentUser; } set { rentUser = value; } } //日租金 private double dailyRent; public double DailyRent { get { return dailyRent; } set { dailyRent = value; } } //使用时间 private int yearOfService; public int YearOfService { get { return yearOfService; } set { yearOfService = value; } } //颜色 private string color; public string Color { get { return color; } set { color = value; } } //车名 private string name; public string Name { get { return name; } set { name = value; } } //车牌号 private string licenseNO; public string LicenseNO { get { return licenseNO; } set { licenseNO = value; } } //计算价格的方法 public abstract double CalcPrice(); }二:轿车类 public class Car:Vehicle { public Car(string licenseNO, string name, string color, int yearsOfService, double dailyRent) : base(licenseNO, name, color, yearsOfService, dailyRent) { ; } public override double CalcPrice() { double totalPrice = 0; double basicPrice = this.RentDate * this.DailyRent; if (this.RentDate <= 30) { totalPrice = basicPrice; } else { totalPrice = basicPrice + (this.RentDate - 30) * this.DailyRent * 0.1; } return totalPrice; } } 三:卡车类 public class Truck:Vehicle { public Truck() { } public Truck(string licenseNO, string name, string color, int yearsOfService, double dailyRent, int load) : base(licenseNO, name, color, yearsOfService, dailyRent) { this.Load = load; } //载重量 private int load; public int Load { get { return load; } set { load = value; } } //卡车费用计算方法 // 30天以内(含30)按日租金计算 // 30天以上超出部分:每天,每吨(载重量)增加日租金10% public override double CalcPrice() { double totalPrice = 0; double basicPrice = RentDate * DailyRent; if (RentDate<=30) { totalPrice = basicPrice; } else { totalPrice = basicPrice + (RentDate - 30) * (DailyRent * 0.1)*load; } return totalPrice; } } 四:汽车类型"工厂" public class VehicleFactory { public static Vehicle CreateVehicle(string licenseNO,string name,string color,int yearOfService,double dailyRent,int load,string type) { Vehicle vehicle = null; switch (type) { case "car": vehicle = new Car(licenseNO, name, color, yearOfService, dailyRent); break; case "truck": vehicle = new Truck(licenseNO,name,color,yearOfService,dailyRent,load); break; default: break; } return vehicle; } } 五:在主窗体加载汽车信息 public partial class Rent : Form { public Rent() { InitializeComponent(); } //保存可租用车的集合(车辆名称,车辆对象) Dictionary<string, Vehicle> notRent; //保存已租用车辆的集合。 Dictionary<string, Vehicle> alreadyRent; private void btnQueryRent_Click(object sender, EventArgs e) { //刷新 MyRefresh(notRent, lvRent); } //构造出两辆小汽车,两辆卡车 public void LoadData() { //实例化未出租字典类型的集合 notRent = new Dictionary<string, Vehicle>(); Car car = new Car("京R00544", "奥迪A8", "黑色", 3, 240); Truck truck = new Truck("京R44944", "东风", "蓝色", 3, 300, 20); notRent.Add(car.LicenseNO,car); notRent.Add(truck.LicenseNO,truck); //实例化已出租字典类型的集合 alreadyRent = new Dictionary<string, Vehicle>(); //出租出去的车 Car rentCar = new Car("粤A001", "宝马318", "白色", 3, 250); rentCar.RentUser = txtRenter.Text; Truck rentTruck = new Truck("粤A002", "东风", "蓝色", 3, 400, 30); alreadyRent.Add(rentCar.LicenseNO, rentCar); alreadyRent.Add(rentTruck.LicenseNO,rentTruck); } 六:绑定TreeView,刷新显示 public void MyRefresh(Dictionary<string,Vehicle> rnotrent, ListView lvshow) { lvshow.Items.Clear(); foreach (Vehicle item in rnotrent.Values) { ListViewItem lvitem = new ListViewItem(item.LicenseNO); if (item is Car) { lvitem.SubItems.Add(item.Name); lvitem.SubItems.Add(item.Color); lvitem.SubItems.Add(item.YearOfService.ToString()); lvitem.SubItems.Add(item.DailyRent.ToString()); } if (item is Truck) { lvitem.SubItems.Add(item.Name); lvitem.SubItems.Add(item.Color); lvitem.SubItems.Add(item.YearOfService.ToString()); lvitem.SubItems.Add(item.DailyRent.ToString()); lvitem.SubItems.Add(((Truck)item).Load.ToString()); } lvshow.Items.Add(lvitem); } } //刷新 private void btnQueryReturn_Click(object sender, EventArgs e) { MyRefresh(alreadyRent,lvReturn); } 七:租车 private void btnRent_Click(object sender, EventArgs e) { if (txtRenter.Text=="") { MessageBox.Show("请输入租车人名称"); return; } //从可租车辆集合中移除车辆A //将A添加到已租车辆集合中 if (lvRent.SelectedItems.Count>0) { string number = lvRent.SelectedItems[0].Text; Vehicle ve = notRent[number]; notRent.Remove(number); MyRefresh(notRent,lvRent); alreadyRent.Add(number, ve); MessageBox.Show("租车成功!"); } } 八:还车界面的结算 private void btnCompute_Click(object sender, EventArgs e) { if (txtRentDate.Text=="") { MessageBox.Show("请输入租车时间"); return; } //01.将车A从已租集合中移除 //02,将车A加入到可租车辆中 string number=lvReturn.SelectedItems[0].Text; Vehicle ve = alreadyRent[number]; alreadyRent.Remove(number); MyRefresh(alreadyRent, lvReturn); notRent.Add(number, ve); ve.RentDate = Convert.ToInt32(txtRentDate.Text); double money=0; money = ve.CalcPrice(); MessageBox.Show("您需要支付"+money+"元"); } 九:新车入库的添加 private void btnAdd_Click(object sender, EventArgs e) { string lincesN0=txtAutoNum.Text; string name=txtName.Text; string color=cobColor.Text; int time=Convert.ToInt32(txtYears.Text); double dailyRent=Convert.ToInt32(txtLetting.Text); if (rdoCar.Checked) { Car car = new Car(lincesN0, name, color, time, dailyRent); notRent.Add(lincesN0, car); } if (rdoTruck.Checked) { int load = Convert.ToInt32(txtLoad.Text); Truck truck = new Truck(lincesN0, name, color, time, dailyRent, load); notRent.Add(lincesN0, truck); } MessageBox.Show("添加成功!"); } 十:轿车和卡车的按钮 private void rdoCar_CheckedChanged(object sender, EventArgs e) { txtLoad.Enabled = false; } private void rdoTruck_CheckedChanged(object sender, EventArgs e) { txtLoad.Enabled = true; }