1. 程式人生 > >C#機房重構之充值

C#機房重構之充值

充值思路:查詢可以充值的卡號,進行寵值,插入充值記錄到衝指表,更新學生餘額
建立順序
D層—IDAL層—Factory層—B層—Facade層—UI層
D層
RechargeDAL

public class RechargeDAL:IDAL.RechargeIDAL
{
   public int recharge(Entity.RechargeInfo RechargeInfo)//插入充值記錄
   {
       SQLHelper sqlHelper = new SQLHelper();
       SqlParameter[] sqlParams = { new SqlParameter("@CardNo", RechargeInfo .CardNo ),
                                    new SqlParameter ("@AddMoney",RechargeInfo .AddMoney),
                                    new SqlParameter ("@Date",RechargeInfo .Date ),
                                    new SqlParameter ("@Time",RechargeInfo.Time ),
                                    new SqlParameter ("@UserID",RechargeInfo .UserID ) ,
                                    new SqlParameter ("@Check",RechargeInfo.Check ) };
       string sql = @"INSERT INTO [Recharge] (CardNo,AddMoney,Date,Time,UserID,[Check])values(@CardNo,@AddMoney,@Date,@Time,@UserID,@Check)";
       int rechargecard = sqlHelper.ExtcuteNonQuery (sql,sqlParams , CommandType.Text );
       return rechargecard;
            
        }
}

StudentDAL

public class StudentDAL:IDAL.StudentIDAL
{
	public List<Entity.StudentInfo> getAllStudentInfo(Entity .StudentInfo StudentInfo)//得到所有使用卡的學生的資訊
	{
    	SQLHelper sqlHelper = new SQLHelper();
    	SqlParameter[] sqlParams = { new SqlParameter("@State", StudentInfo.State ) };
    	//構造要查詢的SQL語句
    	string sql = @"SELECT * FROM [Student] Where State='使用'";
    	//使用SQLhelper進行查詢得到結果
    	DataTable table = sqlHelper.ExecuteQuery (sql,sqlParams ,CommandType.Text );
    	//將table中的資料轉存到list中去
    	ConvertHelper convert = new ConvertHelper();
    	return convert.convertToList<StudentInfo>(table);
	}
	public int updateStudentCash(Entity.StudentInfo StudentInfo)//更新學生餘額
	{
    	SQLHelper sqlHelper = new SQLHelper();
    	SqlParameter[] sqlParams = { new SqlParameter("@CardNo", StudentInfo.CardNo),
                                 						new SqlParameter ("@Cash",StudentInfo .Cash )};
    	string sql = @"UPDATE [Student] SET 
[email protected]
WHERE [email protected]"; int StudentCash = sqlHelper.ExtcuteNonQuery(sql, sqlParams, CommandType.Text); return StudentCash; } }

IDA層
RechargeIDAL

public  interface  RechargeIDAL
{
	int recharge(Entity.RechargeInfo RechargeInfo);//插入充值記錄介面
}

StudentIDAL

public  interface  StudentIDAL
{
	List<Entity.StudentInfo> getAllStudentInfo(Entity.StudentInfo studentInfo);//獲得所有學生資訊介面
	int updateStudentCash(Entity.StudentInfo studentInfo);//實現更新學生餘額介面
}

Factory層
RechargeFactory

public  class RechargeFactory
{
	string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];//接收來自配置檔案的資料
	 public IDAL.RechargeIDAL InsertRechargeInfo()//插入充值記錄
     {
         string ClassName = StrDB + "." + "RechargeDAL";//DAL層的類名
         return (IDAL.RechargeIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);//反射工廠的應用
     }
}

StudentFactory

public class StudentFactory
{
	string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];//接收來自配置檔案的資料	
	public IDAL.StudentIDAL GetStudent()//獲取學生資訊
	{
		string ClassName = StrDB + "." + "StudentDAL";//DAL層的類名
        return (IDAL.StudentIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);//反射工廠的應用
	}
	public IDAL.StudentIDAL UpdateStudentCash()//更新學生餘額
	{
		string ClassName = StrDB + "." + "StudentDAL";//DAL層的類名
        return (IDAL.StudentIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);//反射工廠的應用
	}
}

BLL層
RechargeBLL

public  class RechargeBLL
{
	public int InsertRechargeRecord(Entity.RechargeInfo RechargeInfo)//插入充值記錄
	{
    	//呼叫工廠方法建立InsertRechargeInfo介面
    	Factory.RechargeFactory fact = new Factory.RechargeFactory();
    	//呼叫插入的方法
    	IDAL.RechargeIDAL idal = fact.InsertRechargeInfo();
    	int rechargecard = idal.recharge(RechargeInfo);
    	return rechargecard;
	}
}

