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

📄 logic.cs

📁 是一个模拟ATM的程序
💻 CS
字号:
// Custom.cs created with MonoDevelop// User: Estelle at 12:09 PM 5/11/2008//// To change standard headers go to Edit->Preferences->Coding->Standard Headers//using System;using System.Reflection;using System.Collections;using System.IO;namespace ATM{		/**	 * The entity is an abstract for model which will be saved in the files.	 * It defines a lot of general used methods.	 */	public abstract class Entity{				/**		 * Save or update an entity in their file.		 */		public virtual int save(){			Type type=this.GetType();			if (type.GetProperty("IDNo").GetGetMethod().Invoke(this,null)==null)				return 0;			ArrayList entities=this.getEntities();			if (entities.Contains(this))entities.Remove(this);			entities.Add(this);			StreamWriter sw=new StreamWriter(this.getFileStream(FileMode.Truncate,FileAccess.Write,FileShare.Read));			foreach (Object entity in entities)				sw.WriteLine(entity.ToString());			sw.Close();			return 1;		}				/**		 * Get the entity from file by comparing its primary key.		 */		public virtual Entity getEntityByID(){			Entity entity=null;			Type type=this.GetType();			if (type.GetProperty("IDNo").GetGetMethod().Invoke(this,null)==null)				return null;			MethodInfo generater=type.GetMethod("To"+type.Name);			StreamReader sr=new StreamReader(this.getFileStream(FileMode.OpenOrCreate,FileAccess.Read,FileShare.ReadWrite));			string record="";			while((record=sr.ReadLine())!=null){
                try{
                    entity = (Entity)generater.Invoke(this, new String[] { record });
                    if (this.Equals(entity)) break;
                }catch (ATMException ae){
                    Console.Error.WriteLine(ae.Message);
                }catch (TargetInvocationException tie){
                    throw new ATMException(tie.GetBaseException());
                }			}			sr.Close();			return entity;		}				/**		 * Get the collection of the entities from file by comparing the given property.		 */		public virtual ArrayList getEntitiesByProperty(string property){			ArrayList entities=new ArrayList();			Type type=this.GetType();			Object values=type.GetProperty(property).GetGetMethod().Invoke(this,null);			if (values==null)return entities;			MethodInfo generater=type.GetMethod("To"+type.Name);			StreamReader sr=new StreamReader(this.getFileStream(FileMode.OpenOrCreate,FileAccess.Read,FileShare.ReadWrite));			string record="";			while((record=sr.ReadLine())!=null){				try{					Entity entity=(Entity)generater.Invoke(this,new String[]{record});					MethodInfo mi=entity.GetType().GetProperty(property).GetGetMethod();					Object data=mi.Invoke(entity,null);					if (values.ToString().Equals(data.ToString()))						entities.Add(entity);				}catch(ATMException ae){					Console.Error.WriteLine(ae.Message);
                }catch (TargetInvocationException tie){
                    throw new ATMException(tie.GetBaseException());
                }			}			sr.Close();			return entities;		}				/**		 * Get all the entities form the file. 		 */		public virtual ArrayList getEntities(){			ArrayList entities=new ArrayList();			Type type=this.GetType();			MethodInfo generater=type.GetMethod("To"+type.Name);			StreamReader sr=new StreamReader(this.getFileStream(FileMode.OpenOrCreate,FileAccess.Read,FileShare.ReadWrite));			string record="";			while((record=sr.ReadLine())!=null){				try{					entities.Add(generater.Invoke(this,new String[]{record}));				}catch(ATMException ae){					Console.Error.WriteLine(ae.Message);
                }catch (TargetInvocationException tie){
                    throw new ATMException(tie.GetBaseException());
                }			}			sr.Close();			return entities;		}				/**		 * Traslate the entity into the string for saving it.		 */		public override string ToString(){			string result="";			foreach (PropertyInfo fi in this.GetType().GetProperties())				result+=fi.GetGetMethod().Invoke(this,null).ToString()+",";			result=result.Substring(0,result.Length-1);			return result;		}				public abstract override int GetHashCode();				public override bool Equals(object target){			if (target==null)				return false;			if (!this.GetType().Equals(target.GetType()))				return false;			if (this.GetHashCode()!=target.GetHashCode())				return false;			return true;		}				protected void checkIDNo(string no){			char last=no[no.Length-1];			string ic=((last=='x')||(last=='X'))?no.Substring(0,no.Length-1):no;			if (!this.isDigitString(ic))				throw new ATMException("Identity Card Number is invalid!");			if (ic.Length==ATMForm.IC_NUMBER_LENGTH-1)ic+="x";			if (ic.Length!=ATMForm.IC_NUMBER_LENGTH)				throw new ATMException("Identity Card Number is invalid!");		}				protected bool isDigitString(string str){			foreach (char c in str.ToCharArray())				if (!char.IsDigit(c))return false;			return true;		}				protected FileStream getFileStream(FileMode fm,FileAccess fa,FileShare fs){			Type type=this.GetType();			String dbname=type.GetField("DBNAME").GetValue(this).ToString();			FileStream fsm=File.Open(dbname,fm,fa,fs);			return fsm;		}	}		public class Customer:Entity{		public const string DBNAME="Customer.atm";				private string idno;		public string IDNo{			set{				checkIDNo(value);				this.idno=value;			}			get{				return this.idno;			}		}		private string name;		public string realName{			set{				if (value.Length>ATMForm.MAX_NAME_LENGTH){					throw new ATMException(					     "The length of Name should not longer than "+ATMForm.MAX_NAME_LENGTH+"!"					);				}				if (value.IndexOf(",")>=0)					throw new ATMException("Name is invalid!");				this.name=value;			}			get{				return this.name;			}		}				/**		 * Translate a specail string into an entity instance.		 * It is the reversion of the method "override String ToString".		 */		public static Customer ToCustomer(String info){			String [] pts=info.Split(new Char[]{','});			if (pts.Length!=2)				throw new ATMException("Can't generate the Customer from the given String!");			return new Customer(pts[0],pts[1]);		}				public Customer(){}						public Customer(string idno,string name){			this.IDNo=idno;			this.realName=name;		}				public override int GetHashCode (){			return this.IDNo.GetHashCode();		}	}		public class CustomerLogin:Entity{				public const string DBNAME="CustomerLogin.atm";				private string idno;		public string IDNo{			set{				checkIDNo(value);				this.idno=value;			}			get{				return this.idno;			}		}				private string pwd;		public string password{			set{				if (value.Length!=ATMForm.PASSWORD_LENGTH){					throw new ATMException(					     "The length of Password must be "+ATMForm.PASSWORD_LENGTH.ToString()+"!"					);				}				if (!this.isDigitString(value))					throw new ATMException("Password must be digit!");				if (value.IndexOf(",")>=0)					throw new ATMException("Password is invalid!");				this.pwd=value;			}			get{				return this.pwd;			}		}				public static CustomerLogin ToCustomerLogin(String info){			String [] pts=info.Split(new Char[]{','});			if (pts.Length!=2)				throw new ATMException("Can't generate the CustomerLogin from the given String!");			return new CustomerLogin(pts[0],pts[1]);		}				public CustomerLogin(){}						public CustomerLogin(string idno,string pwd){			this.IDNo=idno;			this.password=pwd;		}				public override int GetHashCode (){			return this.IDNo.GetHashCode();		}	}		public class CustomerBalance:Entity{				public const string DBNAME="CustomerBalance.atm";				private string idno;		public string IDNo{			set{				checkIDNo(value);				this.idno=value;			}			get{				return this.idno;			}		}				private string cardtype;		public string cardType{			set{				bool isPass=false;				foreach (Object o in ATMForm.Banks)					if (value.Equals(((Bank)o).shortName))isPass=true;				if (!isPass)					throw new ATMException("Unsupported Bank!");				this.cardtype=value;			}			get{				return this.cardtype;			}		}				private string cardno;		public string cardNo{			set{				if (!this.isDigitString(value))					throw new ATMException("Card Number must be digit!");				if (value.Length!=ATMForm.CARD_NUMBER_LENGTH)					throw new ATMException(					     "The length of Card Number must be "+ATMForm.CARD_NUMBER_LENGTH.ToString()+"!"					);				this.cardno=value;			}			get{				return this.cardno;			}		}				private int money;		public int balance{			set{				if (value<0)					throw new ATMException("The money canot be negative!");				this.money=value;			}			get{				return this.money;			}		}				public static CustomerBalance ToCustomerBalance(String info){			String [] pts=info.Split(new Char[]{','});			if (pts.Length!=4)				throw new ATMException("Can't generate the CustomerBalance from the given String!");			return new CustomerBalance(pts[0],pts[1],pts[2],Int32.Parse(pts[3]));		}				public CustomerBalance(){}				public CustomerBalance(string idno,string ct,string cn,int balance){			this.IDNo=idno;			this.cardType=ct;			this.cardNo=cn;			this.balance=balance;		}				public override int GetHashCode (){			return this.IDNo.GetHashCode();		}

        public override string ToString(){
            string result = "";
            result += this.idno+",";
            result += this.cardType + ",";
            result += this.cardNo + ",";
            result += this.balance.ToString();
            return result;
        }	}		public abstract class Bank{		public static Bank current=null;				public abstract string name{get;}		public abstract string shortName{get;}				/**		 * Gnerate the number of the bank card.		 */		public abstract string NoGenerater();				public override string ToString (){			return this.name;		}				protected string fill(string target,int length){			string rt=target;			for (int i=0;i<length-target.Length;i++)				rt="0"+rt;			return rt;		}	}		public class CCB:Bank{		public override string name{	get{ return "China Constructor Bank"; } }		public override string shortName{ get{ return "CCB"; } }				public override string NoGenerater(){			string cardNo="436742";			Random rnd=new Random((int)DateTime.Now.Ticks);			cardNo+=this.fill(rnd.Next(0,999).ToString(),3);			cardNo+=this.fill(rnd.Next(0,999).ToString(),3);			cardNo+=this.fill(rnd.Next(0,999999).ToString(),6);			int check=0;			for(int i=0;i<cardNo.Length;i=i+2)				check=check+(int.Parse(cardNo[i].ToString()))*2;			for(int i=1;i<cardNo.Length;i=i+2)				check=check+(int.Parse(cardNo[i].ToString()))*1;			cardNo+=(check % 10).ToString();			return cardNo;		}	}		public class ICBC:Bank{		public override string name{ get{ return "Industrial and Commercial Bank of China"; } }		public override string shortName{ get{ return "ICBC"; } }				public override string NoGenerater(){			throw new ATMException("Sorry, This type of bank is in the maintenance!");			return "";		}	}}

⌨️ 快捷键说明

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