c#控制台应用程序ATM银行操作实例。主要介绍了设计的方法;使用的类介绍;具体的运行界面;程序代码。代码直接写在一起放在Programm.cs中,拷贝可直接运行。

一、设计

1、原则上采用三层:(1)操作界面层;(2)银行管理中间层;(3)底层账户

2、操作界面层 到 银行中间层 到底层账户

二、三个类

1、ATM类:界面类,显示各种操作界面

2、Bank类:银行账号存储及操作,如删除账户、寻找账号、获得所有账号等等

3、Account类:存储某个账户的数据及操作,如收入、支出、获得余额、获得所有存取记录

三、主要运行界面

四、具体代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;

namespace ATMApp
{  
    [Serializable]
    
    class ATM
    {
        public static Bank icbcbank = new Bank("ICBC");
        
        static void Main(string[] args)
        {
            LoadIcbcBankInfo();
            string code = "";
            do
            {
                Console.Clear();
                Console.Title = "ATM";
                Console.WriteLine("    " + icbcbank.bankName + "模拟ATM");
                Console.WriteLine("---------------------");
                Console.WriteLine("1.登录 2.开户 0.退出");
                Console.WriteLine("--------------------");
                Console.Write("请选择:");
                code = Console.ReadLine();
                if (code == "1")
                    ATMLoginAccountUI();
                else if (code == "2")
                    ATMOpenAccountUI();
                else if (code == "3")
                {
                    foreach (Account account in icbcbank.GetAllAccounts())
                    {
                        Console.WriteLine(account.accountNo);
                    }
                    Console.ReadKey();
                }
            } while (code != "0");
            SaveIcbcBankInfo();
            Console.WriteLine("按一下任意键退出系统...");
        }
        
        public static void ATMLoginAccountUI()
        {
            Console.Clear();
            Console.WriteLine("登录");
            Console.WriteLine("--------------");
            Console.Write("输入你的账号:");
            string accNo = Console.ReadLine();
            Console.Write("输入你的密码:");
            string pwd = Console.ReadLine();
            if (!icbcbank.LoginAccount(accNo, pwd))
            {
                Console.WriteLine("无账号或密码不对!");
                Console.ReadKey();
                return;          //返出该方法
            }
            else
            {
                ShowMenuUI(accNo);
            }
        }
        
        public static void ShowMenuUI(string accNo)
        {
            do
            {
                Console.Clear();
                Console.WriteLine("   交易菜单");
                Console.WriteLine("--------------");
                Console.WriteLine("账号:" + accNo);
                Console.WriteLine("--------------");
                Console.WriteLine("1.查余额");
                Console.WriteLine("2.取  款");
                Console.WriteLine("3.存  款");
                Console.WriteLine("4.转  账"); // 转账功能还没有做
                Console.WriteLine("5.查交易");
                Console.WriteLine("6.改密码");
                Console.WriteLine("7.销账号");
                Console.WriteLine("0.退  出");
                Console.WriteLine("---------------");
                Console.Write("请选择:");
                switch (Console.ReadLine())
                {
                    case "1":
                        DisplayBalanceUI(accNo);
                        break;
                    case "2":
                        WithdrawUI(accNo);
                        break;
                    case "3":
                        depositUI(accNo);
                        break;
                    case "6":
                        ChangePasswordUI(accNo);
                        break;
                    case "5":
                        QueryAccountTransUI(accNo);
                        break;
                    case "7":
                        DelAccountUI(accNo);
                        break;
                    case "0":
                        return;
                }
            } while (true);
        }
        
        public static void DelAccountUI(string AccNo)
        {
            Console.Write("是否真的要销户(y/n)?");
            string answer = Console.ReadLine();
            if ("y" == answer)
            {
                if (!icbcbank.DelAccountById(AccNo))
                {
                    Console.WriteLine("账户余额为不为0,不能销户!");
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("销户成功,欢迎再来!");
                    Console.ReadKey();
                    SaveIcbcBankInfo();//别忘,因为是非正常退出应用程序                  
                    Environment.Exit(0);//直接强制退出应用程序.有点暴力哦,哈哈哈—老猪新作。
                }
            }
        }
        
