using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RentCar
{
public partial class FrmRentCar : Form
{
public FrmRentCar()
{
InitializeComponent();
Init();
}
//保存可租用车的集合
Dictionary<string, Vehicle> vehicles = new Dictionary<string,Vehicle>();
//保存租出车的集合
Dictionary<string, Vehicle> rentvehicles = new Dictionary<string,Vehicle>();
//初始化车辆
public void Init()
{
//可租车辆
Car ao = new Car();
ao.Name = "奥迪A6";
ao.LicenseNo = "京R5655";
ao.Color = "黑色";
ao.YearOfService = 3;
ao.DailyRent = 240;
Car ao2 = new Car();
ao2.Name = "奥迪A8";
ao2.LicenseNo = "赣A2333";
ao2.Color = "黄色";
ao2.YearOfService = 3;
ao2.DailyRent = 250;
vehicles.Add(ao.LicenseNo, ao);
vehicles.Add(ao2.LicenseNo, ao2);
//要还车辆
Car fen = new Car();
fen.Name = "火箭狂雷";
fen.LicenseNo = "沪A2333";
fen.Color = "白色";
fen.YearOfService = 3;
fen.DailyRent = 250;
fen.RentUser = "++";
fen.RentDate = 6;
Car fen2 = new Car();
fen2.Name = "兰博基尼";
fen2.LicenseNo = "粤A2333";
fen2.Color = "金色";
fen2.YearOfService = 1;
fen2.DailyRent = 5000;
fen2.RentUser = "sha强";
fen2.RentDate = 2;
rentvehicles.Add(fen.LicenseNo, fen);
rentvehicles.Add(fen2.LicenseNo, fen2);
}
//绑定lvRCar数据源
public void BindLv()
{
lvRCar.Items.Clear();
foreach (var item in vehicles.Values)
{
ListViewItem lv = new ListViewItem(item.LicenseNo);
lv.SubItems.Add(item.Name);
lv.SubItems.Add(item.Color);
lv.SubItems.Add(item.YearOfService.ToString());
lv.SubItems.Add(item.DailyRent.ToString());
lvRCar.Items.Add(lv);
}
}
//绑定lvRRar数据源
public void BindLvG()
{
lvGCar.Items.Clear();
foreach (var item in rentvehicles.Values)
{
ListViewItem lv = new ListViewItem(item.LicenseNo);
lv.SubItems.Add(item.Name);
lv.SubItems.Add(item.Color);
lv.SubItems.Add(item.YearOfService.ToString());
lv.SubItems.Add(item.DailyRent.ToString());
lvGCar.Items.Add(lv);
}
}
//单击刷新可租车辆
private void btnRefurbish_Click(object sender, EventArgs e)
{
BindLv();
}
//单击刷新已租出车辆
private void btnRefurbishs_Click(object sender, EventArgs e)
{
BindLvG();
}
private void btnRCar_Click(object sender, EventArgs e)
{
if (lvRCar.SelectedItems.Count == 0)
{
MessageBox.Show("请选择要租的车!");
}
else if (string.IsNullOrEmpty(txtRName.Text.Trim()))
{
MessageBox.Show("请输入租赁人姓名!");
}
else
{
string key = lvRCar.SelectedItems[0].Text;
vehicles[key].RentUser = this.txtRName.Text;
rentvehicles.Add(vehicles[key].LicenseNo,vehicles[key]);
if (vehicles.ContainsKey(key))
{
vehicles.Remove(key);
}
MessageBox.Show("已租出!");
BindLv();
BindLvG();
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnChoice_Click(object sender, EventArgs e)
{
if (lvRCar.SelectedItems.Count < 0)
{
MessageBox.Show("请选择要还的车!");
}
else if (string.IsNullOrEmpty(txtRDay.Text.Trim()))
{
MessageBox.Show("请输入天数!");
}
else
{
string key = lvGCar.SelectedItems[0].Text;
rentvehicles[key].RentDate = int.Parse(this.txtRDay.Text);
//调用抽象方法
double totprice = rentvehicles[key].CalcPrice();
string msg = string.Format("您的总价是{0}", totprice.ToString());
MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//加入可租车辆集合
vehicles.Add(rentvehicles[key].LicenseNo, rentvehicles[key]);
//还车后从已租车辆集合中移除
if (rentvehicles.ContainsKey(key))
{
rentvehicles.Remove(key);
}
BindLv();
BindLvG();
}
}
//选中卡车类型后,卡车载重可用
private void rbtnCamion_CheckedChanged(object sender, EventArgs e)
{
label11.Enabled = true;
txtZai.Enabled = true;
}
//选中轿车类型后,卡车载重不可用
private void rbtnSaloonCar_CheckedChanged(object sender, EventArgs e)
{
label11.Enabled = false;
txtZai.Enabled = false;
}
//添加车
public void Adds()
{
bool sa = true;
if (string.IsNullOrEmpty(txtCId.Text) || string.IsNullOrEmpty(this.txtColor.Text) || string.IsNullOrEmpty(this.txtCType.Text) || string.IsNullOrEmpty(this.txtDailyRent.Text) || string.IsNullOrEmpty(this.txtRentDay.Text))
{
MessageBox.Show("请完善车辆信息");
sa = false;
}
else
{
foreach (string item in vehicles.Keys)
{
if (txtCId.Text.Trim().Equals(item))
{
MessageBox.Show("已有该车,入库失败!!");
sa = false;
}
}
foreach (string item in rentvehicles.Keys)
{
if (txtCId.Text.Trim().Equals(item))
{
MessageBox.Show("已有该车,入库失败!!");
sa = false;
}
}
if (sa)
{
vehicles.Add(txtCId.Text,new Car(txtCId.Text,txtRName.Text,txtColor.Text,int.Parse(txtRentDay.Text),double.Parse(txtDailyRent.Text)));
MessageBox.Show("入库成功!!");
BindLv();
BindLvG();
}
}
}
private void btnRuKu_Click(object sender, EventArgs e)
{
Adds();
}
}
}