1. 程式人生 > >C#基礎-031 模擬ATM機存取款系統

C#基礎-031 模擬ATM機存取款系統

/*
 * 登入
 * 存款
 * 取款
 * 查詢餘額
 * 查詢交易記錄
 * 切換賬戶
 */
namespace ATM
{
    class Program
    {
        //使用者陣列和密碼陣列索引一樣的表示為一組
        //使用者陣列
        static string[] usernameArr = { "admin", "yy" };
        //密碼陣列
        static string[] passwordArr = {"888888","123456" };
        //金額
        static int[] moneyArr = {10000
,20000 }; static string[] recordArr = { "", "" }; //記錄操作的賬戶索引 static int index = 0; static void Main(string[] args) { while (true) { Console.Clear(); string ret = Input("登入請按0"); if (ret == "0") { bool
result = Login(); if (result) { WelcomeUI(); Operation(); } } } } #region //歡迎頁面 static void WelcomeUI() { Console.Clear(); Console.WriteLine("****************************"
); Console.WriteLine("****** 歡迎您{0}使用 *****",usernameArr[index]); Console.WriteLine("****** ATM機 *******"); Console.WriteLine("****************************"); } #endregion #region//功能項 static void Operation() { Lable: string ret = Input("1.存款\n2.取款\n3.查詢餘額\n4.查詢交易記錄\n5.退出"); switch (ret) { case "1": int saveMoney = int.Parse(Input("請輸入存款金額:")); SaveMoney(saveMoney); goto Lable; case "2": int drawMoney = int.Parse(Input("請輸入取款金額:")); DrawMoney(drawMoney); goto Lable; case "3": QuaryLeftMoney(); goto Lable; case "4": QuaryRecord(); goto Lable; case "5": //Exit(); break; default: Console.WriteLine("輸入不合法"); goto Lable; } } #endregion #region //判斷賬戶是否存在 static bool IsContains(string username,string password) { for (int i = 0; i < usernameArr.Length; i++) { if (usernameArr[i] == username && passwordArr[i] == password) { index = i; return true; } } return false; } #endregion #region //登入 static bool Login() { string username = string.Empty; string password = string.Empty; //記錄登入失敗的次數 int count = 0; do { username = Input("請輸入使用者名稱"); password = Input("請輸入密碼:"); if (IsContains(username,password)) { return true; } else { count++; if (count>=3) { Console.WriteLine("對不起,您的賬戶已經被凍結!"); return false; } else { Console.WriteLine("對不起,您輸入的賬戶名或密碼錯誤,請重新輸入"); } } } while (true); } #endregion #region//輸入函式 static string Input(string str) { Console.WriteLine(str); return Console.ReadLine(); } #endregion #region //存款 static void SaveMoney(int saveMoney) { moneyArr[index] += saveMoney; //記錄一下交易記錄 WriteRecord(DateTime.Now +"存款"+saveMoney+"元人民幣"); } #endregion #region//取款 static void DrawMoney(int drawMoney) { if (moneyArr[index]<drawMoney) { Console.WriteLine("您的餘額不足"); return; } moneyArr[index] -= drawMoney; //記錄一下交易記錄 WriteRecord(DateTime.Now + "取款" + drawMoney + "元人民幣"); } #endregion #region//查詢餘額 static void QuaryLeftMoney() { Console.WriteLine("您的賬戶餘額為:{0}",moneyArr[index]); } #endregion #region//查詢交易記錄 static void QuaryRecord() { Console.WriteLine("您的交易記錄為:{0}", recordArr[index]); } #endregion #region //寫交易記錄 static void WriteRecord(string str) { recordArr[index] += str+"\n"; } #endregion #region //退出 static void Exit() { Environment.Exit(0); } #endregion } }