⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backform.cs

📁 C#通过读取.txt文档进行运行的小系统
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;




namespace BikeRentSystem
{
    public partial class BackForm : Form
    {
        //存储客户信息
        private  RecordClass renter = new RecordClass();

        //用于存储管理员信息
       private  RentForm owner = new RentForm();
        private OwnerInforClass ownerInforInBack = new OwnerInforClass();




        //用于读取文件
        private BinaryFormatter reader = new BinaryFormatter();
        private FileStream input;             


        private void ReadRenterInfor(string fileName)
        {    

            input = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            renter = (RecordClass)reader.Deserialize(input);

            input.Close();
        }

        public BackForm()
        {
            InitializeComponent();
        }

        private void returnButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string fileName;

        private string timeCoststr;

        private void enterButton_Click(object sender, EventArgs e)
        {

            fileName = textBox1.Text+".txt";
            try
            {
                ReadRenterInfor(fileName);
                


                //按照原始编号的形式,获取当前时间的编号,方便处理

                string nowNO = owner.GetNO();


                //存储耗费时间
                double timeCost;
                timeCost = TimeCost();

                //显示租车所用总的时间
                timeCoststr = timeCost + "";
                timeCoststr = timeCoststr.Substring(0, 3);
                timecostLabel.Text = timeCoststr + "小时";

                ownerInforInBack=owner.ReadOwnerInfor();

                
                if (renter.border == true)
                    MessageBox.Show("您一共租用的车数已达到团体租车的限度\n我们在给您正常打折的基础上再打" +
                       ownerInforInBack.discountGroup + "折", "优惠信息",
                       MessageBoxButtons.OK, MessageBoxIcon.Information);

                rentTimeLabel.Text = renter.time + "";
                backTimeLabel.Text = System.DateTime.Now + "";
                depositLabel.Text = "押金" + renter.depesit + "元";
                
                

            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("对不起,编号有误,请重新输入", "编号有误",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Text = "";

            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("您租车时间很短,这次我们就不收您的钱了", "优惠信息",
                      MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            
        }

        private void ClearLable()
        {
            //起止时间栏清空
            rentTimeLabel.Text = "";
            backTimeLabel.Text = "";

            //未还车数栏清空
            notLieTabel1.Text = "";
            notMOIULabel.Text = "";
            notRunLabel.Text = "";
            notDoubleLabel1.Text = "";
            notCollapseLabel1.Text = "";

            depositLabel.Text = "押金";
            costLabel.Text = "租金";
            changeLabel.Text = "找零";

            timecostLabel.Text = "";
        }

        private void ClearTextBox()
        {
            textBox1.Text = "";

            reLieTextBox.Text = "";
            reRunTextBox.Text="";
            reMOUTextBox.Text="";
            reCollapseTextBox.Text="";
            reDoubleTextBox.Text="";
        }

        private void BackForm_Load(object sender, EventArgs e)
        {
            ClearLable();

            
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            ClearLable();
            ClearTextBox();
            NotBackCheckBox.Checked = false;
        }

        private double TimeCost()
        {
           
            TimeSpan day=System.DateTime.Now- renter.time;
            TimeSpan hour=System.DateTime.Now- renter.time;
            TimeSpan minute=System.DateTime.Now- renter.time;

            double timeCost = Double.Parse(day.Days.ToString())*24+
                Double.Parse(day.Hours.ToString())+
                Double.Parse(day.Minutes.ToString()) / 60;  

            return timeCost;
        }

        private double totCost;
        private void CalculateCost(string timeCost)
        {
            
            double totTime = Double.Parse(timeCost);
            totCost = (renter.collapseNum * ownerInforInBack.priceCollapse +
                renter.doubleNum * ownerInforInBack.priceDouble +
                renter.lieNum * ownerInforInBack.pricrLie +
                renter.runNum * ownerInforInBack.priceRun +
                renter.mounNum * ownerInforInBack.priceMOU) * totTime;

            //打折
            if (renter.student == "checked")
                totCost *= ownerInforInBack.discountStudent;
            else if (renter.clintMen == "checked")
                totCost *= ownerInforInBack.discountClientMen;

            //
            if (renter.border == true)
                totCost *= ownerInforInBack.discountGroup;
            costLabel.Text = "租金" + totCost + "元";

            changeLabel.Text="找零"+(renter.depesit-totCost)+"元";
        }

        private void NotBackCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            NotBackCheck();
            CalculateCost(timeCoststr);
        }

        //看上个方法
        private int notBack1, notBack2,notBack3,notBack4,notBack5;

        private void NotBackCheck()
        {
            try
            {
                notLieTabel1.Text = "" + (renter.lieNum - Int32.Parse(reLieTextBox.Text));
                notMOIULabel.Text = "" + (renter.mounNum - Int32.Parse(reMOUTextBox.Text));
                notCollapseLabel1.Text = "" + (renter.collapseNum - Int32.Parse(reCollapseTextBox.Text));
                notDoubleLabel1.Text = "" + (renter.doubleNum - Int32.Parse(reDoubleTextBox.Text));
                notRunLabel.Text = "" + (renter.runNum - Int32.Parse(reRunTextBox.Text));

                if ((notBack1=renter.lieNum - Int32.Parse(reLieTextBox.Text)) >= 0)
                    if ((notBack2=renter.mounNum - Int32.Parse(reMOUTextBox.Text)) >= 0)
                        if ((notBack3=renter.collapseNum - Int32.Parse(reCollapseTextBox.Text)) >= 0)
                            if ((notBack4=renter.doubleNum - Int32.Parse(reDoubleTextBox.Text)) >= 0)
                                if ((notBack5=renter.runNum - Int32.Parse(reRunTextBox.Text)) >= 0)
                                    return;
                throw new ArgumentNullException();


            }
            catch (ArgumentNullException)
            {
                MessageBox.Show("数据格式或者大小错误", "数据有误",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (FormatException)
            {
                MessageBox.Show("还车数据格式错误", "数据有误",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        private void sureButton_Click(object sender, EventArgs e)
        {
           

            //把还车数据加到现有车辆总数上
            int[] temp={0,0,0,0,0};

            UsedClass used = new UsedClass();
            temp = used.GetUsedNum();
            try
            {
                temp[0] -= Int32.Parse(reLieTextBox.Text);
                temp[1] -= Int32.Parse(reMOUTextBox.Text);
                temp[2] -= Int32.Parse(reRunTextBox.Text);
                temp[3] -= Int32.Parse(reCollapseTextBox.Text);
                temp[4] -= Int32.Parse(reDoubleTextBox.Text);
            }

            catch (ArgumentNullException)
            {
                MessageBox.Show("还车数据不能为空", "数据有误",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            catch (FormatException)
            {
                MessageBox.Show("还车数据格式错误", "数据有误",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            used.SetUsedNum(temp);


            //删除原租车文件
            try
            {
                if (notBack1 > 0 || notBack2 > 0 || notBack3 > 0 || notBack4 > 0 || notBack5 > 0)
                    throw new ArgumentException();
                else
                {
                    File.Delete(fileName);
                }
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show("重复还车,或者换车编号有误", "数据有误",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ArgumentException)
            {
                renter.lieNum -= Int32.Parse(reLieTextBox.Text);
                renter.mounNum -= Int32.Parse(reMOUTextBox.Text);
                renter.collapseNum -= Int32.Parse(reCollapseTextBox.Text);
                renter.doubleNum-=Int32.Parse(reDoubleTextBox.Text);
                renter.runNum-=Int32.Parse(reRunTextBox.Text);

                if (renter.lieNum + renter.mounNum + renter.collapseNum + renter.doubleNum + renter.runNum > ownerInforInBack.groupLine)
                    renter.border = true;
                else
                    renter.border = false;

                BinaryFormatter formatter = new BinaryFormatter();
                FileStream output;

                output = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);

                formatter.Serialize(output, renter);

                output.Close();
            }


            //留下记录
            SaveRecord();

            MessageBox.Show("已处理相关数据,不要在按确定按钮了。\n按清空键后可以继续还车", "提示",
                       MessageBoxButtons.OK, MessageBoxIcon.Information);
            
        }

        private void SaveRecord()
        {

            FileInfo fi = new FileInfo("analysis.txt");
            using (StreamWriter sw = fi.AppendText())
            {
                sw.WriteLine(renter.NO + "     " + renter.name + "     " + renter.phone + "     "+
                    renter.ID + "     " + renter.time + "     " + System.DateTime.Now + "     "+
                    totCost + "     " + reLieTextBox.Text + "     " + reMOUTextBox.Text + "     " +
                    reRunTextBox.Text + "     " + reCollapseTextBox.Text + "     " + reDoubleTextBox.Text + "     " +
                    renter.student + "     " + renter.clintMen + "     " + renter.teacher + "     ");
                
                
                sw.Close();
            }
        }

      
        
       
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -