天尽头的那片海 通过本文主要向大家介绍了马桶c的个人空间,c语言,欲情 c max,维生素c,奔驰c200等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
最近学习了继承,多态,集合,设计模式,有一个汽车租凭系统,给大家分享一下:
我们首先来看看我们这个系统的效果
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(); } }</div>
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; } } }</div>
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; } } }</div>
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; } } }</div>
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