深入.NET和C#的小型汽车租赁系统的框架
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了深入.NET和C#的小型汽车租赁系统的框架相关的知识,希望对你有一定的参考价值。
前言:写这个小型系统之前呢,我们应该要猜测可能要用到哪些知识点。
那么对于这个小型系统:主要用到了如下的知识:
封装,集合(ArrayList和HashTable)和泛型和非泛型集合(泛型:List<>和非泛型:Dictioanry<>),
构造函数,方法重载,多态(继承,抽象:抽象类和抽象方法)
效果图:
第一步.
创建几个类
01:工具类(vehicle)(父类,超类,基类),此类为抽象类
有方法重载,构造函数
该类主要负责提供车类的属性
属性:
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; }
02:创建一个汽车类(Car)(子类)
构造函数,方法重载。
03:创建一个卡车 类(Truck)(子类)
构造函数,方法重载
属性:
public int Load
{
get { return load; }
set { load = value; }
}
04:创建一个工具(工厂)类(VehicleUtil)
第二步.
代码实现思路:
01:工具,父类(vehicle):
代码如下:
public Vehicle() { } public Vehicle(string color, double dailyrent, string licenseno, string name, int yearsofservice) { Color = color; DailyRent = dailyrent; LicenseNO = licenseno; Name = name; YearsOfService = yearsofservice; } public abstract double CalcPrice();
02:汽车类(Car)
代码如下:
public Car() { } public Car(string licenseno, string name, string color, int yearsofservice, double dailyrent) { Color = color; DailyRent = dailyrent; LicenseNO = licenseno; Name = name; YearsOfService = yearsofservice; } public override double CalcPrice() { double totalPrice = 0; totalPrice = RentDate * DailyRent; return totalPrice; }
03:卡车 类(Truck)
代码如下:
public Truck(string licenseno, string name, string color, int yearsofservice, double dailyrent, int load) { Color = color; DailyRent = dailyrent; LicenseNO = licenseno; Name = name; YearsOfService = yearsofservice; Load = load; } public override double CalcPrice() { double totalPrice = 0; totalPrice = RentDate * DailyRent; return totalPrice; }
04:工具(工厂)类(VehicleUtil
代码如下:
public static Vehicle CreateVehicle(string licenseno, string name, string color, int yearsofservice, double dailyrent, int load, string type) { Vehicle vehicle = null; switch (type) { case "Car": vehicle = new Car(licenseno, name, color, yearsofservice, dailyrent); break; case "Truck": vehicle = new Truck(licenseno, name, color, yearsofservice, dailyrent, load); break; default: break; } return vehicle; }
第三步.
出租事件:(btnRent_Click(object sender, EventArgs e))
关键代码如下:
if (lvlist.SelectedItems.Count == 0) { MessageBox.Show("请选择一行"); return; } if (txtUser.Text == "") { MessageBox.Show("请输入客户名字!"); return; } string key = lvlist.SelectedItems[0].Text; Vehicle vehicle = vehicles[key]; vehicles.Remove(key); rentvehicles.Add(key, vehicle); PrintVehicles(vehicles, lvlist); MessageBox.Show("出租成功!!"); //PrintVehicles绑定数据到listview上 public void PrintVehicles(Dictionary<string, Vehicle> listprirnt, ListView lvinfo) { ListViewItem lv=null; lvinfo.Items.Clear(); foreach (Vehicle item in listprirnt.Values) { if (item is Car) { lv = new ListViewItem(item.LicenseNO); lv.SubItems.Add(item.Name); lv.SubItems.Add(item.Color.ToString()); lv.SubItems.Add(item.YearsOfService.ToString()); lv.SubItems.Add(item.DailyRent.ToString()); lvinfo.Items.Add(lv); } else if(item is Truck) { lv = new ListViewItem(item.LicenseNO); lv.SubItems.Add(item.Name); lv.SubItems.Add(item.Color.ToString()); lv.SubItems.Add(item.YearsOfService.ToString()); lv.SubItems.Add(item.DailyRent.ToString()); lv.SubItems.Add(((Truck)item).Load.ToString()); lvinfo.Items.Add(lv); }
第四步
还车.在btnGiveMoney_Click事件中写
if (txtDayNum.Text == string.Empty) { MessageBox.Show("请输入天数!"); return; } string addNum = lvlistGive.SelectedItems[0].Text; Vehicle vechile = rentvehicles[addNum]; rentvehicles.Remove(addNum); vechile.RentDate = Convert.ToInt32(txtDayNum.Text); double money = 0; //调用计算价格方法 money = vechile.CalcPrice(); MessageBox.Show("你一共要福le" + money); vehicles.Add(addNum, vechile); //刷新 PrintVehicles(rentvehicles, lvlistGive); MessageBox.Show("成功!!");
第五步.
添加车辆在 btnIncAR_Click事件中写
if (txtWeight.Text == "" || txtUseTime.Text == "" || txtType.Text == "" || txtID.Text == "" || txtDayRentMoney.Text == "" || cboColor.Text == "") { MessageBox.Show("请您填写完整!"); } else { string licon = txtID.Text; string color = cboColor.Text; double mon = Convert.ToDouble(txtDayRentMoney.Text); int tine = Convert.ToInt32(txtUseTime.Text); string ty = txtType.Text; if (radJiaoCar.Checked) { Car ca = new Car(licon, ty, color, tine, mon); vehicles.Add(licon, ca); } if (radKaCar.Checked) { int load = Convert.ToInt32(txtWeight.Text); Truck tr = new Truck(licon, ty, color, tine, mon, load); vehicles.Add(licon, tr); } MessageBox.Show("添加成功!"); }
注:此文没有代码的注释,是因为想在看代码的任何情况下,
也能看懂代码的含义。
以上是关于深入.NET和C#的小型汽车租赁系统的框架的主要内容,如果未能解决你的问题,请参考以下文章