你知道汽车租赁系统的关键点吗?

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: |||超大  

汽车租赁系统

主界面如下:

关键点一:怎样理清各个类之间的关系?

一共需要4个类:Car类,Truck类,Vehicle类,VehicleUtil类

Car类:小汽车类 主要包括小汽车价格的计算方法

Truck类:货车类 主要包括货车费用的计算方法

Vehicle类:车辆类 描述车辆的一些基本信息

VehicleUtil类:工具类 创建汽车对象

下面附上一张类图:

关键点二:租车事件

首先要有两道验证:即 “输入出租人姓名验证” 和 “选择车辆验证”,代码如下:

1if(String.IsNullOrEmpty(this.txtRenter.Text))2{3MessageBox.Show("请输入租车人姓名","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);4return;5}6if(this.lvRent.SelectedItems.Count ==0)7{8MessageBox.Show("请选择车辆","提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);9}

租车完整代码如下:

1PRivatevoidbtnRent_Click(objectsender, EventArgs e)2{3stringkey =null;4if(String.IsNullOrEmpty(this.txtRenter.Text))5{6MessageBox.Show("请输入租车人姓名","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);7return;8}9if(this.lvRent.SelectedItems.Count ==0)10{11MessageBox.Show("请选择车辆","提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);12}13else14{15key = lvRent.SelectedItems[0].Text;16vehicles[key].RentUser =this.txtRenter.Text;17rentVehicles.Add(vehicles[key].LicenseNO, vehicles[key]);18if(vehicles.ContainsKey(key))19{20vehicles.Remove(key);21}22PrintVehicles(vehicles, lvRent);23MessageBox.Show("已出租。","提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);24}25}

关键点三:还车事件

开始依然是两道验证,模式如上。

验证“选择车辆” 和 “输入租车天数” 关键代码:

1if(String.IsNullOrEmpty(this.txtRentDate.Text))2{3MessageBox.Show("请输入租车天数","提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);4return;5}6if(this.lvReturn.SelectedItems.Count ==0)7{8MessageBox.Show("请选择车辆","提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);9}

完整还车结算代码如下:

1privatevoidbtnCompute_Click(objectsender, EventArgs e)2{3doubletotalPrice =0;4stringkey =null;5if(String.IsNullOrEmpty(this.txtRentDate.Text))6{7MessageBox.Show("请输入租车天数","提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);8return;9}10if(this.lvReturn.SelectedItems.Count ==0)11{12MessageBox.Show("请选择车辆","提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);13}14else15{16key = lvReturn.SelectedItems[0].Text;17rentVehicles[key].RentDate =int.Parse(this.txtRentDate.Text);18//调用抽象方法19totalPrice =rentVehicles[key].CalcPrice();20stringmsg =string.Format("您的总价是{0}。", totalPrice.ToString());21MessageBox.Show(msg,"提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);2223vehicles.Add(rentVehicles[key].LicenseNO, rentVehicles[key]);2425if(rentVehicles.ContainsKey(key))26{27rentVehicles.Remove(key);28}29this.PrintVehicles(rentVehicles, lvReturn);30}31}

关键点四:新车入库

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

关键代码如下:

1if(rdoCar.Checked)2{3type ="car";4}5if(rdoTruck.Checked)6{7type ="truck";8load =int.Parse(this.txtLoad.Text);9}

新车入库完整代码如下:

1privatevoidbtnAdd_Click(objectsender, EventArgs e)2{3try4{5stringLicenseNO =this.txtAutoNum.Text;6stringname =this.txtName.Text;7stringcolor =this.cobColor.Text;8intyears =int.Parse(this.txtYears.Text);9doubleDailyRent =double.Parse(this.txtLetting.Text);10intload =0;11stringtype =null;12if(rdoCar.Checked)13{14type ="car";15}16if(rdoTruck.Checked)17{18type ="truck";19load =int.Parse(this.txtLoad.Text);20}21Vehicle auto =VehicleUtil.CreateVehicle(LicenseNO, name, color, years, DailyRent, load, type);22vehicles.Add(auto.LicenseNO, auto);23MessageBox.Show("添加成功。","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);24}25catch(Exception ex)26{27MessageBox.Show("入库数据不正确!","错误!",MessageBoxButtons.OK,MessageBoxIcon.Error);28}29finally30{31this.txtAutoNum.Text ="";32this.txtLetting.Text ="";33this.txtLoad.Text ="";34this.txtName.Text ="";35this.txtRentDate.Text ="";36this.txtRenter.Text ="";37this.txtYears.Text ="";38}39}

关键点五:刷新

首先要把listView的项清一下:

1listView.Items.Clear();

刷新关键代码如下:

1privatevoidPrintVehicles(Dictionary<string, Vehicle>autos, ListView listView)2{3listView.Items.Clear();4if(autos.Count ==0)5{6MessageBox.Show("没有数据","提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);7}8else9{10foreach(Vehicle autoinautos.Values)11{12ListViewItem item =newListViewItem(auto.LicenseNO);13if(autoisCar)14{15item.SubItems.AddRange(newstring[]{auto.Name,auto.Color,auto.YearsOfService.ToString(),16auto.DailyRent.ToString(),"无"});17}18if(autoisTruck)19{20item.SubItems.AddRange(newstring[]{auto.Name,auto.Color,auto.YearsOfService.ToString(),21auto.DailyRent.ToString(),((Truck)auto).Load.ToString()});22}23listView.Items.Add(item);24}25}26}

再在按钮中调用方法:

1privatevoidbtnQueryRent_Click(objectsender, EventArgs e)2{3this.PrintVehicles(vehicles, lvRent);4}

知识回顾:窗体传值

方法一、利用属性传值

BackGround:①点击 Button按钮,将主窗体Form1中textBox1 中的值传到 Form2中的textBox2中。② 点击Form2中的按钮,将Form2中textBox的值传给主窗体的文本框。1、 在Form2中定义一个字段,封装成属性:privatestringflag;///<summary>///接收传过来的值///</summary>publicstringFlag

{get{returnflag; }set{ flag =value; }

}2、 在Form1 Button按钮事件中,实例化一个Form2 窗体对象,并将textBox1中的值赋给 Form2中的Flag,这样在窗体Form2的登录事件中就可以获取到窗体Form1传过来的值。

窗体:Form1中的代码:privatevoidbutton1_Click(objectsender, EventArgs e)

{

Form2 f2=newForm2();

f2.Flag=textBox1.Text;//关键地方 ↓if(f2.ShowDialog() ==DialogResult.OK)

{

textBox1.Text=f2.Flag;

}

}

窗体:Form2的Load()事件privatevoidForm2_Load(objectsender, EventArgs e)

{

textBox1.Text=this.flag;

}3、 子窗体传值给父窗体(回传) 点击Form2中的button按钮将Form2中textBox的值传给父窗体Form1.

窗体:Form2中的代码privatevoidbutton1_Click(objectsender, EventArgs e)

{

flag=this.textBox1.Text;//关键地方 ↓this.DialogResult =DialogResult.OK;

}

方法二、利用子窗体中的构造函数 (实现了父窗体给子窗体的传值,但是子窗体的值回传暂未实现)1、 重载窗体Form2中的 构造函数stringstr = String.Empty;//接收传过来的值publicForm2(stringtextValue)

{

InitializeComponent();this.str =textValue;

}2、 主窗体调用子窗体时候传参数:主窗体Form1的Button事件

Form2 f2=newForm2(textBox1.Text);

f2.ShowDialog();

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航