
·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 你知道汽车租赁系统的关键点吗?
汽车租赁系统
主界面如下:

关键点一:怎样理清各个类之间的关系?
一共需要4个类:Car 类,Truck类,Vehicle类,VehicleUtil类
Car类:小汽车类 主要包括小汽车价格的计算方法
Truck类:货车类 主要包括货车费用的计算方法
Vehicle类:车辆类 描述车辆的一些基本信息
VehicleUtil类:工具类 创建汽车对象
下面附上一张类图:

关键点二:租车事件
首先要有两道验证:即 “输入出租人姓名验证” 和 “选择车辆验证”,代码如下:
1 if (String.IsNullOrEmpty(this.txtRenter.Text))
2 {
3 MessageBox.Show("请输入租车人姓名","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);
4 return;
5 }
6 if (this.lvRent.SelectedItems.Count == 0)
7 {
8 MessageBox.Show("请选择车辆", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
9 }
租车完整代码如下:
1 PRivate void btnRent_Click(object sender, EventArgs e)
2 {
3 string key = null;
4 if (String.IsNullOrEmpty(this.txtRenter.Text))
5 {
6 MessageBox.Show("请输入租车人姓名","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);
7 return;
8 }
9 if (this.lvRent.SelectedItems.Count == 0)
10 {
11 MessageBox.Show("请选择车辆", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
12 }
13 else
14 {
15 key = lvRent.SelectedItems[0].Text;
16 vehicles[key].RentUser = this.txtRenter.Text;
17 rentVehicles.Add(vehicles[key].LicenseNO, vehicles[key]);
18 if(vehicles.ContainsKey(key))
19 {
20 vehicles.Remove(key);
21 }
22 PrintVehicles(vehicles, lvRent);
23 MessageBox.Show("已出租。", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
24 }
25 }
关键点三:还车事件
开始依然是两道验证,模式如上。


验证“选择车辆” 和 “输入租车天数” 关键代码:
1 if (String.IsNullOrEmpty(this.txtRentDate.Text))
2 {
3 MessageBox.Show("请输入租车天数", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
4 return;
5 }
6 if (this.lvReturn.SelectedItems.Count == 0)
7 {
8 MessageBox.Show("请选择车辆", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
9 }
完整还车结算代码如下:
1 private void btnCompute_Click(object sender, EventArgs e)
2 {
3 double totalPrice = 0;
4 string key = null;
5 if (String.IsNullOrEmpty(this.txtRentDate.Text))
6 {
7 MessageBox.Show("请输入租车天数", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
8 return;
9 }
10 if (this.lvReturn.SelectedItems.Count == 0)
11 {
12 MessageBox.Show("请选择车辆", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
13 }
14 else
15 {
16 key = lvReturn.SelectedItems[0].Text;
17 rentVehicles[key].RentDate = int.Parse(this.txtRentDate.Text);
18 //调用抽象方法
19 totalPrice = rentVehicles[key].CalcPrice();
20 string msg = string.Format("您的总价是{0}。", totalPrice.ToString());
21 MessageBox.Show(msg, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
22
23 vehicles.Add(rentVehicles[key].LicenseNO, rentVehicles[key]);
24
25 if (rentVehicles.ContainsKey(key))
26 {
27 rentVehicles.Remove(key);
28 }
29 this.PrintVehicles(rentVehicles, lvReturn);
30 }
31 }
关键点四:新车入库

这里要注意的是:选择轿车单选钮的时候,卡车载重文本框是不可用状态。选择卡车单选钮则是可用状态


关键代码如下:
1 if (rdoCar.Checked)
2 {
3 type = "car";
4 }
5 if (rdoTruck.Checked)
6 {
7 type = "truck";
8 load = int.Parse(this.txtLoad.Text);
9 }
新车入库完整代码如下:
1 private void btnAdd_Click(object sender, EventArgs e)
2 {
3 try
4 {
5 string LicenseNO = this.txtAutoNum.Text;
6 string name = this.txtName.Text;
7 string color = this.cobColor.Text;
8 int years = int.Parse(this.txtYears.Text);
9 double DailyRent = double.Parse(this.txtLetting.Text);
10 int load = 0;
11 string type = null;
12 if (rdoCar.Checked)
13 {
14 type = "car";
15 }
16 if (rdoTruck.Checked)
17 {
18 type = "truck";
19 load = int.Parse(this.txtLoad.Text);
20 }
21 Vehicle auto = VehicleUtil.CreateVehicle(LicenseNO, name, color, years, DailyRent, load, type);
22 vehicles.Add(auto.LicenseNO, auto);
23 MessageBox.Show("添加成功。","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);
24 }
25 catch (Exception ex)
26 {
27 MessageBox.Show("入库数据不正确!","错误!",MessageBoxButtons.OK,MessageBoxIcon.Error);
28 }
29 finally
30 {
31 this.txtAutoNum.Text = "";
32 this.txtLetting.Text = "";
33 this.txtLoad.Text = "";
34 this.txtName.Text = "";
35 this.txtRentDate.Text = "";
36 this.txtRenter.Text = "";
37 this.txtYears.Text = "";
38 }
39 }
关键点五:刷新
首先要把listView的项清一下:
1 listView.Items.Clear();
刷新关键代码如下:
1 private void PrintVehicles(Dictionary<string, Vehicle> autos, ListView listView)
2 {
3 listView.Items.Clear();
4 if (autos.Count == 0)
5 {
6 MessageBox.Show("没有数据", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
7 }
8 else
9 {
10 foreach (Vehicle auto in autos.Values)
11 {
12 ListViewItem item = new ListViewItem(auto.LicenseNO);
13 if (auto is Car)
14 {
15 item.SubItems.AddRange(new string[]{auto.Name,auto.Color,auto.YearsOfService.ToString(),
16 auto.DailyRent.ToString(),"无"});
17 }
18 if (auto is Truck)
19 {
20 item.SubItems.AddRange(new string[]{auto.Name,auto.Color,auto.YearsOfService.ToString(),
21 auto.DailyRent.ToString(),((Truck)auto).Load.ToString()});
22 }
23 listView.Items.Add(item);
24 }
25 }
26 }
再在按钮中调用方法:
1 private void btnQueryRent_Click(object sender, EventArgs e)
2 {
3 this.PrintVehicles(vehicles, lvRent);
4 }
知识回顾:窗体传值
方法一、利用属性传值
BackGround:①点击 Button按钮,将主窗体Form1中textBox1 中的值传到 Form2中的textBox2中。② 点击Form2中的按钮,将Form2中textBox的值传给主窗体的文本框。
1、 在Form2中定义一个字段,封装成属性:
private string flag;
/// <summary>
/// 接收传过来的值
/// </summary>
public string Flag
{
get { return flag; }
set { flag = value; }
}
2、 在Form1 Button按钮事件中,实例化一个Form2 窗体对象,并将textBox1中的值赋给 Form2中的Flag,这样在窗体Form2的登录事件中就可以获取到窗体Form1传过来的值。
窗体:Form1中的代码:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Flag = textBox1.Text;
//关键地方 ↓
if (f2.ShowDialog() == DialogResult.OK)
{
textBox1.Text = f2.Flag;
}
}
窗体:Form2的Load()事件
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.flag;
}
3、 子窗体传值给父窗体(回传) 点击Form2中的button按钮将Form2中textBox的值传给父窗体Form1.
窗体:Form2中的代码
private void button1_Click(object sender, EventArgs e)
{
flag = this.textBox1.Text;
//关键地方 ↓
this.DialogResult = DialogResult.OK;
}
方法二、利用子窗体中的构造函数 (实现了父窗体给子窗体的传值,但是子窗体的值回传暂未实现)
1、 重载窗体Form2中的 构造函数
string str = String.Empty;//接收传过来的值
public Form2(string textValue)
{
InitializeComponent();
this.str = textValue;
}
2、 主窗体调用子窗体时候传参数:主窗体Form1的Button事件
Form2 f2 = new Form2(textBox1.Text);
f2.ShowDialog();