您好,欢迎访问三七文档
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceATM{abstractclassAccount{//账户号码protectedlongid;publiclongID{get{returnid;}set{id=value;}}//账户密码protectedstringpassword;publicstringPassWord{get{returnpassword;}set{password=value;}}//户主的姓名protectedstringname;publicstringName{get{returnname;}set{name=value;}}//身份证号码protectedstringpersonId;publicstringPersonId{get{returnpersonId;}set{personId=value;}}//emailprotectedstringemail;publicstringEmail{get{returnemail;}set{email=value;}}//余额protecteddoublebalance;publicdoubleBalance{get{returnbalance;}set{balance=value;}}//静态号码生成器privatestaticlongidBuilder=100000;publicstaticlongIdBuilder{get{returnidBuilder;}set{idBuilder=value;}}publicvoidDeposit(doublesum)//存款{if(sum0)thrownewInvalidOperationException(输入的金额为负数);balance+=sum;}publicabstractvoidWithdraw(doublesum);//取款publicAccount(){}publicAccount(stringpassword,stringname,stringpersonId,stringemail){this.id=++idBuilder;this.password=password;this.name=name;this.personId=personId;this.email=email;}}//创建CreditAccount类,该类继承抽象类AccountclassCreditAccount:Account{protecteddoubleceiling;//透支额度publicdoubleCeiling{get{returnceiling;}set{ceiling=value;}}publicCreditAccount(stringpassword,stringname,stringpersonId,stringemail):base(password,name,personId,email){}//信用账户的取款操作publicoverridevoidWithdraw(doublesum){if(sum0){thrownewInvalidOperationException(输入的金额为负数!);}if(sumbalance+ceiling){thrownewInvalidOperationException(金额已经超出余额和透支度的总数了);}balance-=sum;}}//创建SavingAccount类,该类继承抽象类AccountclassSavingAccount:Account{publicSavingAccount(stringpassword,stringname,stringpersonId,stringemail):base(password,name,personId,email){}publicoverridevoidWithdraw(doublesum){if(sum0){thrownewInvalidOperationException(输入的金额为负数!);}if(sumbalance){thrownewInvalidOperationException(金额已经超出金额!);}balance-=sum;}}//bank类,对银行中的所有账户进行管理classBank{//存放账户的集合privateListAccountaccounts;publicListAccountAccounts{get{returnaccounts;}set{accounts=value;}}//当前银行的账户数量privateintcurrentAccountNumber;publicintCurrentAccountNumber{get{returncurrentAccountNumber;}set{currentAccountNumber=value;}}//构造函数publicBank(){accounts=newListAccount();}//开户publicAccountOpenAccount(stringpassword,stringconfirmationPassword,stringname,stringpersonId,stringemail,inttypeOfAccount){AccountnewAccount;if(!password.Equals(confirmationPassword)){thrownewInvalidOperationException(两次密码输入的不一致);}switch(typeOfAccount){case1:newAccount=newSavingAccount(password,name,personId,email);break;case2:newAccount=newCreditAccount(password,name,personId,email);break;default:thrownewArgumentOutOfRangeException(账户类型是1和2之间的整数);}//把新开的账号加到集合中accounts.Add(newAccount);returnnewAccount;}//根据账户id得到账户对象privateAccountGetAccountByID(longid){foreach(Accountaccountinaccounts){if(account.ID==id){returnaccount;}}returnnull;}//根据账号和密码登陆账户publicAccountSignIn(longid,stringpassword){foreach(Accountaccountinaccounts){if(account.ID==id&&account.PassWord.Equals(password)){returnaccount;}}thrownewInvalidOperationException(用户名或者密码不正确,请重试);}//存款publicAccountDeposit(longid,doublesum){Accountaccount=GetAccountByID(id);if(account!=null){account.Deposit(sum);returnaccount;}thrownewInvalidOperationException(非法账户!);}//取款publicAccountWithdraw(longid,doublesum){Accountaccount=GetAccountByID(id);if(account!=null){account.Withdraw(sum);returnaccount;}thrownewInvalidOperationException(非法账户!);}//设置透支额度publicAccountSetCeiling(longid,doublenewCeiling){Accountaccount=GetAccountByID(id);try{(accountasCreditAccount).Ceiling=newCeiling;returnaccount;}catch(Exception){thrownewInvalidOperationException(次账户不是信用账户!);}thrownewInvalidOperationException(非法账户);}//统计银行所有账户余额publicdoubleGetTotalBalance(){doubletotalBalance=0;foreach(Accountaccountinaccounts){totalBalance+=account.Balance;}returntotalBalance;}//统计所有信用账户透支额度总数publicdoubleGetTotalCeiling(){doubletotalCeiling=0;foreach(Accountaccountinaccounts){if(accountisCreditAccount){totalCeiling+=(accountasCreditAccount).Ceiling;}}returntotalCeiling;}}//进行客户测试classProgram{staticAccountSignIn(Bankicbc){Console.WriteLine(\nPleaseinputyouraccountID);longid;try{id=long.Parse(Console.ReadLine());}catch(FormatException){Console.WriteLine(InvalidaccountID!);returnnull;}Console.WriteLine(Pleaseinputyourpassword);stringpassword=Console.ReadLine();Accountaccount;try{account=icbc.SignIn(id,password);}catch(InvalidOperationExceptionex){Console.WriteLine(ex.Message);returnnull;}returnaccount;}staticvoidMain(string[]args){Bankicbc=newBank();while(true){Console.WriteLine(Pleasechoosetheserviceyourneed);Console.WriteLine((1)Openanewaccount);Console.WriteLine((2)Desposit);Console.WriteLine((3)Withdraw);Console.WriteLine((4)SetCeiling);Console.WriteLine((5)GetTotalBalance);Console.WriteLine((6)GetTotalCeiling);Console.WriteLine((0)Exit);stringchoice;choice=Console.ReadKey().KeyChar.ToString();//ConsoleKeyi=Console.ReadKey().Key;switch(choice){case1:{stringpersonId;inttypeOfAccount;stringpassword;Console.WriteLine(\nWhichkindofaccountdoyouwanttoopen?);while(true){Console.WriteLi
本文标题:ATM示例2
链接地址:https://www.777doc.com/doc-4448333 .html