MyBank(自助银行)

今天我来给大家说说说C#这东西的用处吧。

就拿MyBank系统来讲解吧,望大家好好看将会得到不少的收获哦。

那么这个MyBank系统我们应该怎样来写呢?请看看下面的详细解答。

首先先看一些部分功能的效果图

想要实现以上的功能以及页面我们改怎么操作呢?

关键时候到了,看清楚了哟:

1.帐户类 User

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace MyBank
  7 {
  8     /// <summary>
  9     /// 帐户类
 10     /// </summary>
 11     public class User
 12     {
 13         #region 成员变量
 14         /// <summary>
 15         /// 帐户姓名
 16         /// </summary>
 17         private string _name;
 18 
 19         public string Name
 20         {
 21             get { return _name; }
 22             set { _name = value; }
 23         }
 24 
 25         /// <summary>
 26         /// 密码
 27         /// </summary>
 28         private string _password;
 29 
 30         public string Password
 31         {
 32             get { return _password; }
 33             set { _password = value; }
 34         }
 35 
 36         /// <summary>
 37         /// 身份证号
 38         /// </summary>
 39         private string _identityNum;
 40 
 41         public string IdentityNum
 42         {
 43             get { return _identityNum; }
 44             set { _identityNum = value; }
 45         }
 46 
 47         /// <summary>
 48         /// 帐户余额
 49         /// </summary>
 50         private double _balance;
 51 
 52         public double Balance
 53         {
 54             get { return _balance; }
 55             set { _balance = value; }
 56         }
 57 
 58         /// <summary>
 59         /// 帐号
 60         /// </summary>
 61         private string _account;
 62 
 63         public string Account
 64         {
 65             get { return _account; }
 66             set { _account = value; }
 67         }
 68         #endregion
 69 
 70         #region 取款
 71         /// <summary>
 72         /// 取款操作
 73         /// </summary>
 74         /// <param name="money">要取的金额</param>
 75         /// <returns>返回余额,输入有误返回-1</returns>
 76         public double MinusMoney(double money)
 77         {
 78             if (money > 0)
 79             {
 80                 if (money <= _balance)
 81                 {
 82                     _balance -= money;
 83                     return _balance;
 84                 }
 85                 else
 86                 {
 87                     return -1;
 88                 }
 89 
 90             }
 91             else
 92             {
 93                 return -1;
 94             }
 95         }
 96         #endregion
 97 
 98         #region 存款
 99         /// <summary>
100         /// 存款
101         /// </summary>
102         /// <param name="money">存款金额</param>
103         public double SaveMoney(double money)
104         {
105             if (money > 0)
106             {
107 
108                 _balance += money;
109                 return _balance;
110             }
111             else
112             {
113                 return -1;
114             }       
115         }
116         #endregion
117 
118     }
119 }

