当时我们小组的UI设计师文彬一直强调面向对象,总是把"应用面向对象思维去实现机房收费系统"挂在嘴边,所以,我们合作版的机房收费系统,三个级别的用户:一般用户、操作员、管理员各有他们自己的主窗体,,,
一般用户,也就是学生的窗体,是这个样子的
相比之前的设计理念,
我们现在的系统更像是"一种网吧运营模式的学校机房收费系统",这是我起的名
现在咱们来说如何计算消费时间
过去是这么实现的:上下机都会获取当前时间,并展示在界面上,调用封装的函数DataDiff就可以求得
可是现在,虽然也有封装好的函数,但上下机时间的获取要么查询数据库,要么存入全局变量,而且计算起来也是很费劲的
正苦恼的时候,灵光一显,相减不容易做到,那就让它递增吧
于是乎,这个窗体文本框里的数据是一直在变化的,并设置每秒更新存入一次数据库
贴一下代码,分享给大家
窗体加载代码
# region 窗体加载代码 private void frm_GU_Main_Load(object sender, EventArgs e) { txtConsumedCash.Text = "0"; txtConsumedLength.Text = "0"; GetLineInfo(); } #endregion #region 获取上机信息 private void GetLineInfo() { //获取收费标准 Facade.QueryBasicData fquerybd = new Facade.QueryBasicData(); List<basicdataEntity> basicData = new List<basicdataEntity>(); basicData = fquerybd.QueryBasicDataInfo(); string regRate = basicData[0].regRate; string tmpRate = basicData[0].temRate; string unitTime = basicData[0].unitTime; string limitTime = basicData[0].limitTime; string prepareTime = basicData[0].prepareTime; string limitCash = basicData[0].limitCash; //一般用户登录成功即上机成功 lblCurrentUser.Text = UI.Common.CommonClass.CurrentUserId.ToString().Trim(); Entity.cardEntity cardInfo = new Entity.cardEntity(); cardInfo.cardID = UI.Common.CommonClass.CurrentUserId; //获取卡的类型和余额 Facade.LoginFa logfa = new Facade.LoginFa(); List<cardEntity> cardIn = new List<cardEntity>(); cardIn = logfa.GetCardBalance(cardInfo); string cardtype = cardIn[0].cardType.ToString(); //int balanceleft = int.Parse(cardIn[0].leftCash); string balanceleft = cardIn[0].leftCash; //计算剩余时长=余额匹配的时间+准备时间+最少上机时间 //先来计算余额匹配的时间 if (cardtype == "固定") { int matchTime = int.Parse(balanceleft) / int.Parse(regRate) * 60; int allTime = matchTime + int.Parse(limitTime) + int.Parse(prepareTime); txtRestLength.Text = allTime.ToString();//剩余时长 } else { int matchTime = int.Parse(balanceleft) / int.Parse(tmpRate) * 60; int allTime = matchTime + int.Parse(limitTime) + int.Parse(prepareTime); txtRestLength.Text = allTime.ToString();//剩余时长 } txtConsumedCash.Text = "0"; txtConsumedLength.Text = "0"; txtBalance.Text = balanceleft.ToString(); //存入数据库上机信息 Entity.onLineEntity onLineInfo = new onLineEntity(); onLineInfo.cardID = UI.Common.CommonClass.CurrentUserId; onLineInfo.loginDate = DateTime.Now.ToString("yyyy-MM-dd"); onLineInfo.loginTime = DateTime.Now.ToString("HH:mm:ss"); onLineInfo.IsOnLine = "正在上机"; onLineInfo.pcNo = Dns.GetHostName(); Facade.LoginFa logGfa = new Facade.LoginFa(); logGfa.GoLineFa(onLineInfo);//调用外观层上机方法 } #endregion
每秒更新代码
#region 每一秒更新一次页面显示,并把页面结果写入数据库 private void timRefresh_Tick(object sender, EventArgs e) { //页面只读 txtBalance.ReadOnly = true; txtConsumedCash.ReadOnly = true; txtConsumedLength.ReadOnly = true; txtRestLength.ReadOnly = true; //消费的时长 txtConsumedLength.Text = (int.Parse(txtConsumedLength.Text.ToString().Trim()) + 1).ToString(); //剩余的时长 txtRestLength.Text = (int.Parse(txtRestLength.Text.ToString().Trim()) - 1).ToString(); //获取收费标准 Facade.QueryBasicData fquerybd = new Facade.QueryBasicData(); List<basicdataEntity> basicData = new List<basicdataEntity>(); basicData = fquerybd.QueryBasicDataInfo(); string regRate = basicData[0].regRate; string tmpRate = basicData[0].temRate; string unitTime = basicData[0].unitTime; string limitTime = basicData[0].limitTime; string prepareTime = basicData[0].prepareTime; string limitCash = basicData[0].limitCash; //把消费信息存到下面这些变量里,写入数据库-韩林-2016年3月17日10:22:34 double costMon;//花费的总金额 string costT = null;//花费总时间 string moneyLeft = null;//余额 string Cid = UI.Common.CommonClass.CurrentUserId.Trim();//卡号 //计算消费的金额,分两种情况 //第一种,消费时间小于准备时间+最少上机时间 if (int.Parse(txtConsumedLength.Text.ToString().Trim()) <= (int.Parse(limitTime) + int.Parse(prepareTime))) { costMon = 0; costT = txtConsumedLength.Text.Trim(); moneyLeft = txtBalance.Text.Trim(); } //第二种,消费时间超过规定时间 else { //上机总花费时间 int costTime = int.Parse(txtConsumedLength.Text.ToString().Trim()) - int.Parse(limitTime) - int.Parse(prepareTime); costT = costTime.ToString().Trim(); //计算共有几个收费单位时间段 int numberCost = Convert.ToInt32(Math.Ceiling((double)costTime / (double)int.Parse(unitTime))); //获取卡的类型和余额 Facade.LoginFa logfa = new Facade.LoginFa(); List<cardEntity> cardIn = new List<cardEntity>(); Entity.cardEntity cardInfo = new Entity.cardEntity(); cardInfo.cardID = UI.Common.CommonClass.CurrentUserId; cardIn = logfa.GetCardBalance(cardInfo); //卡的类型 string cardtype = cardIn[0].cardType.ToString().Trim(); //卡的余额 string balanceleft = cardIn[0].leftCash; //固定用户单位时间花费 double regUserCost = double.Parse(regRate) / double.Parse("60") * double.Parse(unitTime); //临时用户单位时间花费 double tmpUserCost = double.Parse(tmpRate) / double.Parse("60") * double.Parse(unitTime); if (cardtype == "固定") { //注意老外的四舍六入和中国式的四舍五入 double costMoney = Math.Round(regUserCost * numberCost, 1, MidpointRounding.AwayFromZero); costMon = costMoney; } else { double costMoney = Math.Round(tmpUserCost * numberCost, 1, MidpointRounding.AwayFromZero); costMon = costMoney; } txtConsumedCash.Text = costMon.ToString().Trim(); //有一点需要注意,卡内余额只能在消费金额变化时才能变,不能每秒都减去消费金额 //所以不能用下面这句话 //txtBalance.Text = (Math.Round(double.Parse(balanceleft), 1, MidpointRounding.AwayFromZero) - costMon).ToString().Trim(); moneyLeft = txtBalance.Text.Trim(); } //开始写入数据库-韩林-2016年3月17日10:30:16 Entity.cardEntity enC = new cardEntity(); Entity.onLineEntity enL = new onLineEntity(); enC.cardID = Cid; enC.leftCash = moneyLeft; enL.conCash = costMon.ToString().Trim(); enL.conTime = costT; Facade.LoginFa updateFa = new Facade.LoginFa(); bool flag = updateFa.updateCL(enC, enL); if (flag == false) { MessageBox.Show("消费信息更新失败!", "提示"); return; } } #endregion
消费金额文本框值变化时的代码
private void txtConsumedCash_TextChanged(object sender, EventArgs e) { //获取卡的余额和类型 Facade.LoginFa logfa = new Facade.LoginFa(); List<cardEntity> cardIn = new List<cardEntity>(); Entity.cardEntity cardInfo = new Entity.cardEntity(); cardInfo.cardID = UI.Common.CommonClass.CurrentUserId; cardIn = logfa.GetCardBalance(cardInfo); //卡的余额 string balanceleft = cardIn[0].leftCash; //卡的类型 string cardtype = cardIn[0].cardType.ToString().Trim(); //获取基本收费标准 Facade.QueryBasicData fquerybd = new Facade.QueryBasicData(); List<basicdataEntity> basicData = new List<basicdataEntity>(); basicData = fquerybd.QueryBasicDataInfo(); string regRate = basicData[0].regRate; string tmpRate = basicData[0].temRate; string unitTime = basicData[0].unitTime; string limitTime = basicData[0].limitTime; string prepareTime = basicData[0].prepareTime; string limitCash = basicData[0].limitCash; //固定用户单位时间花费 double regUserCost = double.Parse(regRate) / double.Parse("60") * double.Parse(unitTime); //临时用户单位时间花费 double tmpUserCost = double.Parse(tmpRate) / double.Parse("60") * double.Parse(unitTime); if (cardtype == "固定") { txtBalance.Text = (Math.Round(double.Parse(balanceleft), 1, MidpointRounding.AwayFromZero) - regUserCost).ToString().Trim(); } else { txtBalance.Text = (Math.Round(double.Parse(balanceleft), 1, MidpointRounding.AwayFromZero) - tmpUserCost).ToString().Trim(); } }
所以,一条路不好走的时候,要及时换一条路
做软件要做活,做人也要灵活
人有多大胆,地有多大产,我不怕千万人阻挡,只怕自己投降,迈开步子去做,生活总会给你惊喜