A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 贡宗新 中级黑马   /  2013-4-14 01:05  /  1501 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Account.cs类
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ATM
  6. {
  7.     class Account   //源文件
  8.     {
  9.         protected string name;
  10.         protected string password;
  11.         protected decimal balance;
  12.         public decimal Balance    //balance属性 账户金额初始化
  13.         {
  14.             get
  15.             {
  16.                 return balance;
  17.             }
  18.         }
  19.         public string Name     //属性  用户名初始化
  20.         {
  21.             get
  22.             {
  23.                 return name;
  24.             }
  25.         }

  26.         public Account(string name, string password)       //构造函数
  27.         {
  28.             this.balance = 0;
  29.             this.name = name;
  30.             this.password = password;
  31.         }

  32.         public bool Deposit(decimal amount)      //存款方法
  33.         {
  34.             if (amount <= 0)
  35.                 return false;
  36.             balance += amount;
  37.             return true;
  38.         }

  39.         public bool Deposit(double amount)       //重载
  40.         {
  41.             return Deposit((decimal)amount);
  42.         }

  43.         public bool Deposit(int amount)         //重载方法
  44.         {
  45.             return Deposit((decimal)amount);
  46.         }

  47.         public bool Deposit(decimal amount,out decimal balance)   //输出参数out
  48.         {
  49.             bool succeed = Deposit(amount);
  50.             balance = this.balance;
  51.             return succeed;
  52.         }

  53.         public bool Withdraw(decimal amount)      //取钱方法
  54.         {
  55.             if (amount > balance || amount <= 0)
  56.             {
  57.                 return false;
  58.             }

  59.             balance -= amount;
  60.             return true;
  61.         }

  62.         public bool Withdraw(double amount)   //取钱方法重载
  63.         {
  64.             return Withdraw((decimal)amount);
  65.         }

  66.         public bool Withdraw(int amount)
  67.         {
  68.             return Withdraw((decimal)amount);
  69.         }

  70.         public bool Withdraw(decimal amount, out decimal balance)
  71.         {
  72.             bool succeed = Withdraw(amount);
  73.             balance = this.balance;
  74.             return succeed;
  75.         }

  76.         public bool ChangePassword(string oldPassword,string newPassword)  //改密码方法
  77.         {
  78.             if (oldPassword != password)
  79.             {
  80.                 return false;
  81.             }
  82.             password = newPassword;
  83.             return true;
  84.                
  85.         }

  86.         public bool Login(string name, string password)     //登陆判断
  87.         {
  88.             return (this.name == name && this.password == password);
  89.         }
  90.     }
  91. }
复制代码
Bank.cs类
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ATM
  6. {
  7.     class Bank
  8.     {
  9.         protected const int MaxAccountNum = 2048;  //什么意思
  10.         protected string name;
  11.         protected int usedAccountNum;
  12.         protected Account[] accounts;

  13.         public string Name       //属性
  14.         {
  15.             get
  16.             {
  17.                 return name;
  18.             }
  19.         }

  20.         public Bank(string name)
  21.         {
  22.             this.name = name;
  23.             this.usedAccountNum = 0;   //初始化用户金额
  24.             accounts = new Account[MaxAccountNum];   //这个又是什么意思,索引器吗???
  25.         }

  26.         public bool LoginAccount(string name, string password, out Account account)
  27.         {
  28.             account = null;
  29.             for (int i = 0; i < usedAccountNum; ++i)
  30.             {
  31.                 if (accounts[i].Login(name, password))    //有几个账户,遍历账户判断是否存在匹配账户密码
  32.                 {
  33.                     account = accounts[i];
  34.                     return true;
  35.                 }
  36.             }
  37.             return false;
  38.         }

  39.         public bool OpenAccount(string name, string password, out Account account)   //开户
  40.         {
  41.             account = null;
  42.             for (int i = 0; i < usedAccountNum; i++)
  43.             {
  44.                 if (accounts[i].Name == name)
  45.                 {
  46.                     return false;
  47.                 }
  48.             }

  49.             account = new Account(name, password);
  50.             accounts[usedAccountNum++] = account;

  51.             return true;
  52.         }


  53.     }
  54. }
