首先要绑定初始数据
//保存可租用车的集合
Dictionary<string, Vehicle> vehicles=new Dictionary<string,Vehicle>();
//保存租出的车的集合
Dictionary<string, Vehicle> rentVehicles=new Dictionary<string,Vehicle>();
//初始信息
public void Initial()
{
Car car=new Car("京A10001","奥迪A6","黑色",3,240);
vehicles.Add(car.LicenseNo, car);
Truck truck = new Truck("京R44944", "东风", "蓝色", 3, 300,20);
vehicles.Add(truck.LicenseNo, truck);
Car car2=new Car("京A10002","奥迪A7","黑色",3,240);
vehicles.Add(car2.LicenseNo, car2);
//绑定数据
public void Binder()
{
this.lvRent.Items.Clear();
foreach (Vehicle item in vehicles.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());
}
lvRent.Items.Add(lvitem);
}
}
//租车 点击刷新时
private void btnQueryRent_Click(object sender, EventArgs e)
{
Binder();
}
//点击租车时
private void btnRent_Click(object sender, EventArgs e)
{
if (this.txtRenter.Text=="")
{
MessageBox.Show("请输入租车人姓名","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
string key = lvRent.SelectedItems[0].Text;
Vehicle ve = vehicles[key];
vehicles.Remove(key);
Binder();
rentVehicles.Add(key,ve);
MessageBox.Show("租车成功");
}
还车
//点击租车时
private void btnRent_Click(object sender, EventArgs e)
{
if (this.txtRenter.Text=="")
{
MessageBox.Show("请输入租车人姓名","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
string key = lvRent.SelectedItems[0].Text;
Vehicle ve = vehicles[key];
vehicles.Remove(key);
Binder();
rentVehicles.Add(key,ve);
MessageBox.Show("租车成功");
}
//退出
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
//还车 点击刷新时
private void btnQueryReturn_Click(object sender, EventArgs e)
{
this.lvReturn.Items.Clear();
foreach (Vehicle item in rentVehicles.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());
}
lvReturn.Items.Add(lvitem);
}
}
//结算金额
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 = rentVehicles[number];
rentVehicles.Remove(number);
Binder();
vehicles.Add(number, ve);
ve.RentDate = int.Parse( txtRentDate.Text);
double money = 0;
money = ve.CalcPrice();
MessageBox.Show("你需要支付"+money+"元");
}
private void btnAdd_Click(object sender, EventArgs e)
{
string licenseNo = this.txtAutoNum.Text;
string name = this.txtName.Text;
string color = this.chColor.Text;
int year = int.Parse(this.txtYears.Text);
double dailyRent = double.Parse(this.txtLetting.Text);
if (rdoCar.Checked)
{
Car car = new Car(licenseNo,name,color,year,dailyRent);
vehicles.Add(licenseNo,car);
MessageBox.Show("入库成功");
}
if (rdoTruck.Checked)
{
int load = int.Parse( this.txtLoad.Text);
Truck truck = new Truck(licenseNo, name, color, year, dailyRent,load);
vehicles.Add(licenseNo,truck);
MessageBox.Show("入库成功");
}
}
private void rdoCar_CheckedChanged(object sender, EventArgs e)
{
this.txtLoad.Enabled = false;
}
private void rdoTruck_CheckedChanged(object sender, EventArgs e)
{
this.txtLoad.Enabled = true;
}