Java复习2

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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //可租用汽车
        public Dictionary<string, Vehicle> vehicles;
        //已租用汽车
        public Dictionary<string, Vehicle> rentvehicles;

        private void Form1_Load(object sender, EventArgs e)
        {
            txtCapacity.Enabled = false;
            //实例化可租借车辆的字典
            vehicles = new Dictionary<string, Vehicle>();
            //实例化一个Car对象
            Car car = new Car("京R00000", "奥迪A6", "黑色", 3, 240);
            Truck truck = new Truck("京D11111", "东风", "蓝色", 3, 240, 20);
            //向可租借字典add添加方法放入键名和对象(对象.车牌号,对象)
            vehicles.Add(car.LicenseNO,car);
            vehicles.Add(truck.LicenseNO,truck);

            rentvehicles=new Dictionary<string, Vehicle>();
            Car rentCar = new Car("沪R00000", "宝马318", "白色", 3, 250);
            //将文本框内的租借者姓名填入Car和Truck中
            rentCar.RenUser = this.txtRenter.Text; ;
            Truck rentTruck = new Truck("粤D11111", "东风", "蓝色", 3, 240, 20);
            rentTruck.RenUser = this.txtRenter.Text; ;
            //向已租借字典add添加方法放入键名和对象(对象.车牌号,对象)
            rentvehicles.Add(rentCar.LicenseNO, rentCar);
            rentvehicles.Add(rentTruck.LicenseNO, rentTruck);

           
        }
                            //(可租借的车辆字典,创建的listview)
        public void Showlist(Dictionary<string, Vehicle> vehicles,ListView listView) 
        {
            //清理list中的数据
            lvRent.Items.Clear();
            //判断字典里是否有数据
            if (vehicles.Count==0)
            {
                MessageBox.Show("无数据!");
                return;
            }
            //foreach循环向list输入数据
            foreach (Vehicle vehicle in vehicles.Values) 
            {
                //创建一个listviewitem对象,(可借车辆字典的车牌号)
                ListViewItem list=new ListViewItem(vehicle.LicenseNO);
                //用if判断继承的子类类型
                if (vehicle is Car) 
                {
                    //用string数组向subItems中填充数据
                    list.SubItems.AddRange(new string[] { vehicle.Name, vehicle.Color, vehicle.YearOfService.ToString(),
                    vehicle.DailyRent.ToString(), "无" }) ;
                }
                if (vehicle is Truck)
                {
                    list.SubItems.AddRange(new string[] { vehicle.Name, vehicle.Color, vehicle.YearOfService.ToString(),
                    vehicle.DailyRent.ToString(), ((Truck)vehicle).Load.ToString() });
                }
                //最后用创建的listviewitem对象放入到listview添加方法中
                listView.Items.Add(list);
            }
        }

        private void btnQueryRent_Click(object sender, EventArgs e)
        {
            //执行刷新方法(可借车辆字典,租界车辆list)
            Showlist(vehicles,lvRent);
        }

        private void btnRent_Click(object sender, EventArgs e)
        {
            //创建一个key空值
            string key = null;
            //验证非空
            if (string.IsNullOrEmpty(txtRenter.Text)) 
            {
                MessageBox.Show("请先输入租借者姓名!");
                return;
            }
            if (lvRent.SelectedItems.Count == 0) 
            {
                MessageBox.Show("请先选择一辆车!");
                return;
            }
            //获得Key值 key=list中选中那一行的所有集合的第一列,也就是车牌号
            key=lvRent.SelectedItems[0].Text;
            //加入以租借字典中 (可借字典[车牌号].车牌号,可借字典.[车牌号])
            rentvehicles.Add(vehicles[key].LicenseNO, vehicles[key]);
            //通过键名判断可借字典中是否存在
            if (vehicles.ContainsKey(key))
            {
                //通过键名删除可借字典中的数据
                vehicles.Remove(key);
            }
            //调用刷新方法
            Showlist(vehicles,lvRent);
            MessageBox.Show("租借成功!");

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnQueryReturn_Click(object sender, EventArgs e)
        {

            //清除换车list中的数据
            lvReturn.Items.Clear();
            //调用刷新方法
            Showlist(rentvehicles,lvReturn);
        }
        /// <summary>
        /// 还车方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCompute_Click(object sender, EventArgs e)
        {
            //double sumprice = 0;
            //创建一个key空值
            string key = null;
            //验证非空
            if (string.IsNullOrEmpty(txtRentDate.Text))
            {
                MessageBox.Show("请先输入租用天数!");
                return;
            }
            if (lvReturn.SelectedItems.Count == 0) 
            {
                MessageBox.Show("请先选择一项!");
                return;
            }
            //获得Key值 key=list中选中那一行的所有集合的第一列,也就是车牌号
            key = lvReturn.SelectedItems[0].Text;
            //将文本框中的租借天数放入已借字典中
            rentvehicles[key].RenDate=int.Parse(txtRentDate.Text);
            //sumprice = rentvehicles[key].CarPrice();
            MessageBox.Show("您的总价为:"+ rentvehicles[key].CarPrice());
            加入可租借字典中 (可借字典[车牌号].车牌号,可借字典.[车牌号])
            vehicles.Add(rentvehicles[key].LicenseNO, rentvehicles[key]);
            //通过键名判断已借字典中是否存在
            if (rentvehicles.ContainsKey(key))
            {       
                //删除已租借字典(可借字典.ContainsKey(车牌号))
                rentvehicles.Remove(key);
            }
            //调用刷新方法
            Showlist(rentvehicles, lvReturn);

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //验证非空
            if (!CheckInput())
            {
                return;
            }
            //将文本框中输入的数据全部存储起来
            string licenseNO = txtAutoNum.Text;
            string name = txtName.Text;
            string color = cobColor.Text;
            int yearsOfService = int.Parse(txtYears.Text);
            double dailyRent = Convert.ToDouble(txtLetting.Text);
            string type = null;
            int load = 0;
            try
            {
                //通过单选框判断子类类型
                if (rdoCar.Checked)
                {
                    //给type赋值
                    type = "car";
                }
                if (rdoTruck.Checked)
                {
                    type = "truck";
                    //当子类类型为卡车是给load(重量赋值)
                    load = int.Parse(txtCapacity.Text);
                }
                //实例化一个Vehicle类的对象,将上面获取的数据放入到方法里;
                Vehicle ve = Make.MakeVehicle(licenseNO, name, color, yearsOfService, dailyRent, load, type);
                //将对象放入到添加方法中
                vehicles.Add(ve.LicenseNO, ve);
                MessageBox.Show("添加成功!");
        }
            catch (Exception ex)
            {
                MessageBox.Show("数据有异常:" + ex.Message);
                return;
            }
            finally 
            {
             
                txtAutoNum.Text = licenseNO;
                txtName.Text = null;
                cobColor.Text = null;
                txtYears.Text = null;
                txtLetting.Text = null;
                txtCapacity.Text = null;


            }

        }

        public bool CheckInput() 
        {
            if (txtAutoNum.Text.Equals(string.Empty))
            {
                MessageBox.Show("请输入车牌号!");
                return false;
            }
            if (cobColor.Text.Equals(string.Empty))
            {
                MessageBox.Show("请输入颜色!");
                return false;
            }
            if (txtYears.Text.Equals(string.Empty))
            {
                MessageBox.Show("请输入使用时间!");
                return false;
            }
            if (txtLetting.Text.Equals(string.Empty))
            {
                MessageBox.Show("请输入每日租金!");
                return false;
            }
            if (txtCapacity.Text.Equals(string.Empty))
            {
                MessageBox.Show("请输入车牌号!");
                return false;
            }
            if (txtName.Text.Equals(string.Empty))
            {
                MessageBox.Show("请输入车型!");
                return false;
            }
            return true;

        }
        private void rdoCar_CheckedChanged(object sender, EventArgs e)
        {
            txtCapacity.Enabled = false;
        }

        private void rdoTruck_CheckedChanged(object sender, EventArgs e)
        {
            txtCapacity.Enabled = true;
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值