C#实现简单的 * 系统
作者:天尽头的那片海 时间:2022-08-17 00:51:52
最近学习了继承,多态,集合,设计模式,有一个汽车租凭系统,给大家分享一下:
我们首先来看看我们这个系统的效果
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("请完善信息的填写!");
}
}
标签:C#, , ,系统
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot如何集成PageHelper分页功能
2022-10-22 03:18:34
Spring Security基于散列加密方案实现自动登录功能
2022-09-19 01:10:22
![](https://img.aspxhome.com/file/2023/5/86855_0s.png)
Spring Boot中如何使用Convert接口实现类型转换器
2023-05-09 20:28:37
![](https://img.aspxhome.com/file/2023/6/80906_0s.png)
Java实现堆排序和图解
2023-11-11 12:13:37
![](https://img.aspxhome.com/file/2023/6/59336_0s.png)
JWT在OpenFeign调用中进行令牌中继详解
2023-02-07 04:19:15
Spring MVC项目中log4J和AOP使用详解
2022-11-16 08:36:29
![](https://img.aspxhome.com/file/2023/0/76540_0s.png)
Android拼图游戏 玩转从基础到应用手势变化
2021-08-24 02:41:59
![](https://img.aspxhome.com/file/2023/7/95967_0s.gif)
23种设计模式(14)java迭代器模式
2021-12-12 15:28:49
![](https://img.aspxhome.com/file/2023/0/83560_0s.jpg)
Mybatis查询时,区分大小写操作
2021-08-11 14:10:54
一篇文章带你深入了解Java类加载
2023-06-11 22:12:00
![](https://img.aspxhome.com/file/2023/0/66710_0s.png)
java实现简单单链表
2023-10-30 09:45:46
![](https://img.aspxhome.com/file/2023/2/58722_0s.jpg)
Maven安装及MyEclipse中使用Maven
2023-06-20 04:29:07
![](https://img.aspxhome.com/file/2023/7/69307_0s.png)
LeetCode程序员面试题之无重复字符的最长子串
2021-09-12 05:26:36
MultipartResolver实现文件上传功能
2021-06-19 22:38:15
SpringSecurity认证流程详解
2022-06-25 21:44:49
![](https://img.aspxhome.com/file/2023/8/81988_0s.jpg)
mybatis Interceptor对UpdateTime自动处理的实现方法
2023-10-13 16:02:20
Java Set集合的遍历及实现类的比较
2023-11-05 16:08:10
springboot+gradle 构建多模块项目的步骤
2023-02-19 00:33:31
![](https://img.aspxhome.com/file/2023/0/61050_0s.jpg)
Java 关键字break和continue的使用说明
2023-07-22 03:50:59
Java LinkedList实现班级信息管理系统
2021-06-27 04:00:11
![](https://img.aspxhome.com/file/2023/7/84657_0s.jpg)