三个类太多了超过了限制了输入数量了,所以就多发了一个
ATM.cs类- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ATM
- {
- class ATM
- {
- private const string quitCode = "20060824"; //设置的退出程序的条件
- private Bank bank;
- public ATM(Bank bank) //这个ATM构造函数中参数初始化银行,至于参数Bank bank,为什么拿Bank类来当参数类型呢
- {
- this.bank = bank;
- }
- public void Start()
- {
- while (true)
- {
- //主界面
- PrintLogo();
- Console.WriteLine(" 1.开户 ");
- Console.WriteLine(" 2.登陆 ");
- Console.WriteLine("------------------------------");
- Console.WriteLine("");
- Console.WriteLine("请选择(回车结束):");
- string code = Console.ReadLine();
- //退出系统
- if (code == quitCode)
- {
- return;
- }
- if (code == "1") //开户
- {
- OpenAccount();
- }
- else if (code == "2") //登陆选择
- {
- LoginAccount();
- }
- }
- } //开始方法
- private void LoginAccount() //登陆
- {
- PrintLogo();
- Console.WriteLine(" 请输入你的账号的用户名和密码 ");
- Console.WriteLine("-----------------------------------------------");
- Console.WriteLine("");
- string name = Input("用户名(回车结束):");
- string password = Input("密码(回车结束):");
- //登陆账号
- Account account; //用类名来当类型,着实不懂,求指教
- if (!bank.LoginAccount(name,password, out account))
- {
- Console.Write("登陆错误,请检查用户名和密码是否正确。按Enter键继续....");
- Console.Read();
- }
- else
- {
- ManageAccount(ref account); //新的技术点,引用参数
- }
- }
- private void OpenAccount() //开户
- {
- PrintLogo();
- Console.WriteLine(" 请输入你的账号的用户名和密码 ");
- Console.WriteLine("");
- string name = Input("用户名(回车结束):");
- string password = Input("密码(回车结束):");
-
- //开户
- Account account;
- if (!bank.OpenAccount(name, password, out account))
- {
- Console.Write("开户错误,用户名可能已经存在。按Enter键继续.....");
- Console.Read();
- }
- else
- {
- Print("开户",0,account);
- Pause(); //这个不晓得干嘛的哈
- ManageAccount(ref account);
- }
- }
- private void ManageAccount(ref Account account) //账户操作
- {
- while (true)
- {
- PrintLogo();
- Console.WriteLine(" 1.存款 ");
- Console.WriteLine(" 2.取款 ");
- Console.WriteLine(" 3.查询余额 ");
- Console.WriteLine(" 4.修改密码 ");
- Console.WriteLine(" 5.退出 ");
- Console.WriteLine("----------------------------------");
- Console.WriteLine("");
- Console.WriteLine("你的选择(回车结束):");
- string code = Console.ReadLine();
- // string s;
- decimal amount;
- bool succeed;
- switch (code)
- {
- case "1":
- amount = InputNumber("\n输入存款数目:");
- succeed = account.Deposit(amount);
- if (succeed)
- {
- Print("存入", amount, account);
- }
- else
- {
- Console.WriteLine("存款失败!");
- }
- Pause();
- break;
- case "2":
- amount = InputNumber("\n输入存款数目:");
- succeed = account.Withdraw(amount);
- if (succeed)
- {
- Print("取出", amount, account);
- }
- else
- {
- Console.WriteLine("取款失败!");
- }
- Pause();
- break;
- case "3":
- Print(account);
- Pause();
- break;
- case "4":
- string oldPassword = Input("当前密码(回车结束):");
- string newPassword = Input("新密码(回车结束):");
- succeed = account.ChangePassword(oldPassword,newPassword);
- if (succeed)
- Console.WriteLine("密码修改成功!");
- else
- Console.WriteLine("密码修改失败!");
- Pause();
- break;
- case "5":
- return;
- default:
- break;
- }
- }
- } //账户管理
- private void PrintLogo() //显示Logo
- {
- Console.WriteLine("\n--------------------------------------");
- Console.WriteLine("{0}自动取款机 用户第一 服务至上",bank.Name);
- Console.WriteLine("----------------------------------------- ");
- }
- private string Input(string prompt) //输入金额参数控制
- {
- Console.Write(prompt);
- string str = Console.ReadLine();
- while (str == "")
- {
- Console.WriteLine("不能为空,{0}",prompt);
- str = Console.ReadLine();
- }
- return str;
- }
- private decimal InputNumber(string prompt) //输入参数控制重载
- {
- Console.WriteLine(prompt);
- string s = Console.ReadLine();
- //decimal amount = Decimal.Parse(s); //此处会有异常如果输入字母,字符键的时候,改成如下
- decimal amount = 0;
- try //异常处理
- {
- amount = Decimal.Parse(s);
- }
- catch
- {
- Console.Write("输入的数值格式不正确,请输入!");
- amount = InputNumber(prompt); //用递归调用InputNumber
- }
- return amount;
- }
- private void Pause() //暂停方法
- {
- Console.WriteLine("按Enter键继续.....");
- Console.Read();
- }
- private void Print(string operation, decimal amount, Account account) //操作显示
- {
- Console.WriteLine("--------------------------------");
- Console.WriteLine("姓名:{0}",account.Name);
- Console.WriteLine("{0}: {1}",operation,amount);
- Console.WriteLine("余额:{0}",account.Balance);
- Console.WriteLine("---------------------------------");
- Console.WriteLine("{0}成功!",operation);
- }
- public void Print(Account account) //查看显示
- {
- Console.WriteLine("---------------------------------");
- Console.WriteLine("姓名:{0}",account.Name);
- Console.WriteLine("余额:{0}",account.Balance);
- Console.WriteLine("----------------------------------");
- }
- }
- }
复制代码 |