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

 更新时间:2020年6月25日 11:24  点击:1776

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

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

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;
   }
  }
 }
}

我们来分类看看其中的魅力代码:

1.租车的界面功能

01.租车按钮  

//点击租车触发的事件
  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("租车成功");

02.刷新按钮

//刷新按钮
  private void btn_new_Click(object sender, EventArgs e)
  {
   //重新加载
   New(vehicles, lvlist);
  }

这里的刷新定义了一个方法,也就是动态加载ListView的方法(Nuw方法)

这个方法有两个参数,第一个参数传入车辆类型集合对象,第二个传入Listview的名字

这样的作用就是在租车和还车时都能使用此方法进行刷新,岂不妙哉!

 //动态加载listview的方法
  public void New(Dictionary<string,Vehicle> list,ListView lvlist) 
  {
   //初始化LIstviewItem对象
   ListViewItem listview = null;
    //清除Listview,以免有冲突的值
   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);
   }
   
  }

2.还车的界面功能

01.选择结算按钮

 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("还车成功");
   }
   

  }

02.刷新按钮(调用租车时写的方法)

private void button1_Click(object sender, EventArgs e)
  {
   //加载已出租车辆信息
   New(rentvehicles,lvlist_huan);
   
  }

3.新车入库界面功能

01.入库按钮

 //入库按钮点击事件
  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("请完善信息的填写!");
   }
   

  }

以上就是本文的全部内容,希望对大家的学习有所帮助。

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#中list用法实例

    这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25