继承与多态之汽车租赁系统
1.租车
显示系统中所有可出租的汽车,选中要出租的汽车,输入租用人以出租汽车,如图所示
2.还车
在还车列表中选择汽车信息,输入出租天数,计算租金,如图所示
3.新车入库
需要录入汽车的车牌号,车型,颜色,使用时间,和每日租金,如果是卡车还要录入卡车的载重,如图所示
具体实现过程:
1.搭建系统
按照类图创建类,体会Vehicle,Trech和Car三个类之间的关系
Car类:
namespace _09汽车租赁系统
{
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;
}
}
}
Vehicle类
namespace _09汽车租赁系统
{
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;
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();
}
}
Trech类:
namespace _09汽车租赁系统
{
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;
}
}
}
//保存可租用车的集合(车辆名称,车辆对象)
Dictionary<string, Vehicle> notRent;
//保存已租用车辆的集合。
Dictionary<string, Vehicle> alreadyRent;
2.实现汽车出租
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("租车成功!");
3.实现还车
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+"元");
4.实现新车入库
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("添加成功!");