        public static void QueryAccountTransUI(string AccNo)
        {
            Console.WriteLine("\t   所有交易记录");
            Console.WriteLine("\t   ——————");

            Console.WriteLine("{0,-14}\t{1,-10}", "交易时间", "  交易金额");
            Console.WriteLine();
            foreach (var item in icbcbank.GetAccountRecords(AccNo))
            {
                Console.WriteLine("{0,-14}\t{1,10}", item.Key.ToString("yyyy/MM/dd HH:mm"), item.Value.ToString("+#.##;-#.##;0"));        //大写HH是24小时制
                Console.WriteLine("—————————————————");
            }
            Console.WriteLine("{0,-14}\t{1,10}", "目前余额", icbcbank.GetBalanceByAccountNo(AccNo).ToString("+#.##;-#.##;0"));

            Console.ReadKey();
        }
        
        public static void ChangePasswordUI(string AccNo)
        {
            Console.Write("输入老的密码:");
            string oldpwd = Console.ReadLine();
            Console.Write("输入新的密码:");
            string newpwd = Console.ReadLine();
            if (icbcbank.ChangePasword(AccNo, oldpwd, newpwd))
            {
                Console.WriteLine("成功修改");
            }
            else
                Console.WriteLine("成功不修改");
            Console.ReadKey();
        }
        
        public static void depositUI(string AccNo)
        {
            Console.Write("输入存款金额:");
            double amount = Convert.ToDouble(Console.ReadLine());
            if (!icbcbank.DepositAccount(AccNo, amount))
            {
                Console.WriteLine("存款不成功");
            }
            else
                Console.WriteLine("存款成功");
            Console.ReadKey();
        }
        
        public static void WithdrawUI(string AccNo)
        {
            Console.Write("输入取款金额:");
            double amount = Convert.ToDouble(Console.ReadLine());
            if (!icbcbank.WithdrawAccount(AccNo, amount))
            {
                Console.WriteLine("取款不成功,余额可能不足");
            }
            else
                Console.WriteLine("取款成功!,取款{0}元,余额{1}元", amount, icbcbank.GetBalanceByAccountNo(AccNo));
            Console.ReadKey();
        }
       
        public static void DisplayBalanceUI(string AccNo)
        {
            Console.Write("账户余额为:" + icbcbank.GetBalanceByAccountNo(AccNo));
            Console.ReadKey();
        }
        
        public static void ATMOpenAccountUI()
        {
            Console.Clear();
            Console.WriteLine("开户");
            Console.WriteLine("--------------");
            Console.Write("你要的账号:");
            string accNo = Console.ReadLine();
            Console.Write("你设的密码:");
            string pwd = Console.ReadLine();
            if (accNo == "" || pwd == "")
            {
                Console.WriteLine("用户名密码不能为空,开户失败!");
                Console.ReadKey();
                return;
            }
            if (!icbcbank.OpenAccount(accNo, pwd))
            {
                Console.WriteLine("可能已经存在,开户错误!");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("开户成功!");
            Console.ReadKey();
            ShowMenuUI(accNo);
        }
        
        static void LoadIcbcBankInfo()
        {
            ///1、文件存在不
            ///2、读文件
            ///3、二进制
            ///4、反序列读到当前静态银行bank
            if (File.Exists("bank.dat"))
            {
                FileStream fs = new FileStream("bank.dat", FileMode.Open, FileAccess.Read);
                BinaryFormatter bf = new BinaryFormatter();
                icbcbank = bf.Deserialize(fs) as Bank;
                fs.Close();
            }
        }
        
        static void SaveIcbcBankInfo()
        {
            ///1、建立bank
            ///2、二进制化
            ///3、序列化
            FileStream fs = new FileStream("bank.dat", FileMode.Create, FileAccess.Write);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, icbcbank);
            fs.Close();
        }
    }
    
    class Bank
    {
        public string bankName;         //存放银行对象名称变量
        public ArrayList accounts;      //用ArrayList类型,存放银行所有账户对象变量
        public double rateinterest;

        public Bank(string bankname)
        {
            this.bankName = bankname;
            accounts = new ArrayList();
            rateinterest = 0.005;
        }

        public bool OpenAccount(string id, string pwd)
        {
            foreach (Account acc in accounts)
            {
                if (acc.accountNo == id)
                {
                    return false;
                }
            }
            Account account = new Account(id, pwd);
            accounts.Add(account);
            return true;
        }

        public ArrayList GetAllAccounts()
        {
            return accounts;
        }
        
        public Account Find(string AccNo)
        {
            foreach (Account item in accounts)
            {
                if (item.accountNo == AccNo)
                    return item;
            }
            return null;
        }

