C#实现简单的汽车租赁系统

2019-12-30 12:25:40刘景俊

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. 剩下的就是对主窗体的功能进行实现

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

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