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

📄 buttondelegater.cs

📁 是一个模拟ATM的程序
💻 CS
字号:
// Menu.cs created with MonoDevelop// User: Estelle at 2:29 PM 5/5/2008//// To change standard headers go to Edit->Preferences->Coding->Standard Headers//using System;using System.Collections;using System.Reflection;using System.Windows.Forms;namespace ATM{	/**	 * This interface is an abstraction of all the button events.	 * It is useful for bind the delegater by using the reflection.	 * The Names of each implemented class are relatively with Label of the binding button.	 */	public interface IButtonDelegater{		void Execute(object obj,EventArgs er);	}		public delegate void DoJob(ATMForm atm);		public class GotoPage:IButtonDelegater{		public void Execute(object obj,EventArgs er){			Button btn=(Button)obj;			ATMForm atm=(ATMForm)btn.GetContainerControl();			atm.showPage(btn.Name);		}	}		public class Login:IButtonDelegater{		public void Execute(object obj,EventArgs er){			Button btn=(Button)obj;			ATMForm atm=(ATMForm)btn.GetContainerControl();			ArrayList controls=atm.getControls(ATMForm.LOGIN_PAGE);			Bank.current=(Bank)(((ComboBox)controls[1]).SelectedItem);			string bank=Bank.current.shortName.Trim();			string cardno=((TextBox)controls[3]).Text.Trim();			string newpwd=((TextBox)controls[5]).Text.Trim();			try{				if (cardno.Length==0)					throw new ATMException("The Card Number should not be empty!");								if (newpwd.Length==0)					throw new ATMException("The Password should not be empty!");								CustomerBalance cb=new CustomerBalance();				cb.cardNo=cardno;				ArrayList al=cb.getEntitiesByProperty("cardNo");				bool exist=false;				foreach(Object o in al){					cb=(CustomerBalance)o;					if (cb.cardType.Equals(bank)){						exist=true;						break;					}				}				if (!exist)	throw new ATMException("Customer Not Found!");				CustomerLogin cl=new CustomerLogin();				cl.IDNo=cb.IDNo;				cl=(CustomerLogin)cl.getEntityByID();				if (cl.password.Equals(newpwd)){					Customer c=new Customer();					c.IDNo=cl.IDNo;					c=(Customer)c.getEntityByID();					string info="Are your name is "+c.realName;					string yes=MessageBox.Show(info,"ATM",MessageBoxButtons.YesNo).ToString();					if (yes.Equals("Yes")){						atm.customer=c;						atm.updateBalance();						atm.showPage(ATMForm.OPERATION_PAGE);					}else{						atm.returnPage();					}				}else{					throw new ATMException("Password Error!");				}			}catch(ATMException ae){				MessageBox.Show(ae.Message,"ATM Error");			}		}	}		public class Submit:IButtonDelegater{		public void Execute(object obj,EventArgs er){			DoJob doJob=null;			Button btn=(Button)obj;			ATMForm atm=(ATMForm)btn.GetContainerControl();			String page=atm.getCurrentPage();			if (page.Equals(ATMForm.REGISTER_PAGE)){				doJob=new DoJob(register);			}else if (page.Equals(ATMForm.MODIFY_PASSWORD_PAGE)){				doJob=new DoJob(modifyPassword);			}			doJob(atm);		}				private void register(ATMForm atm){			ArrayList controls=atm.getControls(ATMForm.REGISTER_PAGE);			Bank.current=(Bank)(((ComboBox)controls[1]).SelectedItem);			string bank=Bank.current.shortName.Trim();			string idno=((TextBox)controls[3]).Text.Trim();			string name=((TextBox)controls[5]).Text.Trim();			string newpwd=((TextBox)controls[7]).Text.Trim();			string cfmpwd=((TextBox)controls[9]).Text.Trim();			string cardNo="";			try{				if (idno.Length==0)					throw new ATMException("The Identity Card should not be empty!");				if (name.Length==0)					throw new ATMException("The Real Name should not be empty!");				if (newpwd.Length==0)					throw new ATMException("The Password should not be empty!");				if (!newpwd.Equals(cfmpwd))					throw new ATMException("The passwords are not match!");								Customer c=new Customer(idno,name);				if (c.Equals(c.getEntityByID()))					throw new ATMException("Customer already exists!");								CustomerLogin cl=new CustomerLogin(idno,newpwd);								CustomerBalance cb=null;				int generateTime=10000;				do{					cardNo=Bank.current.NoGenerater();					cb=new CustomerBalance(idno,bank,cardNo,0);					if ((generateTime--)==0)						throw new ATMException("Sorry, There is no more numbers can be applied!");				}while(this.isAlreadyRegister(cb));								c.save();				cb.save();				cl.save();				atm.updateBalance();				string info="Successfully, Your card number is "+cardNo;				info+="\nYou may get it from the Clipboard";				MessageBox.Show(info,"ATM");				Clipboard.SetDataObject(cardNo,true);				atm.customer=c;				atm.showPage(ATMForm.OPERATION_PAGE);			}catch(ATMException ae){				MessageBox.Show(ae.Message,"ATM Error");			}		}				private void modifyPassword(ATMForm atm){			ArrayList controls=atm.getControls(ATMForm.MODIFY_PASSWORD_PAGE);			string oldpwd=((TextBox)controls[1]).Text.Trim();			string newpwd=((TextBox)controls[3]).Text.Trim();			string cfmpwd=((TextBox)controls[5]).Text.Trim();			string idno=atm.customer.IDNo;			try{				if (oldpwd.Length==0)					throw new ATMException("The Login Password should not be empty!");				if (newpwd.Length==0)					throw new ATMException("The New Password should not be empty!");				if (!newpwd.Equals(cfmpwd))					throw new ATMException("The passwords are not match!");								CustomerLogin cl=new CustomerLogin();				cl.IDNo=idno;				cl=(CustomerLogin)cl.getEntityByID();				if (!cl.password.Equals(oldpwd))					throw new ATMException("Password Error!");				cl.password=newpwd;				cl.save();								MessageBox.Show("Successfully");				atm.returnPage();			}catch(ATMException ae){				MessageBox.Show(ae.Message,"ATM Error");			}					}				private bool isAlreadyRegister(CustomerBalance cb){			ArrayList al=cb.getEntitiesByProperty("cardNo");			foreach(Object o in al){				if (((CustomerBalance)o).cardType.Equals(cb.cardType))					return true;			}			return false;		}	}		public class Confirm:IButtonDelegater{		public void Execute(object obj,EventArgs er){			DoJob doJob=null;			Button btn=(Button)obj;			ATMForm atm=(ATMForm)btn.GetContainerControl();			String page=atm.getCurrentPage();			if (page.Equals(ATMForm.DEPOSIT_PAGE)){				doJob=new DoJob(deposit);			}else if (page.Equals(ATMForm.DRAW_PAGE)){				doJob=new DoJob(draw);			}			doJob(atm);		}				private void deposit(ATMForm atm){			ArrayList controls=atm.getControls(ATMForm.DEPOSIT_PAGE);			int balance=0;			try{				balance=int.Parse(((TextBox)controls[1]).Text.Trim());			}catch(Exception e){				throw new ATMException(e);			}						try{				if ((balance % 50)!=0)					throw new ATMException("The Balance must be the multiple of 50");				CustomerBalance cb=new CustomerBalance();				cb.IDNo=atm.customer.IDNo;				cb=(CustomerBalance)cb.getEntityByID();				cb.balance=cb.balance+balance;				cb.save();								MessageBox.Show("Successfully");				atm.updateBalance();				atm.returnPage();			}catch(ATMException ae){				MessageBox.Show(ae.Message,"ATM Error");			}				}				private void draw(ATMForm atm){			ArrayList controls=atm.getControls(ATMForm.DRAW_PAGE);			int balance=0;			try{				balance=int.Parse(((TextBox)controls[2]).Text.Trim());			}catch(Exception e){				throw new ATMException(e);			}			try{				if ((balance % 50)!=0)					throw new ATMException("The Balance must be the multiple of 50");				CustomerBalance cb=new CustomerBalance();				cb.IDNo=atm.customer.IDNo;				cb=(CustomerBalance)cb.getEntityByID();				if (cb.balance<balance)					throw new ATMException("You do not have so many Balance!");				cb.balance=cb.balance-balance;				cb.save();								MessageBox.Show("Successfully");				atm.updateBalance();				atm.returnPage();			}catch(ATMException ae){				MessageBox.Show(ae.Message,"ATM Error");			}				}	}		public class Cancel:IButtonDelegater{		public void Execute(object obj,EventArgs er){			Button btn=(Button)obj;			ATMForm atm=(ATMForm)btn.GetContainerControl();			atm.returnPage();		}	}		public class ExitATM:IButtonDelegater{		public void Execute(object obj,EventArgs er){			Environment.Exit(0);		}	}}

⌨️ 快捷键说明

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