【C#重構】——充值
阿新 • • 發佈:2018-11-17
教師端功能:實現對學生卡號的充值
用到的表:student_info 查詢相應卡號學生資訊;recharge_info充值記錄表
recharge_info充值記錄表
UI
public partial class frmTRecharge : Form { //窗體預設 public frmTRecharge() { InitializeComponent(); } //點選註冊 private void btnRecharge_Click(object sender, EventArgs e) { if (txtCardNO.Text.Trim ()=="" || txtRecharge.Text.Trim()=="") { MessageBox.Show ("請把資訊填寫完整"); } //例項化實體層的studnetinfo類和rechargeinfo類 Entity.StudentInfo student = new Entity.StudentInfo(); Entity.RechargeInfo money = new Entity.RechargeInfo(); money.UserID = Convert.ToInt32(txtCardNO.Text.Trim()); money.Addmoney = Convert.ToInt32(txtRecharge.Text.Trim()); student.UserID = money.UserID; DataTable table; //查詢student表的UserID DataTable table1; //DataTable table2; //例項化外觀層 Facade.RechargeFacade facade = new Facade.RechargeFacade(); //外觀層的查詢UserID table1 = facade.SelectStudentFacade(student); //判斷查詢到使用者輸入的UserID是否在使用狀態 if (table1.Rows[0][7].ToString().Trim()=="正使用") { //如果卡號的狀態是正在使用的haul,那就找到status和balance返回回來 //cash 暫存一個變數等待和充值的前進行加和,狀態寫在客戶端窗體上 //存在問題:如果這個卡號涉及到是否結賬,還需要增加什麼內容? //如果這個卡號已經登出了怎麼進行判斷? if (table1.Rows[0][4].ToString()!="") { //查到的狀態賦值到recharge表的status列 student.Status=table1.Rows[0][4].ToString(); //查到的學生餘額暫時存放在balance中 student.Balance= Convert.ToInt32(table1.Rows[0][2].ToString()); int a = student.Balance; int b = money.Addmoney; //增加表資訊內容 money.Status = student.Status; table = facade.InsertRechargeFacade(money); int c = a + b; student.Balance = c; //table2 = facade.UpdateStudentFacade(student); //更新student表之後得到的new balance 和固定還是臨時使用者 顯示在在窗體上 txtBalance.Text = Convert.ToString(student.Balance); cmbState.Text = student.Status; MessageBox.Show("充值成功"); } else { MessageBox.Show("卡號不存在"); } } else { if (table1.Rows[0][7].ToString().Trim() == "已經登出") { MessageBox.Show("卡號已經登出"); } } } }
D層
public class RechargeDAL:IDAL.IRechargeIDL { /// <summary> /// 查詢學生表的初始註冊錢+學生註冊狀態 /// </summary> /// <param name="student"></param> /// <returns></returns> public DataTable SelectStudent(Entity.StudentInfo student) { SQLHelper sqlHelper = new SQLHelper(); SqlParameter[] sqlParams = { new SqlParameter("@UserID", student.UserID) }; String sql = "Select * from student_info where userID
[email protected] "; DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text); return table; } public DataTable InsertRecharge(Entity.RechargeInfo money) { DateTime date = DateTime.Now; SQLHelper sqlHelper = new SQLHelper(); SqlParameter[] sqlParams = { new SqlParameter("@CardNO",money.UserID), new SqlParameter("@Date",date.ToShortDateString()), new SqlParameter("@Status",money.Status), new SqlParameter("@Addmoney",money.Addmoney)}; String sql = "Insert into recharge_info(userID,addmoney,date,status) Values(@CardNO,@Addmoney,@Date,@Status)"; DataTable table = sqlHelper.ExecuteNonQuery(sql,sqlParams,CommandType.Text); return table; }
IDAL層
public interface IRechargeIDL
{
//查詢student表,目的返回balance,state,status
DataTable SelectStudent(Entity.StudentInfo student);
//增加資訊(卡號 status addmoney)到recharge表,
DataTable InsertRecharge(Entity.RechargeInfo money);
//更新student表的balance
//DataTable UpdateStudent(Entity.StudentInfo student);
}
顆粒歸倉,繼續往後走,,,