• 自助取款机系统(C#)


    ATM(自动取款机)系统向用户提供一个方便、简单、及时、随时随地可以随心所欲存取款的、互联的、计算机化的网络系统 ,可以大大减少工作人员,节约人力资源的开销;同时,由于手续减少,减轻了业务员的工作负担,有效地提高了整体的工作效率和精确度,减少了用户办理业务的等待时间;用户可以随时随地存取款,并且操作简单;用户还可以自主选择在柜台办理业务或自己在自助取款机办理业务。

    打开Visual Studio 2022,点击创建新项目。

     在创建新项目中,选择C#--Windows--控制台,选择控制台应用(.NET Framework) ,点击下一步。

    配置新项目,在项目名称是Automatic-Teller-Machine,然后点击创建。

    创建新项目以后:

    1、帐号类

    帐号类(Account)包含所有的账号信息,负责所有的账号操作。基本的帐号信息包括账号名(name)、账号密码(password)、账号余额(balance)。主要的账号操作包括登录(Login)、存款(Deposit)、取款(Withdraw)、查询余额(Get_Balance)、修改密码(ChangePassword)。

    鼠标指针放在Automatic-Teller-Machine的字样,点击右键,选择“添加”,再选择“类”。

     在添加新项上,名称修改,输入是Account.cs,点击添加。

     添加新项之后:

     代码如下:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace Automatic_Teller_Machine
    7. {
    8. internal class Account
    9. {
    10. protected string name;
    11. protected string password;
    12. protected decimal balance;
    13. public decimal Banlance
    14. {
    15. get
    16. {
    17. return balance;
    18. }
    19. }
    20. public string Name
    21. {
    22. get
    23. {
    24. return name;
    25. }
    26. }
    27. //构造函数
    28. public Account(string name, string password)
    29. {
    30. this.name = name;
    31. this.password = password;
    32. this.balance = 0;
    33. }
    34. public bool Deposit(decimal amount)
    35. {
    36. if(amount<=0)
    37. {
    38. return false;
    39. }
    40. balance += amount;
    41. return true;
    42. }
    43. //存款的三种重载方法
    44. public bool Deposit(double amount)
    45. {
    46. return Deposit((decimal)amount);
    47. }
    48. public bool Deposit(int amount)
    49. {
    50. return Deposit((decimal)amount);
    51. }
    52. public bool Deposit(decimal amount,out decimal balance)
    53. {
    54. bool succeed=Deposit(amount);
    55. balance = this.balance;
    56. return succeed;
    57. }
    58. public bool Withdraw(decimal amount)
    59. {
    60. if(amount>balance||amount<=0)
    61. { return false;
    62. }
    63. balance -= amount;
    64. return true;
    65. }
    66. //取款的三种重载方法
    67. public bool Withdraw(double amount)
    68. {
    69. return Withdraw((decimal)amount);
    70. }
    71. public bool Withdraw(int amount)
    72. {
    73. return Withdraw((decimal)amount);
    74. }
    75. public bool Withdraw(decimal amount,out decimal balance)
    76. {
    77. bool succeed = Withdraw(amount);
    78. balance = this.balance;
    79. return succeed;
    80. }
    81. //修改密码
    82. public bool ChangePassword(string oldPassword, string newPassword)
    83. {
    84. if(oldPassword!=password)
    85. {
    86. return false;
    87. }
    88. password = newPassword;
    89. return true;
    90. }
    91. //进入系统
    92. public bool Login(string name,string password)
    93. {
    94. return (this.name == name && this.password == password);
    95. }
    96. }
    97. }

    2、银行类

    银行类(Bank)的本质就是一组账号的组合,并负责管理账号。基本的银行信息包括银行名(name)、已经开户的账号数(usedAccountNum)、可以容纳的最大账户数(MaxAccountNum)、账号集(accounts)。主要的银行操作包括开户(OpenAccount)、登录账号(LoginAccount)。

    鼠标指针放在Automatic-Teller-Machine的字样,点击右键,选择“添加”,再选择“类”。

    在添加新项上,名称修改,输入是Bank.cs,点击添加。

    添加新项之后:

      代码如下:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace Automatic_Teller_Machine
    7. {
    8. internal class Bank
    9. {
    10. protected string name;
    11. protected const int MaxAccountNum = 2048;
    12. protected int usedAccountNum;
    13. protected Account[] accounts;
    14. public string Name
    15. {
    16. get
    17. {
    18. return name;
    19. }
    20. }
    21. public Bank(string name)
    22. {
    23. this.name = name;
    24. this.usedAccountNum = 0;
    25. accounts= new Account[MaxAccountNum];
    26. }
    27. public bool LoginAccount(string name,string password, out Account account)
    28. {
    29. account= null;
    30. for(int i=0;i
    31. {
    32. if (accounts[i].Login(name,password))
    33. {
    34. account = accounts[i];
    35. return true;
    36. }
    37. }
    38. return false;
    39. }
    40. public bool OpenAccount(string name,string password,out Account account)
    41. {
    42. account = null;
    43. for(int i=0;i
    44. {
    45. if (accounts[i].Name==name)
    46. {
    47. return false;
    48. }
    49. }
    50. account = new Account(name, password);
    51. accounts[usedAccountNum++] = account;
    52. return true;
    53. }
    54. }
    55. }

    3、ATM类

    ATM类与银行类之间存在一对一的关联关系,ATM提供用户界面,并将用户的请求提交给银行,将银行的反馈提交给用户。主要的ATM操作包括启动(Start)、开户(OpenAccount)、登录账号(LoginAccount)、管理账号(ManageAccount)、一些显示不同信息的辅助操作(Print、Pause等)。

    鼠标指针放在Automatic-Teller-Machine的字样,点击右键,选择“添加”,再选择“类”。

    在添加新项上,名称修改,输入是ATM.cs,点击添加。

     添加新项之后:

     代码如下:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace Automatic_Teller_Machine
    7. {
    8. internal class ATM
    9. {
    10. private const string quitcode = "5";
    11. private Bank bank;
    12. public ATM(Bank bank)
    13. {
    14. this.bank = bank;
    15. }
    16. //开始页面
    17. public void Start()
    18. {
    19. while(true)
    20. {
    21. //主界面
    22. Console.WriteLine();
    23. Console.WriteLine(" ******************************");
    24. Console.WriteLine(" *-----------1.开户-----------*");
    25. Console.WriteLine(" *-----------2.登录-----------*");
    26. Console.WriteLine(" *-----------3.退出-----------*");
    27. Console.Write("请输入您的选择(回车结束):");
    28. string code=Console.ReadLine();
    29. if(code==quitcode)
    30. {
    31. return;
    32. }
    33. if(code=="1")
    34. {
    35. OpenAccount();
    36. }
    37. else if(code=="2")
    38. {
    39. LoginAccount();
    40. }
    41. else if(code=="3")
    42. {
    43. Console.WriteLine("按一下任意键直接退出...");
    44. Console.ReadKey();
    45. return;
    46. }
    47. }
    48. }
    49. //账号登录方法
    50. private void LoginAccount()
    51. {
    52. Console.Clear();
    53. Console.WriteLine(" * 您已进入登录界面 *");
    54. Console.WriteLine(" =====================================");
    55. Console.WriteLine(" * 请输入您的账号的用户名和密码 *");
    56. Console.WriteLine(" =====================================");
    57. string name = Input("用户名(回车结束):");
    58. string password = Input("密 码(回车结束):");
    59. //登录账号
    60. Account account;
    61. if(!bank.LoginAccount(name,password, out account))
    62. {
    63. Console.WriteLine("---登录错误,请检查用户名和密码是否正确。按Enter键继续---");
    64. Console.Read();
    65. }
    66. else
    67. {
    68. ManageAccount(ref account);
    69. }
    70. }
    71. //开户方法
    72. private void OpenAccount()
    73. {
    74. Console.WriteLine(" =====================================");
    75. Console.WriteLine(" * 请输入您的帐号的用户名和密码 *");
    76. Console.WriteLine(" =====================================");
    77. string name = Input("用户名(回车结束):");
    78. string password = Input("密 码(回车结束):");
    79. //登录账号
    80. Account account;
    81. if (!bank.OpenAccount(name, password, out account))
    82. {
    83. Console.WriteLine(" *开户错误,用户名和密码已经存在。按Enter键继续---*");
    84. Console.Read();
    85. }
    86. else
    87. {
    88. Print("开户",0,account);
    89. ManageAccount(ref account);
    90. }
    91. }
    92. //账号管理方法
    93. private void ManageAccount(ref Account account)
    94. {
    95. Console.Clear();
    96. Console.WriteLine(" ******************************");
    97. Console.WriteLine(" *-----------1.存款------------");
    98. Console.WriteLine(" *-----------2.取款------------");
    99. Console.WriteLine(" *-----------3.查询余额--------");
    100. Console.WriteLine(" *-----------4.修改密码--------");
    101. Console.WriteLine(" *-----------5.回到主页--------");
    102. Console.WriteLine(" ******************************");
    103. Console.Write("您的选择是(回车结束):");
    104. while(true)
    105. {
    106. //管理账号界面
    107. Console.WriteLine("");
    108. string code = Console.ReadLine();
    109. //string s:
    110. decimal amount;
    111. bool succeed;
    112. switch(code)
    113. {
    114. case "1":
    115. amount = InputNumber("\n 请输入存款数目");
    116. succeed=account.Deposit(amount);
    117. if(succeed)
    118. {
    119. Print("存入", amount, account);
    120. }
    121. else
    122. {
    123. Console.WriteLine("存款失败!");
    124. }
    125. Pause();
    126. break;
    127. case "2":
    128. amount = InputNumber("\n 请输入取款数目:");
    129. succeed = account.Withdraw(amount);
    130. if(succeed)
    131. {
    132. Print("取出", amount, account);
    133. }
    134. else
    135. {
    136. Console.WriteLine("取款失败!");
    137. }
    138. Pause();
    139. break;
    140. case "3":
    141. break;
    142. case "4":
    143. string oldPassword = Input("当前密码(回车结束):");
    144. string newPassword = Input("新密码(回车结束):");
    145. succeed=account.ChangePassword(oldPassword, newPassword);
    146. if(succeed)
    147. {
    148. Console.WriteLine("密码修改成功!");
    149. }
    150. else
    151. {
    152. Console.WriteLine("密码修改失败!");
    153. }
    154. Pause();
    155. break;
    156. case "5":
    157. Console.Clear();
    158. break;
    159. default:
    160. break;
    161. }
    162. }
    163. }
    164. //输出信息
    165. private string Input(string prompt)
    166. {
    167. Console.Write(prompt);
    168. string str=Console.ReadLine();
    169. while(str=="")
    170. {
    171. Console.Write("不能为空, {0}",prompt);
    172. str= Console.ReadLine();
    173. }
    174. return str;
    175. }
    176. private decimal InputNumber(string prompt)
    177. {
    178. Console.WriteLine(prompt);
    179. string s=Console.ReadLine();
    180. decimal amount = Decimal.Parse(s);
    181. return amount;
    182. }
    183. //打印辅助信息
    184. private void Pause()
    185. {
    186. Console.WriteLine("按Enter继续...");
    187. Console.Read();
    188. }
    189. //打印信息
    190. private void Print(string operation,decimal amount,Account account)
    191. {
    192. Console.WriteLine(" =====================================");
    193. Console.WriteLine(" *姓名:"+account.Name);
    194. Console.WriteLine(" *"+operation+":"+amount);
    195. Console.WriteLine(" *余额:" + account.Banlance);
    196. Console.WriteLine(" " + operation + "成功");
    197. Console.WriteLine(" =====================================");
    198. }
    199. private void Print(Account account)
    200. {
    201. Console.WriteLine(" =====================================");
    202. Console.WriteLine(" *姓名:{0}" , account.Name);
    203. Console.WriteLine(" *余额:{0}", account.Banlance);
    204. Console.WriteLine(" =====================================");
    205. }
    206. }
    207. }

    4、启动程序类

    启动程序类(Program)的唯一功能就是创建银行类和ATM类的实例,并将它们关联起来,然后启动ATM(执行ATM对象的Start方法)。因此,该类仅仅是包含程序 的入口点Main方法。

    鼠标指针放在Automatic-Teller-Machine的字样,点击右键,选择“添加”,再选择“类”。

    在添加新项上,名称修改,输入是Account.cs,点击添加。

     添加新项之后:

     代码如下:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace Automatic_Teller_Machine
    7. {
    8. internal class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.WriteLine(" ******************************");
    13. Console.WriteLine(" * 欢迎登录中国银行 *");
    14. Console.WriteLine(" ******************************");
    15. //实例化Bank类
    16. Bank bank = new Bank("ATM自助取款机");
    17. //实例化ATM类
    18. ATM atm=new ATM(bank);
    19. atm.Start();
    20. }
    21. }
    22. }

  • 相关阅读:
    SAP - 事务码
    QT+OSG/osgEarth编译之四十:osg+Qt编译(一套代码、一套框架,跨平台编译,版本:OSG-3.6.5核心库osg)
    在CARLA中手动开车,添加双目相机stereo camera,激光雷达Lidar
    深入解析JVM的GC过程
    【国外框架】—— quasar 响应式
    【网络安全---XSS漏洞(1)】XSS漏洞原理,产生原因,以及XSS漏洞的分类。附带案例和payload让你快速学习XSS漏洞
    Flask 框架:运用Echarts绘制图形
    数据结构----线性表之队列
    AlexNet论文笔记
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java抗包虫病药物查询与推荐系统rx40p
  • 原文地址:https://blog.csdn.net/DXB2021/article/details/125694776