2.银行类 Bank

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace MyBank
  7 {
  8     public class Bank
  9     {
 10         #region 成员变量的定义
 11         User user = new User();  //实例化User对象并赋值
 12         User[] _userGroup = new User[3];
 13         const string MESSAGE = "操作成功!";
 14         #endregion
 15 
 16         #region 初始化三个帐户
 17         /// <summary>
 18         /// 初始化三个帐户
 19         /// </summary>
 20         public void Initial()
 21         {
 22             _userGroup[0] = new User();
 23             _userGroup[0].Account = "179708064356";
 24             _userGroup[0].Name = "王丽丽";
 25             _userGroup[0].Password = "1234";
 26             _userGroup[0].IdentityNum = "210050619890808185";
 27             _userGroup[0].Balance = 1000.1111;
 28 
 29             _userGroup[1] = new User();
 30             _userGroup[1].Account = "179708064359";
 31             _userGroup[1].Name = "张颖颖";
 32             _userGroup[1].Password = "4321";
 33             _userGroup[1].IdentityNum = "510010619891231127";
 34             _userGroup[1].Balance = 2000;
 35 
 36             _userGroup[2] = new User();
 37             _userGroup[2].Account = "179708064368";
 38             _userGroup[2].Name = "刘华";
 39             _userGroup[2].Password = "4567";
 40             _userGroup[2].IdentityNum = "410207198904051271";
 41             _userGroup[2].Balance = 8000;
 42         }
 43         #endregion
 44 
 45         #region 显示所有帐户信息
 46         /// <summary>
 47         /// 显示所有帐户的帐号、姓名、密码、身份证号、帐户余额
 48         /// </summary>
 49         public void ShowAllUser()
 50         {
 51             foreach (User userItem in _userGroup)
 52             {
 53                 Console.WriteLine("帐户姓名:" + userItem.Name + " 帐号:" + userItem.Account + " 存款余额:" + userItem.Balance + " 密码:" + userItem.Password + " 身份证号:" + userItem.IdentityNum);
 54             }
 55         }
 56         #endregion
 57 
 58         #region 显示菜单
 59         /// <summary>
 60         /// 显示菜单
 61         /// </summary>
 62         public void ShowCustomMenu()
 63         {
 64             string option = "";
 65             do
 66             {
 67                 Console.WriteLine();
 68                 Console.WriteLine("==================欢迎使用自动银行服务==================");
 69                 Console.WriteLine("1:开户 2:存款 3:取款 4:转帐 5:查询余额 6:修改密码 0:退出");
 70                 Console.WriteLine("========================================================");
 71                 option = Console.ReadLine();
 72                 switch (option)
 73                 {
 74                     case "1":
 75                         CreateAccount();
 76                         continue;
 77                     case "2":
 78                         Deposit();
 79                         continue;
 80                     case "3":
 81                         WithDraw();
 82                         continue;
 83                     case "4":
 84                         InputTransferData();
 85                         continue;
 86                     case "5":
 87                         ShowBalance();
 88                         continue;
 89                     case "6":
 90                         ModifyPwd();
 91                         continue;
 92                     case "0":
 93                         break;      //结束switch
 94                     default:
 95                         Console.WriteLine("输入无效!");
 96                         continue;
 97                 }
 98 
 99                 break;              //结束do-while循环
100             } while (true);
101         }
102         #endregion
103 
104         #region 开户
105         /// <summary>
106         /// 开户
107         /// </summary>
108         public void CreateAccount()
109         {
110             //接受输入的数据
111             Console.WriteLine("请输入帐户名");
112             user.Name = Console.ReadLine();
113             user.Account = "179708064356";
114             Console.WriteLine("请输入帐户密码");
115             user.Password = Console.ReadLine();
116             Console.WriteLine("请输入帐户身份证号");
117             user.IdentityNum = Console.ReadLine();
118             Console.WriteLine("请输入帐户存款金额");
119             user.Balance = double.Parse(Console.ReadLine());
120 
121             Console.WriteLine("帐户:{0},帐户名:{1},存款金额:{2}创建成功!", user.Account, user.Name, user.Balance);
122             //Console.ReadLine();
123         }
124         #endregion
125 
126          #region 取款
127         /// <summary>
128         /// 取款
129         /// </summary>
130         public void WithDraw()
131         {
132             string account = "";             //帐号
133             string pwd;                      //密码
134 
135             Console.WriteLine("请输入帐号:");
136             account = Console.ReadLine();
137             if (account.Length == 0)
138             {
139                 Console.WriteLine("输入的帐号不正确!");
140                 return;
141             }
142             // 新增:在3个帐户中查找指定的帐户
143             User user = CheckUserByAccount(account);
144             if (user == null)
145             {
146                 Console.WriteLine("输入的帐号不正确!");
147                 return;
148             }
149 
150 
151             //接收帐户密码,并验证
152             Console.WriteLine("请输入帐户密码:");
153             pwd = Console.ReadLine();
154 
155             if (user.Password != pwd)
156             {
157                 Console.WriteLine("密码有误!");
158                 return;
159             }
160 
161             Console.WriteLine("请输入取款金额");
162             double money = double.Parse(Console.ReadLine());
163             double result = user.MinusMoney(money);
164             if (result == -1)
165             {
166                 Console.WriteLine("取款失败");
167             }
168             else
169             {
170                 Console.WriteLine("取款成功!当前余额:" + result);
171             }
172         }
173         #endregion
174    
175 
176         #region 存款
177         /// <summary>
178         /// 存款
179         /// </summary>
180         public void Deposit()
181         {
182             string account = "";               //帐号
183             double money = 0;                //存款金额
184 
185             Console.WriteLine("请输入帐号:");
186             account = Console.ReadLine();
187             Console.WriteLine("请输入存入金额:");
188             money = double.Parse(Console.ReadLine());
189 
190             User user;
191             if ((user = CheckUserByAccount(account)) == null)
192             {
193                 Console.WriteLine("您输入的帐号不存在!");
194             }
195 
196             if (user.SaveMoney(money) > 0)
197             {
198                 Console.WriteLine(MESSAGE + "当前余额:" + user.Balance);
199             }
200             else
201             {
202                 Console.WriteLine("存款失败!");
203             }
204 
205         }
206         #endregion
207 
208         #region 判断帐户是否存在
209         /// <summary>
210         /// 通过帐号判断帐户是否存在
211         /// </summary>
212         /// <param name="account">帐号</param>
213         /// <returns>存在返回user对象,不存在返回null</returns>
214         private User CheckUserByAccount(string account)
215         {
216             foreach (User user in _userGroup)
217             {
218                 if (user.Account == account)
219                 {
220                     return user;
221                 }
222             }
223             return null;
224         }
225   
226         /// <summary>
227         /// 根据帐号和密码判定帐户是否存在
228         /// </summary>
229         /// <param name="account">帐号</param>
230         /// <param name="pwd">密码</param>
231         /// <returns>User & null</returns>
232         private User CheckUser(string account,string pwd)
233         {
234             foreach (User user in _userGroup)
235             {
236                 if (user.Account == account && user.Password ==pwd)
237                 {
238                     return user;
239                 }
240             }
241             return null;
242         }
243         #endregion
244 
245         #region 转帐
246         /// <summary>
247         /// 转帐输入输出信息
248         /// </summary>
249         public void InputTransferData()
250         {
251             Console.WriteLine("请输入转出帐号:");
252             string fromAccount = Console.ReadLine();
253             Console.WriteLine("请输入转出帐户密码:");
254             string fromPwd = Console.ReadLine();
255             Console.WriteLine("请输入转入帐号:");
256             string toAccount = Console.ReadLine();
257             Console.WriteLine("请输入转帐金额:");
258             double money = double.Parse(Console.ReadLine());
259             double fbalance = 0, tbalance = 0;
260 
261             // 执行转帐操作
262             int iRet = Transfer(fromAccount, fromPwd, toAccount, money, ref fbalance, ref tbalance);
263             if (iRet == 1)
264             {
265                 Console.WriteLine("转帐成功,转出帐号{0}的余额为:{1},转入帐号{2}的余额为:{3}", fromAccount, fbalance, toAccount, tbalance);
266             }
267             else if (iRet == -1)
268             {
269                 Console.WriteLine("转出帐户的帐号或密码输入错误!");
270             }
271             else if (iRet == -2)
272             {
273                 Console.WriteLine("转入帐号不正确!");
274             }
275             else if (iRet == -3)
276             {
277                 Console.WriteLine("转帐操作失败!");
278             }
279 
280         }
281 
282         /// <summary>
283         /// 执行转帐操作
284         /// </summary>
285         /// <param name="fAccount">当前帐号</param>
286         /// <param name="fPwd">帐号密码</param>
287         /// <param name="tAccount">转帐帐号</param>
288         /// <param name="money">转帐金额</param>
289         /// <param name="fBalance">当前帐号余额</param>
290         /// <returns>-1:帐号或密码输入错误;1:转帐成功;0:转帐帐号不存在</returns>
291         private int Transfer(string fAccount, string fPwd, string tAccount, double money, ref double fBalance, ref double tBalance)
292         {
293             //检查转出帐号和密码
294             User userfrom = CheckUser(fAccount, fPwd);
295             if (userfrom == null)   //转出帐户和密码不正确
296             {
297                 return -1;
298             }
299             //检查转入帐号
300             User userTo = CheckUserByAccount(tAccount);
301             if (userTo == null)     //转入帐号不正确
302             {
303                 return -2;
304             }
305 
306             //取款
307             if (userfrom.MinusMoney(money) == -1)   //转帐操作失败
308             {
309                 return -3;
310             }
311             //取得余额
312             fBalance = userfrom.Balance;
313 
314             //存款
315             if (userTo.SaveMoney(money) == -1)     //转帐操作失败
316             {
317                 return -3;
318             }
319 
320             tBalance = userTo.Balance;
321 
322             return 1;
323 
324         }
325         #endregion
326    
327 
328         #region 显示帐户余额
329         /// <summary>
330         /// 显示帐户余额
331         /// </summary>
332         public void ShowBalance()
333         {
334             string accountName = "";    //当前帐号
335             string password = "";       //当前帐户密码
336             Console.WriteLine("请输入帐号");
337             accountName = Console.ReadLine();
338             Console.WriteLine("请输入密码");
339             password = Console.ReadLine();
340             User user = CheckUser(accountName, password);
341             if (user == null)
342             {
343                 Console.WriteLine("当前帐号不存在");
344             }
345             else
346             {
347                 Console.WriteLine("帐户的余额是:" + user.Balance.ToString ("0.00"));
348             }
349         }
350         #endregion
351 
352         #region 修改密码
353         public void ModifyPwd()
354         {
355             string accountName = "";    //当前帐号
356             string password = "";       //当前帐户密码
357             Console.WriteLine("请输入帐号");
358             accountName = Console.ReadLine();
359             Console.WriteLine("请输入原密码");
360             password = Console.ReadLine();
361             
362             string newPwd1, newPwd2;
363             Console.WriteLine("请输入新密码");
364             newPwd1 = Console.ReadLine();
365             Console.WriteLine("请再次输入新密码");
366             newPwd2 = Console.ReadLine();
367 
368             if(CheckPwd(accountName, password, newPwd1, newPwd2)==true)
369                 Console.WriteLine("帐户密码修改成功!");
370             else
371                 Console.WriteLine("帐户密码修改失败!");
372 
373         }
374 
375         public bool CheckPwd(string accountName, string password, string newPwd1, string newPwd2)
376         {
377             User user = CheckUser(accountName, password);
378             if (user == null)
379             {
380                 Console.WriteLine("当前帐号不存在");
381                 return false;
382             }
383 
384             if (newPwd1 != newPwd2)
385             {
386                 Console.WriteLine("两只输入的帐户密码不一致!");
387                 return false;
388             }
389             else
390             {
391                 user.Password = newPwd1;
392                 return true;
393             }            
394         }
395         #endregion
396     }
397 
398 }

 

3.最关键的也是最后的了,Program类, 关于系统运行

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyBank
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Bank myBank = new Bank();
13 
14             //开户
15             //myBank.CreateAccount();
16 
17             //初始化数据
18             myBank.Initial();
19 
20             //显示所有帐户
21             myBank.ShowAllUser();
22 
23             //显示菜单
24             myBank.ShowCustomMenu();
25 
26             //取款
27             //myBank.WithDraw();
28         }
29     }
30 }

好了,以上就是MyBank银行管理系统了哟

posted @ 2018-07-13 09:01  久孤丶  阅读(1046)  评论(0编辑  收藏  举报