面向对象基础之基础—控制台C#模拟银行ATM存取操作实例

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;
        }
    }
}

复制代码

 

posted @   波波猪(bobchu)  阅读(461)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示