博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第十章,租车系统
阅读量:5346 次
发布时间:2019-06-15

本文共 9810 字,大约阅读时间需要 32 分钟。

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 RentCar{    public partial class FrmRentCar : Form    {        public FrmRentCar()        {            InitializeComponent();            Init();        }        //保存可租用车的集合        Dictionary
vehicles = new Dictionary
(); //保存租出车的集合 Dictionary
rentvehicles = new Dictionary
(); //初始化车辆 public void Init() { //可租车辆 Car ao = new Car(); ao.Name = "奥迪A6"; ao.LicenseNo = "京R5655"; ao.Color = "黑色"; ao.YearOfService = 3; ao.DailyRent = 240; Car ao2 = new Car(); ao2.Name = "奥迪A8"; ao2.LicenseNo = "赣A2333"; ao2.Color = "黄色"; ao2.YearOfService = 3; ao2.DailyRent = 250; vehicles.Add(ao.LicenseNo, ao); vehicles.Add(ao2.LicenseNo, ao2); //要还车辆 Car fen = new Car(); fen.Name = "火箭狂雷"; fen.LicenseNo = "沪A2333"; fen.Color = "白色"; fen.YearOfService = 3; fen.DailyRent = 250; fen.RentUser = "++"; fen.RentDate = 6; Car fen2 = new Car(); fen2.Name = "兰博基尼"; fen2.LicenseNo = "粤A2333"; fen2.Color = "金色"; fen2.YearOfService = 1; fen2.DailyRent = 5000; fen2.RentUser = "sha强"; fen2.RentDate = 2; rentvehicles.Add(fen.LicenseNo, fen); rentvehicles.Add(fen2.LicenseNo, fen2); } //绑定lvRCar数据源 public void BindLv() { lvRCar.Items.Clear(); foreach (var item in vehicles.Values) { ListViewItem lv = new ListViewItem(item.LicenseNo); lv.SubItems.Add(item.Name); lv.SubItems.Add(item.Color); lv.SubItems.Add(item.YearOfService.ToString()); lv.SubItems.Add(item.DailyRent.ToString()); lvRCar.Items.Add(lv); } } //绑定lvRRar数据源 public void BindLvG() { lvGCar.Items.Clear(); foreach (var item in rentvehicles.Values) { ListViewItem lv = new ListViewItem(item.LicenseNo); lv.SubItems.Add(item.Name); lv.SubItems.Add(item.Color); lv.SubItems.Add(item.YearOfService.ToString()); lv.SubItems.Add(item.DailyRent.ToString()); lvGCar.Items.Add(lv); } } //单击刷新可租车辆 private void btnRefurbish_Click(object sender, EventArgs e) { BindLv(); } //单击刷新已租出车辆 private void btnRefurbishs_Click(object sender, EventArgs e) { BindLvG(); } private void btnRCar_Click(object sender, EventArgs e) { if (lvRCar.SelectedItems.Count == 0) { MessageBox.Show("请选择要租的车!"); } else if (string.IsNullOrEmpty(txtRName.Text.Trim())) { MessageBox.Show("请输入租赁人姓名!"); } else { string key = lvRCar.SelectedItems[0].Text; vehicles[key].RentUser = this.txtRName.Text; rentvehicles.Add(vehicles[key].LicenseNo,vehicles[key]); if (vehicles.ContainsKey(key)) { vehicles.Remove(key); } MessageBox.Show("已租出!"); BindLv(); BindLvG(); } } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } private void btnChoice_Click(object sender, EventArgs e) { if (lvRCar.SelectedItems.Count < 0) { MessageBox.Show("请选择要还的车!"); } else if (string.IsNullOrEmpty(txtRDay.Text.Trim())) { MessageBox.Show("请输入天数!"); } else { string key = lvGCar.SelectedItems[0].Text; rentvehicles[key].RentDate = int.Parse(this.txtRDay.Text); //调用抽象方法 double totprice = rentvehicles[key].CalcPrice(); string msg = string.Format("您的总价是{0}", totprice.ToString()); MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); //加入可租车辆集合 vehicles.Add(rentvehicles[key].LicenseNo, rentvehicles[key]); //还车后从已租车辆集合中移除 if (rentvehicles.ContainsKey(key)) { rentvehicles.Remove(key); } BindLv(); BindLvG(); } } //选中卡车类型后,卡车载重可用 private void rbtnCamion_CheckedChanged(object sender, EventArgs e) { label11.Enabled = true; txtZai.Enabled = true; } //选中轿车类型后,卡车载重不可用 private void rbtnSaloonCar_CheckedChanged(object sender, EventArgs e) { label11.Enabled = false; txtZai.Enabled = false; } //添加车 public void Adds() { bool sa = true; if (string.IsNullOrEmpty(txtCId.Text) || string.IsNullOrEmpty(this.txtColor.Text) || string.IsNullOrEmpty(this.txtCType.Text) || string.IsNullOrEmpty(this.txtDailyRent.Text) || string.IsNullOrEmpty(this.txtRentDay.Text)) { MessageBox.Show("请完善车辆信息"); sa = false; } else { foreach (string item in vehicles.Keys) { if (txtCId.Text.Trim().Equals(item)) { MessageBox.Show("已有该车,入库失败!!"); sa = false; } } foreach (string item in rentvehicles.Keys) { if (txtCId.Text.Trim().Equals(item)) { MessageBox.Show("已有该车,入库失败!!"); sa = false; } } if (sa) { vehicles.Add(txtCId.Text,new Car(txtCId.Text,txtRName.Text,txtColor.Text,int.Parse(txtRentDay.Text),double.Parse(txtDailyRent.Text))); MessageBox.Show("入库成功!!"); BindLv(); BindLvG(); } } } private void btnRuKu_Click(object sender, EventArgs e) { Adds(); } }}
主窗体
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace RentCar 8 { 9     public class Car:Vehicle10     {11         public Car()12         {13         }14         public Car(string licenseNo, string name, string color, int yearOfService, double dailyRent)15             : base(licenseNo, name, color, yearOfService, dailyRent)16         {17 18         }19 20         public override double CalcPrice()21         {22             double pirce = 0;23             pirce = this.DailyRent * this.RentDate;24             return pirce;25         }26     }27 }
Car类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace RentCar 8 { 9     public class Truck:Vehicle10     {11         //载重12         public int Load { get; set; }13         public Truck():base()14         {15 16         }17         public Truck(string licenseNo, string name, string color, int yearOfService, double dailyRent, int load)18             : base(licenseNo, name, color, yearOfService, dailyRent)19         {20             this.Load = load;21         }22 23         public override double CalcPrice()24         {25             double pirce = 0;26             pirce = this.DailyRent * this.RentDate;27             return pirce;28         }29     }30 }
Truck类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace RentCar 8 { 9     public abstract class Vehicle10     {11         //颜色12         public string Color { get; set; }13         //日租金14         public double DailyRent { get; set; }15         //车牌号16         public string LicenseNo { get; set; }17         //名字18         public string Name { get; set; }19         //租用时间20         public int RentDate { get; set; }21         //租用者22         public string RentUser { get; set; }23         //最少租用时长24         public int YearOfService { get; set; }25 26         public Vehicle(string color, double dailyRent, string licenseNo, string name, int rentDate, string rentUser, int yearsOfService)27         {28             this.Color = color;29             this.DailyRent = dailyRent;30             this.LicenseNo = licenseNo;31             this.Name = name;32             this.RentDate = rentDate;33             this.RentUser = rentUser;34             this.YearOfService = yearsOfService;35         }36         public Vehicle(string licenseNo, string name, string color, int yearOfService, double dailyRent)37         {38             this.Color = color;39             this.DailyRent = dailyRent;40             this.LicenseNo = licenseNo;41             this.Name = name;42             this.YearOfService = yearOfService;43         }44         public Vehicle()45         {46         }47         //抽象方法,计算总金额48         public abstract double CalcPrice();49     }50 }
Vehicle类(父类)

 

转载于:https://www.cnblogs.com/lzx666/p/6706682.html

你可能感兴趣的文章
Bit Twiddling Hacks
查看>>
LeetCode : Reverse Vowels of a String
查看>>
时间戳与日期的相互转换
查看>>
jmeter(五)创建web测试计划
查看>>
python基本数据类型
查看>>
1305: [CQOI2009]dance跳舞 - BZOJ
查看>>
关于TDD的思考
查看>>
Cocos2d-x学习之windows 7 android环境搭建
查看>>
将html代码中的大写标签转换成小写标签
查看>>
jmeter多线程组间的参数传递
查看>>
零散笔记
查看>>
MaiN
查看>>
[Python学习] 简单网络爬虫抓取博客文章及思想介绍
查看>>
触发器课程SQL Server 知识梳理九 触发器的使用
查看>>
信息浏览器从Android的浏览器中传递cookie数据到App中信息浏览器
查看>>
客户端连接linux虚拟机集群报错
查看>>
linux下部署一个JavaEE项目的简单步骤
查看>>
hash储存机制
查看>>
[Android学习系列16]Android把php输出的json加载到listview
查看>>
20145205 《信息安全系统设计基础》第14周学习总结
查看>>