自助取款机系统(C#)

ATM(自动取款机)系统向用户提供一个方便、简单、及时、随时随地可以随心所欲存取款的、互联的、计算机化的网络系统 ,可以大大减少工作人员,节约人力资源的开销;同时,由于手续减少,减轻了业务员的工作负担,有效地提高了整体的工作效率和精确度,减少了用户办理业务的等待时间;用户可以随时随地存取款,并且操作简单;用户还可以自主选择在柜台办理业务或自己在自助取款机办理业务。

打开Visual Studio 2022,点击创建新项目。

 在创建新项目中,选择C#--Windows--控制台,选择控制台应用(.NET Framework) ,点击下一步。

配置新项目,在项目名称是Automatic-Teller-Machine,然后点击创建。

创建新项目以后:

1、帐号类

帐号类(Account)包含所有的账号信息,负责所有的账号操作。基本的帐号信息包括账号名(name)、账号密码(password)、账号余额(balance)。主要的账号操作包括登录(Login)、存款(Deposit)、取款(Withdraw)、查询余额(Get_Balance)、修改密码(ChangePassword)。

鼠标指针放在Automatic-Teller-Machine的字样,点击右键,选择“添加”,再选择“类”。

 在添加新项上,名称修改,输入是Account.cs,点击添加。

 添加新项之后:

 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automatic_Teller_Machine
{
    internal class Account
    {
        protected string name;
        protected string password;
        protected decimal balance;
        public decimal Banlance
        {
            get
            {
                return balance;
            }
        }
        public string Name
        {
            get 
            { 
                return name; 
            }
        }

        //构造函数
        public Account(string name, string password)
        {
            this.name = name;
            this.password = password;
            this.balance = 0;
        }
        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);
        }

        

    }
}

2、银行类

银行类(Bank)的本质就是一组账号的组合,并负责管理账号。基本的银行信息包括银行名(name)、已经开户的账号数(usedAccountNum)、可以容纳的最大账户数(MaxAccountNum)、账号集(accounts)。主要的银行操作包括开户(OpenAccount)、登录账号(LoginAccount)。

鼠标指针放在Automatic-Teller-Machine的字样,点击右键,选择“添加”,再选择“类”。

在添加新项上,名称修改,输入是Bank.cs,点击添加。

添加新项之后:

  代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automatic_Teller_Machine
{
    internal class Bank
    {
        protected string name;
        protected const int MaxAccountNum = 2048;
        protected int usedAccountNum;
        protected Account[] accounts;
        public string Name
        {
            get
            {
                return name;
            }
        }
        public Bank(string name)
        {
            this.name = name;
            this.usedAccountNum = 0;
            accounts= new Account[MaxAccountNum];
        }
        public bool LoginAccount(string name,string password, out Account account)
        {
            account= null;
            for(int i=0;i<usedAccountNum;++i)
            {
                if (accounts[i].Login(name,password))
                {
                    account = accounts[i];
                    return true;
                }
            }
            return false;
        }
        public bool OpenAccount(string name,string password,out Account account)
        {
            account = null;
            for(int i=0;i<usedAccountNum;++i)
            {
                if (accounts[i].Name==name)
                {
                    return false;
                }
            }
            account = new Account(name, password);
            accounts[usedAccountNum++] = account;
            return true;
        }

    }
}

3、ATM类

ATM类与银行类之间存在一对一的关联关系,ATM提供用户界面,并将用户的请求提交给银行,将银行的反馈提交给用户。主要的ATM操作包括启动(Start)、开户(OpenAccount)、登录账号(LoginAccount)、管理账号(ManageAccount)、一些显示不同信息的辅助操作(Print、Pause等)。

鼠标指针放在Automatic-Teller-Machine的字样,点击右键,选择“添加”,再选择“类”。

