欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#基础-031 模拟ATM机存取款系统

程序员文章站 2022-07-09 21:42:49
...
/*
 * 登录
 * 存款
 * 取款
 * 查询余额
 * 查询交易记录
 * 切换账户
 */
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
    }
}
相关标签: 密码