你的位置:首页 > ASP.net教程

[ASP.net教程]汽车租凭系统


最近学习了继承,多态,集合,设计模式,有一个汽车租凭系统,给大家分享一下:

我们首先来看看我们这个系统的效果

1.做一个项目,我们首先对项目进行分析

  根据我们最近学的知识,我们可以看出继承,多态,集合,设计模式,我们都能用到

  我们把所需要的类和简单模式中的“简单工厂”的工厂准备好

  类图:

 

 

  01.车辆类(父类)

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 汽车租凭系统{  // 车辆类 父类  public abstract class Vehicle  {    //属性    //车牌号    public string LicenseNo { get; set; }       //车名    public string Name { get; set; }    //颜色    public string Color { get; set; }    //使用时间    public int RentDate { get; set; }    //使用人    public string RentUser { get; set; }    //日租金    public double DailyRent { get; set; }    //还车日期    public int ReturnDate { get; set; }    public Vehicle() { }    //构造    public Vehicle(string liceseno,string name,string color,int rentdate,double dailyrent)    {      this.Color = color;      this.DailyRent = dailyrent;      this.LicenseNo = liceseno;      this.Name = name;      this.RentDate = rentdate;     }    //计算价格的虚方法    public abstract double GetNum();      }}

  02.子类汽车类   (继承 车辆类 父类)

  

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 汽车租凭系统{  //汽车类 子类  public class Car:Vehicle  {    public Car() { }    //构造    public Car(string licenseno, string name, string color, int rentdate, double dailyrent)      : base(licenseno, name, color, rentdate, dailyrent)     { }    //重写父类计算价格的方法    public override double GetNum()    {      //日租金*租的天数      double result = this.DailyRent * this.ReturnDate;      return result;    }  }}

  03.子类卡车类 继承 车辆类 父类

  

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 汽车租凭系统{  //子类  public class Truck:Vehicle  {    //载重    public int Load { get; set; }    public Truck() { }    //构造    public Truck(string licenseno,string name,string color,int rentdate,double dailyrent,int load )      :base(licenseno,name,color,rentdate,dailyrent )    {      this.Load = load;    }    //重写父类计算价格的方法    public override double GetNum()    {      //日租金*租的天数      double result = this.DailyRent * this.ReturnDate;      return result;    }  }}

 04.“简单工厂”的工厂类

   说这个工厂类,就是为了在新车入库的时候,可以知道它是轿车还是卡车,实例化不同的对象,方便使用

  

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 汽车租凭系统{  //工厂类  public class VehicleFactory  {    //带参静态方法    public static Vehicle Carteshow(string liceseno, string name, string color, int rentdate, double dailyrent, int Load,string type)    {      //初始化父类对象      Vehicle vehicle = null;      switch (type)      {        //根据传来的值,实例化对应的对象        case"卡车":          vehicle = new Truck(liceseno, name, color, rentdate, dailyrent, Load);          break;        case"轿车":          vehicle = new Car(liceseno, name, color, rentdate, dailyrent);          break;      }      //返回实例化对象      return vehicle;            }  }}

2. 剩下的就是对主窗体的功能进行实现

  其实租车和还车的核心就是两个集合之间的交互

   新车入库就是使用“简单工厂”的设计模式进行对应添加

  其中有个我们以前没见过的控件TabControl

   

  

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 汽车租凭系统{  public partial class FrmMain : Form  {    public FrmMain()    {      InitializeComponent();    }    //保存可租车辆的集合    Dictionary<string, Vehicle> vehicles=new Dictionary<string,Vehicle>();    //保存租出车的集合    Dictionary<string, Vehicle> rentvehicles=new Dictionary<string,Vehicle>();    //动态加载listview的方法    public void New(Dictionary<string,Vehicle> list,ListView lvlist)     {      ListViewItem listview = null;      lvlist.Items.Clear();      foreach (Vehicle item in list.Values)      {        if (item is Car)        {          listview = new ListViewItem();          listview.Text = item.LicenseNo;          listview.SubItems.Add(item.Name);          listview.SubItems.Add(item.Color);          listview.SubItems.Add(item.RentDate.ToString());          listview.SubItems.Add(item.DailyRent.ToString());        }        else if (item is Truck)        {          listview = new ListViewItem();          listview.Text = item.LicenseNo;          listview.SubItems.Add(item.Name);          listview.SubItems.Add(item.Color);          listview.SubItems.Add(item.RentDate.ToString());          listview.SubItems.Add(item.DailyRent.ToString());          listview.SubItems.Add(((Truck)item).Load.ToString());        }        lvlist.Items.Add(listview);      }          }    //准备可租车辆    public void Intitle()     {            Truck truck = new Truck("京A111","奥迪","红色",3,240,10);      Car car = new Car("京A222", "宝马", "黑色", 3, 240);      vehicles.Add(truck.LicenseNo,truck);      vehicles.Add(car.LicenseNo, car);      //加载数据      New(vehicles,lvlist);         }    private void FrmMain_Load(object sender, EventArgs e)    {      Intitle();    }    //点击租车触发的事件    private void btn_zu_Click(object sender, EventArgs e)    {      if(lvlist.SelectedItems.Count==0)            {        MessageBox.Show("请选中一行!");        return;      }      if (txt_name.Text=="")      {        MessageBox.Show("请输入姓名!");        return;      }      //执行租车.      //获取车牌号的值      string carnum = lvlist.SelectedItems[0].Text;      Vehicle ve= vehicles[carnum];      //直接把获得要租的信息放入rentvehicles集合      rentvehicles.Add(carnum,ve);      //删除原来的集合      vehicles.Remove(carnum);      //重新加载      New(vehicles,lvlist);      MessageBox.Show("租车成功");    }    private void button1_Click(object sender, EventArgs e)    {      //加载已出租车辆信息      New(rentvehicles,lvlist_huan);          }    private void btn_ji_Click(object sender, EventArgs e)    {      if (txt_day.Text=="")      {        MessageBox.Show("请输入天数");        return;      }      if (lvlist_huan.SelectedItems.Count==0)      {        MessageBox.Show("请选择一行");        return;      }      //获取车牌号的值      string carnum1 = lvlist_huan.SelectedItems[0].Text;      Vehicle ve = rentvehicles[carnum1];          //获取租的天数      int num = Convert.ToInt32(txt_day.Text);      ve.ReturnDate = num;      double money=ve.GetNum();      DialogResult result= MessageBox.Show("你要支付"+money+"元","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);      if (result==DialogResult.OK)      {        //直接把获得要还的信息放入vehicles集合        vehicles.Add(carnum1, ve);        //删除原来的集合        rentvehicles.Remove(carnum1);        //重新加载        New(rentvehicles, lvlist_huan);        MessageBox.Show("还车成功");      }          }    //刷新按钮    private void btn_new_Click(object sender, EventArgs e)    {      //重新加载      New(vehicles, lvlist);    }    //判断填写是否正确的方法    public bool Good()     {      bool flag = true;      if (txt_id.Text==""||txt_xing.Text==""||cmb_color.Text==""||txt_time.Text==""||txt_money.Text==""||txt_zhong.Text==""|| rdb_jiao.Text=="")      {        flag = false;      }      return flag;    }    //入库按钮点击事件    private void btn_ruku_Click(object sender, EventArgs e)    {      if (Good())//判断填写是否正确      {        foreach (string item in vehicles.Keys)        {          if (txt_id.Text==item)          {            MessageBox.Show("此车牌已经有库存了,请你确认!");            return;          }        }        //使用"简单工厂"        Vehicle ve = null;        if (rdb_jiao.Checked == true)        {          ve = VehicleFactory.Carteshow(txt_id.Text, txt_xing.Text, cmb_color.Text,Convert.ToInt32(txt_time.Text), Convert.ToDouble(txt_money.Text), Convert.ToInt32(txt_zhong.Text), rdb_jiao.Text);        }        else        {          ve = VehicleFactory.Carteshow(txt_id.Text, txt_xing.Text, cmb_color.Text, Convert.ToInt32(txt_time.Text), Convert.ToDouble(txt_money.Text), Convert.ToInt32(txt_zhong.Text), rdb_ka.Text);        }        //添加集合        vehicles.Add(txt_id.Text, ve);        MessageBox.Show("入库成功");        //清空所要填的值选项        txt_id.Text="";        txt_xing.Text="";        cmb_color.Text="";        txt_time.Text="";        txt_money.Text= "";        txt_zhong.Text = "";       }      else      {        MessageBox.Show("请完善信息的填写!");      }          }    //选择不同的按钮    private void rdb_jiao_CheckedChanged(object sender, EventArgs e)    {      if (rdb_jiao.Checked==true)      {        lab_zhong.ForeColor = Color.Red;        txt_zhong.Enabled = false;      }      else      {        lab_zhong.ForeColor = Color.Black;        txt_zhong.Enabled = true;      }    }  }}