1. 程式人生 > >C#機房重構之上下機

C#機房重構之上下機

上機

上機的難點在於對流程的劃分是否細緻,以及程式碼順序是否安排合理。這裡附上一張上機流程圖。

 

 

 

下機

下機的難點在金額計算以及靈活運用表的增刪改查。 關於運算,小編沒有設定準備時間,一是因為這樣計算簡單了一些,二是我們每個人都要對自己的選擇負責,即使上機不滿1小時,也要扣你4塊錢。這樣既讓顧客理性登入該系統,又給管理員減少了不必要的麻煩。

 

 

U層程式碼

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace UI
{
    public partial class frmSignOut : Form
    {
        public frmSignOut()
        {
            InitializeComponent();
        }

        //應用單例模式
        private static frmSignOut fmsignout;
        public static frmSignOut GetInstance()
        {
            if (fmsignout == null || fmsignout.IsDisposed)
            {
                fmsignout = new frmSignOut();
            }
            return fmsignout;
        }

        private void frmSignOut_Load(object sender, EventArgs e)
        {


            //例項化Online表用於顯示上機資訊的list
             Enitity.OnlineEnitity online = new Enitity.OnlineEnitity();
            Facade.SignOutFacade facade = new Facade.SignOutFacade();
            online.userID = LoginUI.userID;
            List<Enitity.OnlineEnitity> list = new List<Enitity.OnlineEnitity>();
            list = facade.FacadeSignOut(online);


            //顯示上機資訊
            txtUserID.Text = Convert.ToString(list[0].userID);
            txtLoginTime.Text = Convert.ToString(list[0].loginTime);
            txtNowTime.Text = Convert.ToString(DateTime.Now);   
            TimeSpan ts = DateTime.Now - list[0].loginTime;     //獲取DateTime型別的上機時長
            
            string str = ts.ToString();
            int start = 0, length = 8;      //切割DateTime型別上機時長的小數部分

            txtOnlineTime.Text = str.Substring(start, length);  //顯示只有時,分,秒的上機時長
            txtBalance.Text = Convert.ToString(list[0].balance);    //顯示餘額


            //例項化出BasicData表,用來呼叫基本資訊         
            Enitity.BasicDataEnitity basicdata = new Enitity.BasicDataEnitity();
            Facade.SignOutFacade fBasicData = new Facade.SignOutFacade();
            List<Enitity.BasicDataEnitity> basicdatalist = new List<Enitity.BasicDataEnitity>();
            basicdatalist = fBasicData.FacadeBasicData(basicdata);
            textBox1.Text = Convert.ToString(basicdatalist[0].unitRate);

        }


        private void button1_Click(object sender, EventArgs e)
        {
           //例項化出BasicData表,計算消費        
            Enitity.BasicDataEnitity basicdata = new Enitity.BasicDataEnitity();
            Facade.SignOutFacade fBasicData = new Facade.SignOutFacade();
            List<Enitity.BasicDataEnitity> basicdatalist = new List<Enitity.BasicDataEnitity>();
            basicdatalist = fBasicData.FacadeBasicData(basicdata);
            textBox1.Text = Convert.ToString(basicdatalist[0].unitRate);

            //計算消費金額

            TimeSpan ts = DateTime.Now - Convert.ToDateTime(txtLoginTime.Text); //獲取上機時長
            double ontime = Convert.ToDouble(ts.TotalHours);//轉化時間
            double a = Convert.ToDouble(ontime) + Convert.ToDouble(1);
            double b = Convert.ToDouble(textBox1.Text);
            int a1 = (int)a; int b1 = (int)b;   //取整
            double spendmoney = a * b;
            double newbalance;  
            
            //例項化檢視V_OnlineStudent表用於讀取原餘額balance,並計算balance

            Enitity.OnlineEnitity online = new Enitity.OnlineEnitity();
            Facade.SignOutFacade facade = new Facade.SignOutFacade();
            online.userID = LoginUI.userID;
            List<Enitity.OnlineEnitity> list = new List<Enitity.OnlineEnitity>();
            list = facade.FacadeSignOut(online);

            double c = Convert.ToDouble(list[0].balance);
            int c1 = (int)c;
            int s1 = (int)spendmoney;
            newbalance = c1 - s1; //計算出下機之後的餘額
            int nbalance = (int)newbalance; //把小數型的餘額轉換成int型別,目的是取整


            //例項化更新學生表,更新新餘額
            Enitity.StudentEnitity updatebalance = new Enitity.StudentEnitity();
            Facade.SignOutFacade balancefacade = new Facade.SignOutFacade();
            updatebalance.userID = LoginUI.userID;
            updatebalance.balance = Convert.ToString(nbalance);         //      更新取整後的餘額到表中
            List<Enitity.StudentEnitity> studentlist = new List<Enitity.StudentEnitity>();
            studentlist = balancefacade.FacadeStudent(updatebalance);

            MessageBox.Show("下機成功!");
            

            //例項化更新Online表,更新Online表使用者的下機時間和下機狀態。       
            Enitity.OnlineEnitity addonline = new Enitity.OnlineEnitity();
            Facade.SignOutFacade addfacade = new Facade.SignOutFacade();
            addonline.userID = LoginUI.userID;
            addonline.status = "下機";
            addonline.logoutTime = DateTime.Now.ToLocalTime();
            List<Enitity.OnlineEnitity> addlist = new List<Enitity.OnlineEnitity>();
            addlist = addfacade.FacadeAddOnline(addonline);


            //例項化查詢Online表,用於把Online表到使用者資訊賦給Line表
            Enitity.OnlineEnitity selectonline = new Enitity.OnlineEnitity();
            Facade.SignOutFacade selectfacade = new Facade.SignOutFacade();
            selectonline.userID = LoginUI.userID;
            List<Enitity.OnlineEnitity> selectlist = new List<Enitity.OnlineEnitity>();
            selectlist = selectfacade.FacadeSelectOnline(selectonline);



            //例項化出Line表資訊,用來更新記錄
            Enitity.LineEnitity line = new Enitity.LineEnitity();
            Facade.SignOutFacade fLine = new Facade.SignOutFacade();
            line.userID = selectlist[0].userID;
            line.loginTime = selectlist[0].loginTime;
            line.logoutTime = selectlist[0].logoutTime;
            line.computer = selectlist[0].computer;
            line.status = selectlist[0].status;
            int linelist;
            linelist = fLine.FacadeLine(line);




            //例項化用於後面刪除使用者上機表資訊的deletelist
            Enitity.OnlineEnitity deleteonline = new Enitity.OnlineEnitity();
            Facade.SignOutFacade deletefacade = new Facade.SignOutFacade();
            deleteonline.userID = LoginUI.userID;
            List<Enitity.OnlineEnitity> deletelist = new List<Enitity.OnlineEnitity>();
            deletelist = deletefacade.FacadeDeleteOnline(deleteonline);



            //清空文字框
            foreach (Control ctr in Controls)   //遍歷控制元件
            {
                if (ctr is TextBox)    //找到TextBox控制元件和ComboBox控制元件
                {
                    ((TextBox)ctr).Text = string.Empty;  //清空TextBox控制元件的文字內容                               
                }
            }

            this.Close();   //關閉當前窗體
            Application.OpenForms["frmMain"].Close();   //關閉主窗體
            System.Environment.Exit(0);     //關閉所有窗體

        }
    }
}

 

窗體對照