        internal bool LoginAccount(string AccNo, string pwd)
        {
            Account account = Find(AccNo);
            if (account != null && account.Login(AccNo, pwd))  //如果账号null。所以加account!=null
                return true;
            else
                return false;
        }

        public double GetBalanceByAccountNo(string AccountNo)
        {
            Account account = Find(AccountNo);
            return account.GetBalance();
        }

        internal bool WithdrawAccount(string AccNo, double amount)
        {
            Account account = Find(AccNo);
            return account.Withdraw(amount);
        }

        internal bool DepositAccount(string AccNo, double amount)
        {
            Account account = Find(AccNo);
            return account.Deposit(amount);
        }

        internal bool ChangePasword(string accNo, string oldpwd, string newpwd)
        {
            Account account = Find(accNo);
            return account.ChangePasword(oldpwd, newpwd);
        }

        internal Dictionary<DateTime, double> GetAccountRecords(string accNo)
        {
            Account account = Find(accNo);
            return account.GetRecords();
        }

        internal bool DelAccountById(string AccNo)
        {
            Account account = Find(AccNo);
            if (account.GetBalance() == 0)
            {
                accounts.Remove(account);
                return true;
            }
            return false;
        }
    }

    class Account
    {
        public string accountNo;
        public string password;
        public double balance;
        public DateTime createdTime;
        public Dictionary<DateTime, double> dicRecords; //存放账户所有交易记录字段:交易(存或取)一次记一次

        public Account(string no, string pwd)
        {
            accountNo = no;
            password = pwd;
            balance = 0;
            createdTime = DateTime.Now;
            dicRecords = new Dictionary<DateTime, double>();
            dicRecords[createdTime] = balance;//下面也可以 dicRecords.Add(createdTime,balance);//开户
        }

        public bool Withdraw(double amount)
        {
            if (amount > balance || amount <= 0)
            {
                return false;
            }
            balance -= amount;
            dicRecords.Add(DateTime.Now, -amount);
            return true;
        }

        public bool Deposit(double amount)
        {
            if (amount < 0)
                return false;
            else
            {
                balance += amount;
                dicRecords.Add(DateTime.Now, +amount);
                return true;
            }
        }

        public double GetBalance()
        {
            return balance;
        }

        public bool Login(string no, string pwd)
        {
            if (this.accountNo == no && this.password == pwd)
                return true;
            else
                return false;
        }

        public bool ChangePasword(string oldPwd, string newPwd)
        {
            if (oldPwd != password)
            {
                return false;
            }
            password = newPwd;
            return true;
        }

        public Dictionary<DateTime, double> GetRecords()
        {
            return dicRecords;
        }
    }
}

最新文章

  1. label和input里面文字不对齐的解决方法!
  2. JavaScript 详说事件机制之冒泡、捕获、传播、委托
  3. 不支持C++11 decltype的噩耗
  4. PAT 1015. 德才论 (25)
  5. 彻底禁止QQ更新
  6. 未能找到Microsoft.Office.Core.MsoTriState的引用
  7. 使用openxml读取xml数据
  8. Android 获取WIFI MAC地址的方法
  9. Core Data 教学
  10. BZOJ 2879 NOI2012美食节
  11. 查看当前支持的shell,echo -e相关转义符,一个简单shell脚本,dos2unix命令把windows格式转为Linux格式
  12. Linux 快速执行历史命令,用 !编号
  13. Django Middleware简介
  14. poj 3268 Silver Cow Party(最短路dijkstra)
  15. mysql查询正在执行的sql
  16. asp.net mvc 3.0 知识点整理 ----- (4).asp.net mvc 3 和asp.net mvc 4 对比
  17. Nginx Log日志统计分析常用命令
  18. javaScript中ajax、axios总结
  19. influxDB硬件配置指南
  20. 蓝桥杯 算法提高 3000米排名预测 DFS 递归搜索 next_permutation()使用

热门文章

  1. 基于ERNIELayout&amp;pdfplumber-UIE的多方案学术论文信息抽取
  2. tempdb日志文件暴增分析
  3. 如何用 30s 给面试官讲清楚什么是 Session-Cookie 认证
  4. 【带你读论文】向量表征经典之DeepWalk
  5. Codeforces Round #844 (Div.1 + Div.2) CF 1782 A~F 题解
  6. 1. 使用 fluent-bit 采集文件
  7. 如何解决github下载很慢的问题?(已经解决)
  8. Flutter 3.7 新特性:介绍后台isolate通道
  9. VUEX 使用学习五 : getter
  10. get请求与post请求的区别