问题描述:
用C#写一个ATM机相关程序!!(自己想的题目,其他语言可以直接改部分代码即可!)

大概也就分为开户、登录、存取款、查询、退出等等功能

问题分析:

大概也就用到了数据库相关知识

示例代码
主函数:

~~~ javascript
// Program.cs
// ATM项目中的Program类源文件

using System;
using System.Collections.Generic;
using System.Text;

namespace ATM
{
    class Program
    {
        static void Main(string[] args)
        {
            Bank bank = new Bank("XYZ银行");
            ATM atm = new ATM(bank);
            atm.Start();
        }
    }
}

银行相关业务逻辑:

// Bank.cs
// ATM项目中的Bank类源文件

using System;
using System.Collections;
using System.Text;

namespace ATM
{
    class Bank
    {
        protected const int MaxAccountNum = 2048;
        protected string name;
        protected ArrayList accounts;

        public string Name
        {
            get
            {
                return name;
            }
        }

        public Bank(string name)
        {
            this.name = name;
            accounts = new ArrayList();
        }
        public bool LoginAccount(string name, string password, out Account account)
        {
            account = null;
            foreach (Account acc in accounts)
            {
                if (acc.Login(name, password))
                {
                    account = acc;
                    return true;
                }
            }
            return false;
        }
        public bool OpenAccount(string name, string password, out Account account)
        {
            account = null;
            foreach (Account acc in accounts)
            {
                if (acc.Name == name)
                    return false;
            }
            account = new Account(name, password);
            accounts.Add(account);
            return true;
        }
    }
}

用户相关业务逻辑

// Account.cs
// ATM项目中的Account类源文件

using System;
namespace ATM
{
    class Account
    {
        protected string name;
        protected string password;
        protected decimal balance;

