• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#实现简单的汽车租赁系统

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

作者:天尽头的那片海 字体:[增加 减小] 来源:互联网 时间:2017-05-28

天尽头的那片海 通过本文主要向大家介绍了马桶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



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#利用ReportViewer生成报表
  • C#基于正则去掉注释的方法示例
  • C#中new的用法及与override的区别分析
  • C#实现两个richtextbox控件滚动条同步滚动的简单方法
  • C# for循环的经典案例集锦
  • C#操作word的方法示例
  • C#使用WebClient登录网站并抓取登录后的网页信息实现方法
  • C# WinForm制作异形窗体与控件的方法
  • C#实现Excel表数据导入Sql Server数据库中的方法
  • C#使用NPOI上传excel

相关文章

  • 2017-05-28C#使用非托管代码直接修改字符串的方法
  • 2017-05-28C# 实现QQ式截图功能实例代码
  • 2017-05-28c# Invoke和BeginInvoke 区别分析
  • 2017-05-28C#中如何使用Winform实现炫酷的透明动画界面
  • 2017-05-28C#异常处理详解
  • 2017-05-28C#读取中文文件出现乱码的解决方法
  • 2017-05-28C#遍历删除字符串中重复字符
  • 2017-05-28C#实现将程序运行信息写入日志的方法
  • 2017-05-28VB.NET中TextBox的智能感知应用实例
  • 2017-05-28C#中IEnumerable、ICollection、IList、List之间的区别

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#获取串口列表实现实时监控串口
    • C#编程实现连接SQL SERVER数据库实例详解
    • WPF中引入WindowsForms控件的方法
    • C# 游戏外挂实现核心代码
    • c#桥接模式(bridge结构模式)用法实例
    • 基于C#中XmlWriter写入Xml的深入分析
    • C#使用IComparer自定义List类实现排序的方法
    • c#转换全角半角方法示例
    • C#实现无限级联下拉列表框
    • C#保存listbox中数据到文本文件的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有