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

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

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

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,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,double dailyrent,int load )
   :base(licenseno,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,int Load,string type)
  {
   //初始化父类对象
   Vehicle vehicle = null;
   switch (type)
   {
    //根据传来的值,实例化对应的对象
    case"卡车":
     vehicle = new Truck(liceseno,dailyrent,Load);
     break;
    case"轿车":
     vehicle = new Car(liceseno,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","宝马","黑色",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,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,lvlist);
   MessageBox.Show("租车成功");

02.刷新按钮

//刷新按钮
  private void btn_new_Click(object sender,lvlist);
  }

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

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

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

 //动态加载listview的方法
  public void New(Dictionary<string,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+"元",lvlist_huan);
    MessageBox.Show("还车成功");
   }
   

  }

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

private void button1_Click(object sender,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,ve);
    MessageBox.Show("入库成功");

    //清空所要填的值选项
    txt_id.Text="";
    txt_xing.Text="";
    cmb_color.Text="";
    txt_time.Text="";
    txt_money.Text= "";
    txt_zhong.Text = ""; 
   }
   else
   {

    MessageBox.Show("请完善信息的填写!");
   }
   

  }

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件的解析。本文会介绍CsvHelper、TextFieldParser、正则表达式三种解析CSV文件的方法,顺带也会介绍一下CSV文件的写方法。 CSV文件标准 在介绍CSV文件的读写方法前,我们需要了解一下CSV文件的格式。 文件示例 一
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的人能快速转入Halcon平台下,通过文中的示例开发者能快速了解一个Halcon项目开发的基本步骤,让开发者能把精力完全集中到算法的开发上面。 首先,你需要安装Halcon,HALCON 18.11.0.1的安装包会放在文章末尾。安装包分开发和
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期使用。 设置dll路径 参考C#开发奇技淫巧三:把dll放在不同的目录让你的程序更整洁中间的 方法一:配置App.config文件的privatePath : &lt;runtime&gt; &lt;assemblyBinding xml
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——NET的流行高性能JSON框架,当然也可以使用.NET自带的 System.Text.Json(.NET5)、DataContractJsonSerializer、JavaScriptSerializer(不推荐)。
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理机制,允许不同的组件之间进行彼此通信而又不需要相互依赖,达到一种解耦的目的。&#xA;EventBus维护一个事件的字典,发布者、订阅者在事件总线中获取事件实例并执行发布、订阅操作,事件实例负责维护、执行事件处理程序。
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api/trans/vip/translate,使用方法参考通用翻译API接入文档 。&#xA;请求方式可使用 GET 或 POST 方式(Content-Type 请指定为:application/x-www-for
词云”由美国西北大学新闻学副教授、新媒体专业主任里奇·戈登(Rich Gordon)于2006年最先使用,是通过形成“关键词云层”或“关键词渲染”,对文本中出现频率较高的“关键词”的视觉上的突出。词云图过滤掉大量的文本信息,使浏览者只要一眼扫过文本就可以领略文本的主旨。&#xA;网上大部分文章介绍的是使用P
微软在.NET中对串口通讯进行了封装,我们可以在.net2.0及以上版本开发时直接使用SerialPort类对串口进行读写操作。&#xA;为操作方便,本文对SerialPort类做了一些封装,暂时取名为**SerialPortClient**。
简介 管道为进程间通信提供了平台, 管道分为两种类型:匿名管道、命名管道,具体内容参考.NET 中的管道操作。简单来说,匿名管道只能用于本机的父子进程或线程之间,命名管道可用于远程主机或本地的任意两个进程,本文主要介绍命名管道的用法。 匿名管道在本地计算机上提供进程间通信。 与命名管道相比,虽然匿名
目录自定义日志类NLog版本的日志类Serilog版本的日志类 上个月换工作,新项目又要重新搭建基础框架,把日志实现部分单独记录下来方便以后参考。 自定义日志类 代码大部分使用ChatGPT生成,人工进行了测试和优化,主要特点: 线程安全,日志异步写入文件不影响业务逻辑 支持过期文件自动清理,也可自
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机自动启动的两种常用方法](https://blog.csdn.net/weixin_42288432/article/details/120059296),将里面中的第一种方法做了封装成**AutoStart**类,使
简介 FTP是FileTransferProtocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。 FTP
使用特性,可以有效地将元数据或声明性信息与代码(程序集、类型、方法、属性等)相关联。 将特性与程序实体相关联后,可以在运行时使用反射这项技术查询特性。&#xA;在 C# 中,通过用方括号 ([]) 将特性名称括起来,并置于应用该特性的实体的声明上方以指定特性。
# 简介 主流的识别库主要有ZXing.NET和ZBar,OpenCV 4.0后加入了QR码检测和解码功能。本文使用的是ZBar,同等条件下ZBar识别率更高,图片和部分代码参考[在C#中使用ZBar识别条形码](https://www.cnblogs.com/w2206/p/7755656.htm
C#中Description特性主要用于枚举和属性,方法比较简单,记录一下以便后期使用。 扩展类DescriptionExtension代码如下: using System; using System.ComponentModel; using System.Reflection; /// &lt;
本文实现一个简单的配置类,原理比较简单,适用于一些小型项目。主要实现以下功能:保存配置到json文件、从文件或实例加载配置类的属性值、数据绑定到界面控件。&#xA;一般情况下,项目都会提供配置的设置界面,很少手动更改配置文件,所以选择以json文件保存配置数据。
前几天用SerialPort类写一个串口的测试程序,关闭串口的时候会让界面卡死。网上大多数方法都是定义2个bool类型的标记Listening和Closing,关闭串口和接受数据前先判断一下。我的方法是DataReceived事件处理程序用this.BeginInvoke()更新界面,不等待UI线程
约束告知编译器类型参数必须具备的功能。 在没有任何约束的情况下,类型参数可以是任何类型。 编译器只能假定 System.Object 的成员,它是任何 .NET 类型的最终基类。 如果客户端代码使用不满足约束的类型,编译器将发出错误。 通过使用 where 上下文关键字指定约束。&#xA;最常用的泛型约束为
protobuf-net是用于.NET代码的基于契约的序列化程序,它以Google设计的“protocol buffers”序列化格式写入数据,适用于大多数编写标准类型并可以使用属性的.NET语言。&#xA;protobuf-net可通过NuGet安装程序包,也可直接访问github下载源码:https:/
工作中经常遇到需要实现TCP客户端或服务端的时候,如果每次都自己写会很麻烦且无聊,使用SuperSocket库又太大了。这时候就可以使用SimpleTCP了,当然仅限于C#语言。&#xA;SimpleTCP是一个简单且非常有用的 .NET 库,用于处理启动和使用 TCP 套接字(客户端和服务器)的重复性任务