        public decimal Balance
        {
            get
            {
                return balance;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
        }

        public Account(string name, string password)
        {
            this.balance = 0;
            this.name = name;
            this.password = password;
        }
        public bool Deposit(decimal amount)
        {
            if (amount <= 0)
                return false;
            balance += amount;
            return true;
        }
        public bool Deposit(double amount)
        {
            return Deposit((decimal)amount);
        }
        public bool Deposit(int amount)
        {
            return Deposit((decimal)amount);
        }
        public bool Deposit(decimal amount, out decimal balance)
        {
            bool succeed = Deposit(amount);
            balance = this.balance;
            return succeed;
        }
        public bool Withdraw(decimal amount)
        {
            if (amount > balance || amount <= 0)
                return false;
            balance -= amount;
            return true;
        }
        public bool Withdraw(double amount)
        {
            return Withdraw((decimal)amount);
        }
        public bool Withdraw(int amount)
        {
            return Withdraw((decimal)amount);
        }
        public bool Withdraw(decimal amount, out decimal balance)
        {
            bool succeed = Withdraw(amount);
            balance = this.balance;
            return succeed;
        }
        public bool ChangePassword(string oldPassword, string newPassword)
        {
            if (oldPassword != password)
                return false;
            password = newPassword;
            return true;
        }
        public bool Login(string name, string password)
        {
            return (this.name == name && this.password == password);
        }
    }
}

ATM机相关功能

// ATM.cs
// ATM项目中的ATM类源文件

using System;
using System.Collections.Generic;
using System.Text;

namespace ATM
{
    class ATM
    {
        private const string quitCode = "20190506";
        private Bank bank;
        public ATM(Bank bank)
        {
            this.bank = bank;
        }
        public void Start()
        {
            while (true)
            {
                Console.Clear();
                // 主界面
                PrintLogo();
                Console.WriteLine("       1. 开户                                ");
                Console.WriteLine("       2. 登录                                ");
                Console.WriteLine("----------------------------------------------");
                Console.WriteLine("");
                Console.Write("你的选择(回车结束):");
                string code = Console.ReadLine();

                // quit system
                if (code == quitCode)
                    return;


                if (code == "1")            // 开户
                    OpenAccount();
                else if (code == "2")       // 登陆
                    LoginAccount();
            }
        }
        private void LoginAccount()
        {
            Console.Clear();
            PrintLogo();
            Console.WriteLine("       请输入你的帐号的用户名和密码           ");
            Console.WriteLine("----------------------------------------------");
            Console.WriteLine("");
            string name = Input("用户名(回车结束):");
            string password = Input("密码(回车结束):");

            // 登录帐号
            Account account;
            if (!bank.LoginAccount(name, password, out account))
            {
                Console.Write("登录错误,请检查用户名和密码是否正确。按Enter键继续...");
                Console.Read();
            }
            else
            {
                ManageAccount(ref account);
            }
        }
        private void OpenAccount()
        {
            Console.Clear();
            PrintLogo();
            Console.WriteLine("       请输入你的帐号的用户名和密码           ");
            Console.WriteLine("----------------------------------------------");
            Console.WriteLine("");
            string name = Input("用户名(回车结束):");
            string password = Input("密码(回车结束):");

            // 开户
            Account account;
            if (!bank.OpenAccount(name, password, out account))
            {
                Console.Write("开户错误,用户名可能已经存在。按Enter键继续...");
                Console.Read();
            }
            else
            {
                Print("开户", 0, account);
                Pause();
                ManageAccount(ref account);
            }
        }
        private void ManageAccount(ref Account account)
        {
            
            while (true)
            {Console.Clear();
                // 管理帐号界面
                PrintLogo();
                Console.WriteLine("       1. 存款                                ");
                Console.WriteLine("       2. 取款                                ");
                Console.WriteLine("       3. 查询余额                            ");
                Console.WriteLine("       4. 修改密码                            ");
                Console.WriteLine("       5. 退出                                ");
                Console.WriteLine("----------------------------------------------");
                Console.WriteLine("");
                Console.Write("你的选择(回车结束):");
                string code = Console.ReadLine();

                decimal amount;
                bool succeed;
                switch (code)
                {
                    case "1":
                        amount = InputNumber("\n输入存款数目:");
                        succeed = account.Deposit(amount);
                        if (succeed)
                        {
                            Print("存入", amount, account);
                        }
                        else
                        {
                            Console.WriteLine("存款失败!");
                        }
                        Pause();
                        break;
                    case "2":
                        amount = InputNumber("\n输入取款数目:");
                        succeed = account.Withdraw(amount);
                        if (succeed)
                        {
                            Print("取出", amount, account);
                        }
                        else
                        {
                            Console.WriteLine("取款失败!");
                        }
                        Pause();
                        break;
                    case "3":
                        Print(account);
                        Pause();
                        break;
                    case "4":
                        string oldPassword = Input("当前密码(回车结束):");
                        string newPassword = Input("新密码(回车结束):");
                        succeed = account.ChangePassword(oldPassword, newPassword);
                        if (succeed)
                            Console.WriteLine("密码修改成功!");
                        else
                            Console.WriteLine("密码修改失败!");
                        Pause();
                        break;
                    case "5":
                        return;
                    default:
                        break;
                }

            }
        }
        private void PrintLogo()
        {
            Console.WriteLine("\n----------------------------------------------");
            Console.WriteLine("  {0}自动取款机       用户第一  服务至上 ", bank.Name);
            Console.WriteLine("----------------------------------------------");
        }
        private string Input(string prompt)
        {
            string str="";
            int count = 0;
            Console.Write(prompt);
            if (prompt == "密码(回车结束):")
            {
                while (true)
                {
                    
                    ConsoleKeyInfo ck = Console.ReadKey(true);
                    if (ck.Key != ConsoleKey.Enter)
                    {
                        if (ck.Key != ConsoleKey.Backspace)
                        {
                            str += ck.KeyChar.ToString();
                            Console.Write("*");
                            count++;
                        }
                        else
                        {
                            Console.Write("\b\b");
                        }

                    }
                    else {
                        Console.WriteLine();
                        break;
                    }
                }
                if (count != 6)
                {
                    Console.Write("密码必须是六位数,请重新输入!");
                    int c = Console.Read();
                    str = Input("密码(回车结束):");
                }

            }
            else { 
            str = Console.ReadLine();
            while (str == "")
            {
                Console.Write("不能为空,{0}", prompt);
                str = Console.ReadLine();
            }
          
            }  return str;
            
        }
        private decimal InputNumber(string prompt)
        {
            Console.Write(prompt);
            string s = Console.ReadLine();
            decimal amount = 0;
            try
            {
                amount = Decimal.Parse(s);
            }
            catch (Exception)		// 捕获任何异常
            {
                Console.Write("输入的数值格式不正确,请重新输入!"); // 提示错误
                amount = InputNumber(prompt);	// 递归调用InputNumber
            }
            return amount;
        }
        private void Pause()
        {
            Console.Write("按Enter键继续...");
            Console.Read();
        }
        private void Print(string operation, decimal amount, Account account)
        {

            Console.WriteLine("---------------------------");
            Console.WriteLine("姓名: {0}", account.Name);
            Console.WriteLine("{0}: {1}", operation, amount);
            Console.WriteLine("余额: {0}", account.Balance);
            Console.WriteLine("---------------------------");
            Console.WriteLine("{0}成功!", operation);
        }
        public void Print(Account account)
        {
            Console.WriteLine("---------------------------");
            Console.WriteLine("姓名: {0}", account.Name);
            Console.WriteLine("余额: {0}", account.Balance);
            Console.WriteLine("---------------------------");
        }
    }
}