1 using PK10.Etity;
2 using System;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Drawing;
7 using System.Linq;
8 using System.Text;
9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11
12 namespace PK10
13 {
14 public partial class FrmMain : Form
15 {
16 public FrmMain()
17 {
18 InitializeComponent();
19 Init();
20 BingDing();
21 }
22 /// <summary>
23 /// 保存可租车的集合
24 /// </summary>
25 Dictionary<string, Vebicle> vebicles = new Dictionary<string, Vebicle>();
26
27 /// <summary>
28 ///保存出租的车的集合
29 /// </summary>
30 Dictionary<string, Vebicle> rentvebicles = new Dictionary<string, Vebicle>();
31
32 /// <summary>
33 /// 初始化可出租车辆信息
34 /// </summary>
35 public void Init()
36 {
37 dgvZhuChe.AutoGenerateColumns = false;
38 dgvHuanChe.AutoGenerateColumns = false;
39 Vebicle ds = new Car(null, "红色", 250, "粤A52445", "奥迪A6", 3);
40 Vebicle ds1 = new Car(null, "蓝色", 240, "粤B54666", "奔驰", 3);
41 Vebicle ds2 = new Truck("20", "白色", 260, "泸C515547", "宝马R8", 3);
42
43 //添加到可租集合
44 vebicles.Add(ds.LicenseNo, ds);
45 vebicles.Add(ds1.LicenseNo, ds1);
46 vebicles.Add(ds2.LicenseNo, ds2);
47 foreach (var item in vebicles.Values)
48 {
49 if (item.Load == null)
50 {
51 item.Load = "无";
52 }
53 }
54
55 //已出租车辆
56 Vebicle bb = new Car(null, "红色", 250, "粤A88888", "奥迪A6", 3);
57 Vebicle bb1 = new Car(null, "蓝色", 240, "粤B66666", "奔驰", 3);
58 Vebicle bb2 = new Truck("30", "白色", 260, "泸C545557", "宝马R8", 3);
59
60 //添加到已租集合
61 rentvebicles.Add(bb.LicenseNo, bb);
62 rentvebicles.Add(bb1.LicenseNo, bb1);
63 rentvebicles.Add(bb2.LicenseNo, bb2);
64 foreach (var item in rentvebicles.Values)
65 {
66 if (item.Load == null)
67 {
68 item.Load = "无";
69 }
70 }
71
72 }
73 /// <summary>
74 /// 绑定
75 /// </summary>
76 public void BingDing()
77 {
78 ////绑定可租车
79 BindingSource bd = new BindingSource();
80 bd.DataSource = vebicles.Values.ToList();
81 this.dgvZhuChe.DataSource = bd;
82
83 //已出租车辆
84 BindingSource ds = new BindingSource();
85 ds.DataSource = rentvebicles.Values.ToList();
86 this.dgvHuanChe.DataSource = ds;
87
88 }
89 /// <summary>
90 /// 租车
91 /// </summary>
92 /// <param name="sender"></param>
93 /// <param name="e"></param>
94 private void btnZuChe_Click(object sender, EventArgs e)
95 {
96 //判断
97 if (dgvZhuChe.SelectedRows.Count > 0)
98 {
99 if (!string.IsNullOrEmpty(txtName.Text.Trim()))
100 {
101 string LicenseNO = dgvZhuChe.SelectedRows[0].Cells[0].Value.ToString();
102 Vebicle Che = null;
103 if (vebicles[LicenseNO] is Car)
104 {
105 Che = new Car();
106 Che = vebicles[LicenseNO];
107 }
108 if (vebicles[LicenseNO] is Truck)
109 {
110 Che = new Truck();
111 Che = vebicles[LicenseNO];
112 }
113 Che.RentUser = txtName.Text;
114 //添加车辆到已租车集合
115 rentvebicles.Add(Che.LicenseNo, Che);
116 //删除指定车辆的可租车集合
117 vebicles.Remove(LicenseNO);
118 MessageBox.Show("客户" + Che.RentUser + "租车成功!");
119 BingDing();
120 }
121 else
122 {
123 MessageBox.Show("请填写租用者", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
124 }
125 }
126 else
127 {
128 MessageBox.Show("请选择租用车辆", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
129 }
130 }
131
132 /// <summary>
133 ///刷新可租用车辆列表
134 /// </summary>
135 /// <param name="sender"></param>
136 /// <param name="e"></param>
137 private void btnNew_Click(object sender, EventArgs e)
138 {
139 BingDing();
140 }
141 /// <summary>
142 /// 退出
143 /// </summary>
144 /// <param name="sender"></param>
145 /// <param name="e"></param>
146 private void btnOut_Click(object sender, EventArgs e)
147 {
148 Application.Exit();
149 }
150 /// <summary>
151 /// 选择结算
152 /// </summary>
153 /// <param name="sender"></param>
154 /// <param name="e"></param>
155 private void btnJieSuan_Click(object sender, EventArgs e)
156 {
157 if (dgvHuanChe.SelectedRows.Count > 0)
158 {
159 if (!string.IsNullOrEmpty(txtDay.Text.Trim()))
160 {
161 //找到还哪一辆车
162 string Key = dgvHuanChe.SelectedRows[0].Cells[0].Value.ToString();
163 //按照天数给该车属性赋值
164 rentvebicles[Key].RentDate = int.Parse(txtDay.Text);
165 //调用计算总价方法 该车总价
166 MessageBox.Show("还车成功!总价为:" + rentvebicles[Key].Money() + "元");
167 //还车成功,添加到可租车集合
168 vebicles.Add(rentvebicles[Key].LicenseNo, rentvebicles[Key]);
169 //还车成功!在已租车删除该车
170 rentvebicles.Remove(Key);
171 BingDing();
172 }
173 else
174 {
175 MessageBox.Show("请输入租用天数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
176 }
177 }
178 else
179 {
180 MessageBox.Show("请选择需要归还的车", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
181 }
182 }
183 /// <summary>
184 /// 车辆入库
185 /// </summary>
186 /// <param name="sender"></param>
187 /// <param name="e"></param>
188 private void btnRuKu_Click(object sender, EventArgs e)
189 {
190 Vebicle Pand = null;
191 //判断输入是否为空
192 if (string.IsNullOrEmpty(txtChePaiHao.Text.Trim()))
193 { }
194 else if (string.IsNullOrEmpty(txtCheType.Text.Trim()))
195 { }
196 else if (string.IsNullOrEmpty(txtColor.Text.Trim()))
197 { }
198 else if (string.IsNullOrEmpty(txtShiJian.Text.Trim()))
199 { }
200 else if (string.IsNullOrEmpty(txtZuJin.Text.Trim()))
201 { }
202 else if (string.IsNullOrEmpty(txtZaiZhon.Text.Trim()))
203 {
204 MessageBox.Show("信息不能为空!");
205 }
206 else
207 {
208 if (rbtCar.Checked == true)
209 {
210 Pand = new Car();
211 }
212 else if (rbtTruck.Checked == true)
213 {
214 Pand = new Truck();
215 (Pand as Truck).Load = txtZaiZhon.Text;
216 }
217
218
219 Pand.LicenseNo = txtChePaiHao.Text;
220 Pand.Name = txtCheType.Text;
221 Pand.Color = txtColor.Text;
222 Pand.YearsOfService = int.Parse(txtShiJian.Text);
223 Pand.DailyRent = int.Parse(txtZuJin.Text);
224
225 try
226 {
227 vebicles.Add(Pand.LicenseNo, Pand);
228 MessageBox.Show("添加成功~!");
229 }
230 catch (Exception)
231 {
232 MessageBox.Show("不能有重复的车辆!");
233 }
234 }
235 }
236 }
237 }