一、系统窗体
二、思路分析:
我们看见这有三个类分别是:Vehicle 交通工具类父类 Car和Truck分别是Vehicle是它的子类 需要用到继承和多态、简单工厂的知识点进行书写
1)vehic类
public abstract class Vehicle { //无参数 public Vehicle() { } //有参数 public Vehicle(string Name, string LicenseNo, string Color, int YearsOfService, double DailyRent) { this.Name = Name; this.LicenseNo = LicenseNo; this.Color = Color; this.YearsOfService = YearsOfService; this.DailyRent = DailyRent; } //颜色 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 abstract double CalcPrice(); }
2)Car小轿车类
//小汽车 public class Car:Vehicle { public Car() { } public Car(string Name, string LicenseNo, string Color, int YearsOfService, double DailyRent) : base(Name, LicenseNo, Color, YearsOfService, DailyRent) { } //计算价格的 public override double CalcPrice() { double totalPrice = 0; double baseicPrice = this.RentDate*this.DailyRent; if (this.RentDate <= 30) { totalPrice = baseicPrice; } else { totalPrice = baseicPrice + (this.RentDate - 30) * this.DailyRent * 0.1; } return totalPrice; } }
3)Truck卡车类
//大卡车 public class Truck:Vehicle { //载重量 public Truck() { } public Truck(string Name, string LicenseNo, string Color, int YearsOfService, double DailyRent,int Load) :base(Name,LicenseNo,Color,YearsOfService,DailyRent) { this.Load = Load; } public int Load { get; set; } //计算价格的 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; } }
4)我们不知道用户会选择什么类型所以我们有借助一个工厂类帮助我们做这件事。VehicleFactory
public class VehicleFactory { public static Vehicle ReadMagenner(string Name, string LicenseNo, string Color, int YearsOfService, double DailyRent, string Type, int Load) { Vehicle vehicle = null; switch (Type) { case"Car": vehicle = new Car( Name,LicenseNo,Color,YearsOfService,DailyRent); break; case"Truck": vehicle = new Truck(Name, LicenseNo, Color, YearsOfService, DailyRent, Load); break; } return vehicle; } }
5)我们要在FrmMain主窗体进行
public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } //保存可租用车的集合(车辆名称,车辆对象) Dictionary<string, Vehicle> notRent = new Dictionary<string, Vehicle>(); //保存已租用车辆的集合。 Dictionary<string, Vehicle> alreadyRent = new Dictionary<string, Vehicle>(); //构造出两辆小汽车、卡车在Lind事件中调用 public void LoadData() { Car Car = new Car("奥迪A8", "京R00544", "黑色", 3, 240); Truck truck = new Truck("东风", "京A9988770", "蓝色", 3, 300, 240); notRent.Add(truck.LicenseNo, truck); notRent.Add(Car.LicenseNo, Car); //出租出去的车 Car rentCar = new Car("奥A001","东风","红色",3,200); rentCar.RentUser = tbPeople.Text; Truck rentTruck = new Truck("京B111","宝马","红色",3,200,300); alreadyRent.Add(rentCar.LicenseNo,rentCar); alreadyRent.Add(rentTruck.LicenseNo, rentTruck); } private void FrmMain_Load(object sender, EventArgs e) { //将数据加载到集合中 LoadData(); //默认我的大卡车载重不能用 tbBigCar.Enabled = false; } private void btNew_Click(object sender, EventArgs e) { //刷新租车 控件的名字 MyRefresh(notRent, ListCar); } //刷新租车的按钮调用的方法 public void MyRefresh(Dictionary<string, Vehicle> rnotrent, ListView lvshow) { //list(汽车)进行清空 ListCar.Items.Clear(); foreach (Vehicle item in rnotrent.Values) { //ListView 添加值 ListViewItem lvitem = new ListViewItem(item.LicenseNo); if (item is Car) { lvitem.SubItems.Add(item.Name); lvitem.SubItems.Add(item.Color); lvitem.SubItems.Add(item.YearsOfService.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.YearsOfService.ToString()); lvitem.SubItems.Add(item.DailyRent.ToString()); lvitem.SubItems.Add(((Truck)item).Load.ToString()); } lvshow.Items.Add(lvitem); } } private void btCar_Click(object sender, EventArgs e) { //判断是否写租车人的名称 if (tbPeople.Text=="") { MessageBox.Show("请输入租车人名称"); return; } //从可租车辆集合中移除车辆A //将A添加到已租车辆集合中 if (ListCar.SelectedItems.Count>0) { //??看看 string number = ListCar.SelectedItems[0].SubItems[0].Text; Vehicle ve=notRent[number]; notRent.Remove(number); MyRefresh(notRent,ListCar); alreadyRent.Add(number,ve); MessageBox.Show("租车成功"); } } private void btreturnCar_Click(object sender, EventArgs e) { //用于保存已租的车辆 控件名 MyRefresh(alreadyRent, listReturn); } private void btSelect_Click(object sender, EventArgs e) { //判断 if (tbDay.Text=="") { MessageBox.Show("请输入租车时间"); return; } //将车A从一组集合中移除 //将车A加入到可租车辆中 string number = listReturn.SelectedItems[0].SubItems[0].Text; Vehicle ve=alreadyRent[number]; alreadyRent.Remove(number); MyRefresh(alreadyRent,listReturn); notRent.Add(number,ve); ve.RentDate = Convert.ToInt32(tbDay.Text); double money = 0; money = ve.CalcPrice(); MessageBox.Show("您需要支付" + money + "元"); } private void btPut_Click(object sender, EventArgs e) { string lincesNo = tbCarNum.Text; string name = tbType.Text; string color = cbClor.Text; int time = Convert.ToInt32(tbTime.Text); double dailyRent = Convert.ToInt32(tbDayMoney.Text); if (rbSmallCar.Checked) { Car car = new Car(lincesNo, name, color, time, dailyRent); notRent.Add(lincesNo,car); } if (rbBigCar.Checked) { int load = Convert.ToInt32(tbBigCar.Text); Truck truck = new Truck(lincesNo, name, color, time, dailyRent, load); notRent.Add(lincesNo, truck); } MessageBox.Show("添加成功。。。。。。"); } private void rbSmallCar_CheckedChanged(object sender, EventArgs e) { tbBigCar.Enabled = false; } private void rbBigCar_CheckedChanged(object sender, EventArgs e) { tbBigCar.Enabled = true; } private void btExit_Click(object sender, EventArgs e) { this.Close(); } }
这个程序完毕了,也许会有瑕疵,希望我能不断进步。