复制代码
可能其中也有些不懂的地方,请大家讲解一下咯,总的来看对学完基础非常有好处

1 个回复

倒序浏览
三个类太多了超过了限制了输入数量了,所以就多发了一个

ATM.cs类
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ATM
  6. {
  7.     class ATM
  8.     {
  9.         private const string quitCode = "20060824";    //设置的退出程序的条件
  10.         private Bank bank;
  11.         public ATM(Bank bank)   //这个ATM构造函数中参数初始化银行,至于参数Bank bank,为什么拿Bank类来当参数类型呢
  12.         {
  13.             this.bank = bank;
  14.         }

  15.         public void Start()
  16.         {
  17.             while (true)
  18.             {
  19.                 //主界面
  20.                 PrintLogo();
  21.                 Console.WriteLine("          1.开户             ");
  22.                 Console.WriteLine("          2.登陆             ");
  23.                 Console.WriteLine("------------------------------");
  24.                 Console.WriteLine("");
  25.                 Console.WriteLine("请选择(回车结束):");
  26.                 string code = Console.ReadLine();

  27.                 //退出系统
  28.                 if (code == quitCode)
  29.                 {
  30.                     return;
  31.                 }
  32.                 if (code == "1")      //开户
  33.                 {
  34.                     OpenAccount();
  35.                 }
  36.                 else if (code == "2")      //登陆选择
  37.                 {
  38.                     LoginAccount();
  39.                 }
  40.             }
  41.         }        //开始方法

  42.         private void LoginAccount()                              //登陆
  43.         {
  44.             PrintLogo();
  45.             Console.WriteLine("         请输入你的账号的用户名和密码          ");
  46.             Console.WriteLine("-----------------------------------------------");
  47.             Console.WriteLine("");
  48.             string name = Input("用户名(回车结束):");
  49.             string password = Input("密码(回车结束):");

  50.             //登陆账号
  51.             Account account;     //用类名来当类型,着实不懂,求指教
  52.             if (!bank.LoginAccount(name,password, out account))
  53.             {
  54.                 Console.Write("登陆错误,请检查用户名和密码是否正确。按Enter键继续....");
  55.                 Console.Read();
  56.             }
  57.             else
  58.             {
  59.                 ManageAccount(ref account);      //新的技术点,引用参数
  60.             }
  61.         }

  62.         private void OpenAccount()                      //开户
  63.         {
  64.             PrintLogo();
  65.             Console.WriteLine("         请输入你的账号的用户名和密码             ");
  66.             Console.WriteLine("");
  67.             string name = Input("用户名(回车结束):");
  68.             string password = Input("密码(回车结束):");
  69.             


  70.             //开户

  71.             Account account;
  72.             if (!bank.OpenAccount(name, password, out account))
  73.             {
  74.                 Console.Write("开户错误,用户名可能已经存在。按Enter键继续.....");
  75.                 Console.Read();
  76.             }
  77.             else
  78.             {
  79.                 Print("开户",0,account);
  80.                 Pause();       //这个不晓得干嘛的哈
  81.                 ManageAccount(ref account);

  82.             }
  83.         }
  84.         private void ManageAccount(ref Account account)       //账户操作
  85.         {
  86.             while (true)
  87.             {
  88.                 PrintLogo();
  89.                 Console.WriteLine("           1.存款            ");
  90.                 Console.WriteLine("           2.取款            ");
  91.                 Console.WriteLine("           3.查询余额            ");
  92.                 Console.WriteLine("           4.修改密码            ");
  93.                 Console.WriteLine("           5.退出            ");
  94.                 Console.WriteLine("----------------------------------");
  95.                 Console.WriteLine("");
  96.                 Console.WriteLine("你的选择(回车结束):");
  97.                 string code = Console.ReadLine();
  98.              //  string s;
  99.                 decimal amount;
  100.                 bool succeed;
  101.                 switch (code)
  102.                 {
  103.                     case "1":
  104.                         amount = InputNumber("\n输入存款数目:");
  105.                         succeed = account.Deposit(amount);
  106.                         if (succeed)
  107.                         {
  108.                             Print("存入", amount, account);

  109.                         }
  110.                         else
  111.                         {
  112.                             Console.WriteLine("存款失败!");
  113.                         }
  114.                         Pause();
  115.                         break;

  116.                     case "2":
  117.                         amount = InputNumber("\n输入存款数目:");
  118.                         succeed = account.Withdraw(amount);
  119.                         if (succeed)
  120.                         {
  121.                             Print("取出", amount, account);
  122.                         }
  123.                         else
  124.                         {
  125.                             Console.WriteLine("取款失败!");
  126.                         }
  127.                         Pause();
  128.                         break;

  129.                     case "3":
  130.                         Print(account);
  131.                         Pause();
  132.                         break;

  133.                     case "4":
  134.                         string oldPassword = Input("当前密码(回车结束):");
  135.                         string newPassword = Input("新密码(回车结束):");
  136.                         succeed = account.ChangePassword(oldPassword,newPassword);

  137.                         if (succeed)
  138.                             Console.WriteLine("密码修改成功!");
  139.                         else
  140.                             Console.WriteLine("密码修改失败!");
  141.                         Pause();
  142.                         break;

  143.                     case "5":
  144.                         return;
  145.                     default:
  146.                         break;
  147.                 }
  148.             }
  149.         }     //账户管理

  150.         private void PrintLogo()           //显示Logo
  151.         {
  152.             Console.WriteLine("\n--------------------------------------");
  153.             Console.WriteLine("{0}自动取款机     用户第一   服务至上",bank.Name);
  154.             Console.WriteLine("----------------------------------------- ");
  155.         }
  156.        private string Input(string prompt)      //输入金额参数控制
  157.         {
  158.             Console.Write(prompt);     
  159.             string str = Console.ReadLine();

  160.             while (str == "")
  161.             {

  162.                 Console.WriteLine("不能为空,{0}",prompt);
  163.                 str = Console.ReadLine();
  164.             }
  165.             return str;
  166.         }

  167.         private decimal InputNumber(string prompt)    //输入参数控制重载
  168.         {
  169.             Console.WriteLine(prompt);
  170.             string s = Console.ReadLine();

  171.             //decimal amount = Decimal.Parse(s);    //此处会有异常如果输入字母,字符键的时候,改成如下
  172.             decimal amount = 0;
  173.             try                    //异常处理
  174.             {
  175.                 amount = Decimal.Parse(s);
  176.             }
  177.             catch
  178.             {
  179.                 Console.Write("输入的数值格式不正确,请输入!");
  180.                 amount = InputNumber(prompt);    //用递归调用InputNumber
  181.             }
  182.             return amount;
  183.         }

  184.         private void Pause()     //暂停方法
  185.         {
  186.             Console.WriteLine("按Enter键继续.....");
  187.             Console.Read();
  188.         }

  189.         private void Print(string operation, decimal amount, Account account)   //操作显示
  190.         {
  191.             Console.WriteLine("--------------------------------");
  192.             Console.WriteLine("姓名:{0}",account.Name);
  193.             Console.WriteLine("{0}: {1}",operation,amount);
  194.             Console.WriteLine("余额:{0}",account.Balance);
  195.             Console.WriteLine("---------------------------------");
  196.             Console.WriteLine("{0}成功!",operation);
  197.         }

  198.         public void Print(Account account)                //查看显示
  199.         {
  200.             Console.WriteLine("---------------------------------");
  201.             Console.WriteLine("姓名:{0}",account.Name);
  202.             Console.WriteLine("余额:{0}",account.Balance);
  203.             Console.WriteLine("----------------------------------");
  204.         }
  205.     }
  206. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马