一、系統窗體 二、思路分析: 我們看見這有三個類分別是:Vehicle 交通工具類父類 Car和Truck分別是Vehicle是它的子類 需要用到繼承和多態、簡單工廠的知識點進行書寫 1)vehic類 public abstract class Vehicle { //無參數 public Vehi
一、系統窗體
二、思路分析:
我們看見這有三個類分別是: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(); } }
這個程式完畢了,也許會有瑕疵,希望我能不斷進步。