在添加新项上,名称修改,输入是ATM.cs,点击添加。

 添加新项之后:

 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automatic_Teller_Machine
{
    internal class ATM
    {
        private const string quitcode = "5";
        private Bank bank;
        public ATM(Bank bank)
        {
            this.bank = bank;
        }
        //开始页面
        public void Start()
        {
            while(true)
            {
                //主界面
                Console.WriteLine();
                Console.WriteLine("  ******************************");
                Console.WriteLine("  *-----------1.开户-----------*");
                Console.WriteLine("  *-----------2.登录-----------*");
                Console.WriteLine("  *-----------3.退出-----------*");
                Console.Write("请输入您的选择(回车结束):");
                string code=Console.ReadLine();
                if(code==quitcode)
                { 
                    return;
                }
                if(code=="1")
                {
                    OpenAccount();
                }
                else if(code=="2")
                {
                    LoginAccount();
                }
                else if(code=="3")
                {
                    Console.WriteLine("按一下任意键直接退出...");
                    Console.ReadKey();
                    return;
                }
            }
        }

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

        }
        //开户方法
        private void OpenAccount()
        {
            Console.WriteLine("  =====================================");
            Console.WriteLine("  *   请输入您的帐号的用户名和密码    *");
            Console.WriteLine("  =====================================");
            string name = Input("用户名(回车结束):");
            string password = Input("密  码(回车结束):");
            //登录账号
            Account account;
            if (!bank.OpenAccount(name, password, out account))
            {
                Console.WriteLine("  *开户错误,用户名和密码已经存在。按Enter键继续---*");
                Console.Read();
            }
            else
            {
                Print("开户",0,account);
                ManageAccount(ref account);
            }

        }

        //账号管理方法
        private void ManageAccount(ref Account account)
        {
            Console.Clear();
            Console.WriteLine("  ******************************");
            Console.WriteLine("  *-----------1.存款------------");
            Console.WriteLine("  *-----------2.取款------------");
            Console.WriteLine("  *-----------3.查询余额--------");
            Console.WriteLine("  *-----------4.修改密码--------");
            Console.WriteLine("  *-----------5.回到主页--------");
            Console.WriteLine("  ******************************");
            Console.Write("您的选择是(回车结束):");
            while(true)
            {
                //管理账号界面
                Console.WriteLine("");
                string code = Console.ReadLine();
                //string s:
                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":
                        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":
                        Console.Clear();
                        break;
                    default:
                        break;
                }                    
            }
        }
        //输出信息

        private string Input(string prompt)
        {
            Console.Write(prompt);
            string str=Console.ReadLine();
            while(str=="")
            {
                Console.Write("不能为空, {0}",prompt);
                str= Console.ReadLine();
            }
            return str;
        }
        private decimal InputNumber(string prompt)
        {
            Console.WriteLine(prompt);
            string s=Console.ReadLine();
            decimal amount = Decimal.Parse(s);
            return amount;
        }

        //打印辅助信息
        private void Pause()
        {
            Console.WriteLine("按Enter继续...");
            Console.Read();
        }

        //打印信息
        private void Print(string operation,decimal amount,Account account)
        {
            Console.WriteLine("  =====================================");
            Console.WriteLine("  *姓名:"+account.Name);
            Console.WriteLine("  *"+operation+":"+amount);
            Console.WriteLine("  *余额:" + account.Banlance);
            Console.WriteLine("              " + operation + "成功");
            Console.WriteLine("  =====================================");
        }
        private void Print(Account account)
        {
            Console.WriteLine("  =====================================");
            Console.WriteLine("  *姓名:{0}" , account.Name);
            Console.WriteLine("  *余额:{0}", account.Banlance);
            Console.WriteLine("  =====================================");
        }
    }
}

4、启动程序类

启动程序类(Program)的唯一功能就是创建银行类和ATM类的实例,并将它们关联起来,然后启动ATM(执行ATM对象的Start方法)。因此,该类仅仅是包含程序 的入口点Main方法。

鼠标指针放在Automatic-Teller-Machine的字样,点击右键,选择“添加”,再选择“类”。

在添加新项上,名称修改,输入是Account.cs,点击添加。

 添加新项之后:

 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automatic_Teller_Machine
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("  ******************************");
            Console.WriteLine("  *      欢迎登录中国银行      *");
            Console.WriteLine("  ******************************");
            //实例化Bank类
            Bank bank = new Bank("ATM自助取款机");
            //实例化ATM类
            ATM atm=new ATM(bank);
            atm.Start();
        }
    }
}

  • 16
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值