StudentBLL層

public  class StudentBLL
{
	public List<Entity.StudentInfo> GetALLStudentinfo(Entity.StudentInfo StudentInfo)//查詢所有學生資訊
	{
      	Factory.StudentFactory fact = new Factory.StudentFactory();//例項化工廠
      	IDAL.StudentIDAL idal = fact.GetStudent ();
      	List<Entity.StudentInfo> list = idal.getAllStudentInfo(StudentInfo);
      	return list;
 	}
}

Facade層
RechargeFacade

public  class RechargeFacade
{
	public int InsertRechargeInfo(Entity.RechargeInfo rechargeInfo )//插入充值記錄
	{
		BLL.RechargeBLL rechargeBLL = new BLL.RechargeBLL();
    	int rechargecard = rechargeBLL.InsertRechargeRecord(rechargeInfo );
    	return rechargecard ;
	}
}

StudentFacade

public class StudentFacade
{
	//獲取所有學生資訊
    public List<Entity.StudentInfo> GetAllStudentInfo(Entity.StudentInfo student)
    {
        BLL.StudentBLL studentBLL = new BLL.StudentBLL();
        List<Entity.StudentInfo> list =studentBLL.GetALLStudentinfo (student);
        return list;
    }
     //更新學生餘額
    public int UpdateStudentCash(Entity.StudentInfo student)
    {
        BLL.StudentBLL studentBLL = new BLL.StudentBLL();
        return studentBLL.UpdateStudentCash(student);
     }
}

UI層

//加載出所有可以充值的卡號
        private void frmRecharge_Load(object sender, EventArgs e)
        {
            Entity.StudentInfo student = new Entity.StudentInfo();//例項化實體
            Facade.StudentFacade studentFacade = new Facade.StudentFacade();
            student.State = "使用";
            List<Entity.StudentInfo> list = studentFacade.GetAllStudentInfo(student);//得到所有使用卡的學生資訊
            list = studentFacade.GetAllStudentInfo(student);

            if (list.Count == 0)
            {
                MessageBox.Show("無可充值的學生", "溫馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                for (int i = 0; i < list.Count; i++)
                {
                    comboCardNo.Items.Add(list[i].CardNo);//載入學號
                }
            }
            //載入充值的數值
            for (int i = 10; i < 100; i += 10)
            {
                comboAddMoney.Items.Add(i);
            }
            for (int i = 100; i < 500; i += 100)
            {
                comboAddMoney.Items.Add(i);
            }
        }
        //充值操作
        private void btnrecharge_Click(object sender, EventArgs e)
        {
            Entity.RechargeInfo rechargeInfo = new Entity.RechargeInfo();//例項化充值層
            Facade.RechargeFacade faca = new Facade.RechargeFacade();//例項化外觀層
            //定義需要插入到充值表當中的資料
            rechargeInfo.CardNo = Convert.ToInt32(comboCardNo.Text.Trim());
            rechargeInfo.AddMoney = Convert.ToDecimal(comboAddMoney.Text.Trim());
            rechargeInfo.Check = "未結賬";
            rechargeInfo.UserID = Convert.ToInt32(frmLogin.UserID);
            rechargeInfo.Date = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            rechargeInfo.Time = Convert.ToDateTime(DateTime.Now.ToShortTimeString());


            Entity.StudentInfo student = new Entity.StudentInfo();//例項化學生
            Facade.StudentFacade facade = new Facade.StudentFacade(); //例項化學生外觀
            student.CardNo = Convert.ToInt32(comboCardNo.Text.Trim());
            student.State = "使用";
            DataTable table = facade.GetStudent(student);
            student.Cash = Convert.ToDecimal(table.Rows[0][8].ToString()) + Convert.ToDecimal(comboAddMoney.Text.Trim());


            int rechargecard = faca.InsertRechargeInfo(rechargeInfo);//呼叫插入充值記錄方法
            int updatestudentcash = facade.UpdateStudentCash(student);//呼叫更新學生餘額的方法
            //顯示充值資訊
            label3.Text = "充值卡號:" + comboCardNo.Text + System.Environment.NewLine
                         + "原金額:" + table.Rows[0][9].ToString() + System.Environment.NewLine
                         + "充值金額:" + comboAddMoney.Text + System.Environment.NewLine
                         + "現有金額:" + student.Cash + System.Environment.NewLine
                         + "充值日期:" + DateTime.Now.ToShortDateString() + System.Environment.NewLine
                         + "充值時間:" + DateTime.Now.ToShortTimeString() + System.Environment.NewLine
                         + "充值教師:" + frmLogin.UserID;
            MessageBox.Show("充值成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }