C#之银行ATM实例-Part1

一、题目要求:

在下面银行ATM示例的基础上,利用所学的面向对象的思想及语法,进行改进。要求如下:

1. 使用面向对象的思想,模拟现实世界中的银行、账号、ATM等对象,其中类中有字段、方法;

2. 在程序中适当的地方,使用属性、索引,注意使用修饰符;

3. 使用继承,继承账号(Account类)得到一个子类(如信用账号),增加字段(如信用额度)、属性、方法,覆盖(override)一些方法(如WithdrawMoney)。

4. 根据程序的需要(可选做),使用C#的其他语法成分,诸如:接口、结构、枚举等。

程序中加上适当的注释,并加一个说明文件,简要描述在什么地方使用了一些特殊的语法要素。

       评分标准:

              银行、账号、ATM等类,其中类中有字段、方法(3分);

              使用属性或索引,注意使用修饰符(2分);

              使用继承(3分);

              使用其他语法要素(2分)。

二、银行ATM类图

三、题目中使用的类

//-------------------------------使用这些类-------------------------
public class BankDemo {
public static void Main(string [] args)
{ 
    Bank bank = new Bank();
    bank.OpenAccount("2222", "2222", 20);
    bank.OpenAccount("3333", "3333", 50);
    ATM atm = new ATM(bank);
    for( int i=0; i<5; i++)
    {
        atm.Transaction();
    }
}
}
//--------------账号类--------------------
public class Account {
    double money; //decimal money;
    string id;
    string pwd;
    //string name;
public Account( string id, string pwd, double money )
{
    //if( money < 0 ) throw new Exception("....");
    this.id = id;
    this.pwd = pwd;
    this.money = money;
}
public double getMoney()
{
    return money;
}
public void setMoney(double val)
{
    this.money = val;
}
public string getId()
{
    return id;
}
public void setId(string id)
{
    this.id = id;
}
public string getpwd()
{
    return pwd;
}
public void setPwd(string pwd)
{
    this.pwd = pwd;
}
public bool SaveMoney( double money)
{
    if( money < 0 ) return false; //卫语句
    this.money += money;
    return true;
}
public bool WithdrawMoney( double money)
{
    if( this.money >= money )
    {
        this.money -= money;
        return true;
    }
    return false;
}
public bool IsMatch( string id, string pwd )
{
    return id==this.id && pwd==this.pwd;
}
}
//--------------------银行类----------------
using System;
using System.Collections;
using System.Collections.Generic;
public class Bank {
    List<Account> accounts = new List<Account>();
    public Account OpenAccount(string id, string pwd,
    double money)
    {
    Account account = new Account(id, pwd, money);
    accounts.Add( account );
    return account;
    }
    public bool CloseAccount( Account account)
    {
    int idx = accounts.IndexOf(account);
    if( idx<0 )return false;
    accounts.Remove(account);
    return true;
    }
    public Account FindAccount(string id, string pwd)
    {
    foreach(Account account in accounts)
    {
    if( account.IsMatch(id, pwd))
    {
        return account;
    }
    }
    return null;
}
}
//---------------ATM---------
using System;
using System.Collections;
public class ATM {
    Bank bank;
public ATM( Bank bank)
{
    this.bank = bank;
}
public void Transaction()
{
    Show("please insert your card");
    string id = GetInput();
    Show("please enter your password");
    string pwd = GetInput();
    Account account = bank.FindAccount(id, pwd);
if( account == null)
{
    Show("card invalid or password not corrent");
    return;
}
    Show("1: display; 2: save; 3: withdraw");
    string op = GetInput();
    if (op == "1")
    {
        Show( "balance: " + account.getMoney() );
    }
    else if( op=="2")
    {
        Show("save money");
        string smoney = GetInput();
        double money = double.Parse(smoney);
        bool ok = account.SaveMoney(money);
        if( ok ) Show("ok");
        else Show("eeer");
        Show("balance: " + account.getMoney());
    }
    else if( op=="3" )
    {
        Show("withdraw money");
        string smoney = GetInput();
        double money = double.Parse(smoney);
        bool ok = account.WithdrawMoney(money);
        if( ok ) Show("ok");
        else Show("eeer");
        Show("balance: " + account.getMoney());
    }
}
public void Show(string msg)
{
    Console.WriteLine(msg);
}
public string GetInput()
{
    return Console.ReadLine();// 输入字符
}
}

四、实验步骤

1.本项目中的类:Account、Bank、ATM、CreditAccount(继承Account的信用类)、CreditBank。原有的类的字段和方法均未改动。源代码见本文末。

2.继承Account的Credit Account类。其中,credit为增加的信用额度字段,因为信用账户增加了信用额度,所以需要重写Account类中的WithdrawMoney方法。当信用额度大于0即存在时,此时账户的余额应为实际余额加上信用额度。

3.继承Bank类的CreditBank类增加了credit参数,可以增加初始化信用账户,实现了普通用户和信用账户共存的结果CreditBank类重写了Bank类中的方法,在普通用户实际额度的基础上增加了信用额度。

4.修改主程序中的账户信息,此时已经新增了信用账户的类,可同时产生若干个普通账户和信用账户。

五、实验数据及处理结果

设置初始账户数据:

其中,普通账户的卡号为1111,密码为1111,余额为1000。信用账户的卡号为2222,密码为2222,实际余额为1000,信用额度为500。

1.普通用户

登录失败:

登录成功:

输入1,查看账户余额:

输入2:存钱并显示存钱后的余额:

输入3:取钱并显示取钱后的余额:

2.信用账户

登录失败、成功如上。

输入3,取钱并显示取钱后的余额(此时账户2000里包含信用余额500):

六、源代码

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

namespace ConsoleApp9
{
    //---------------ATM---------

    public class ATM
    {
        Bank bank;
        public ATM(Bank bank)
        {
            this.bank = bank;
        }
        public void Transaction()
        {
            Show("please insert your card");   //输入卡号
            string id = GetInput();
            Show("please enter your password");   //输入密码
            string pwd = GetInput();
            Account account = bank.FindAccount(id, pwd);
            if (account == null)
            {
                Show("card invalid or password not corrent");//卡号或密码错误
                return;
            }
            Show("1: display; 2: save; 3: withdraw");//成功登录,有三个选项对应三个功能
            string op = GetInput();
            if (op == "1")
            {
                Show("balance: " + account.getMoney());//显示当前账户余额
            }
            else if (op == "2")
            {
                Show("save money");//给当前用户存钱
                string smoney = GetInput();
                double money = double.Parse(smoney);
                bool ok = account.SaveMoney(money);
                if (ok) Show("ok");
                else Show("eeer");
                Show("balance: " + account.getMoney());
            }
            else if (op == "3")  //从当前用户取钱
            {
                Show("withdraw money");
                string smoney = GetInput();
                double money = double.Parse(smoney);
                bool ok = account.WithdrawMoney(money);
                if (ok) Show("ok");//当取钱金额大于余额时,报错
                else Show("eeer");
                Show("balance: " + account.getMoney());
            }
        }
        public void Show(string msg)
        {
            Console.WriteLine(msg);
        }
        public string GetInput()
        {
            return Console.ReadLine();// 输入字符
        }
    }

  
    //--------------账号类--------------------
    public class Account
    {
        double money; //decimal money;
        string id;
        string pwd;
        //string name;
        public Account(string id, string pwd, double money)
        {
            this.id = id;//账号
            this.pwd = pwd;//密码
            this.money = money;//余额
        }
        public double getMoney()
        {
            return money;
        }
        public void setMoney(double val)
        {
            this.money = val;
        }
        public string getId()
        {
            return id;
        }
        public void setId(string id)
        {
            this.id = id;
        }
        public string getpwd()
        {
            return pwd;
        }
        public void setPwd(string pwd)
        {
            this.pwd = pwd;
        }
        public bool SaveMoney(double money)
        {
            if (money < 0) return false; //卫语句
            this.money += money;
            return true;
        }
        public bool WithdrawMoney(double money)//取钱方法
        {
            if (this.money >= money)
            {
                this.money -= money;
                return true;
            }
            return false;
        }
        public bool IsMatch(string id, string pwd)
        {
            return id == this.id && pwd == this.pwd;
        }
    }

    //信用类
    public class CreditAccount : Account
    {
        double credit;//新增的信用额度字段
        public CreditAccount(string id, string pwd, double money, double credit)
            : base(id, pwd, money)
        {
            this.credit = credit;
            this.setMoney(credit + this.getMoney());
        }
        public CreditAccount(string id, string pwd, double money)
            : base(id, pwd, money)
        {
            this.credit = 0;
        }
        public bool changedebit(double debit)//重写方法
        {
            if (debit >= 0)
            {
                this.setMoney(this.getMoney() + (debit - this.credit));
                this.credit = debit;
                return true;
            }
            else
                return false;
        }
        public double getdebit()
        {
            return credit;
        }
    }
    //--------------------银行类----------------

    public class Bank
    {
        protected ArrayList accounts = new ArrayList();
        public Account OpenAccount(string id, string pwd,
        double money)
        {
            Account account = new Account(id, pwd, money);
            accounts.Add(account);
            return account;
        }
        public bool CloseAccount(Account account)
        {
            int idx = accounts.IndexOf(account);
            if (idx < 0) return false;
            accounts.Remove(account);
            return true;
        }
        public Account FindAccount(string id, string pwd)
        {
            foreach (Account account in accounts)
            {
                if (account.IsMatch(id, pwd))
                {
                    return account;
                }
            }
            return null;
        }
    }
    public class CreditBank : Bank
    {

        protected ArrayList creditaccounts = new ArrayList();
        new public Account OpenAccount(string id, string pwd, double money)
        {
            CreditAccount account = new CreditAccount(id, pwd, money);
            creditaccounts.Add(account);

            return account;
        }
        public CreditAccount OpenAccount(string id, string pwd, double money, double debit)
        {
            CreditAccount creditaccount = new CreditAccount(id, pwd, money, debit);
            creditaccounts.Add(creditaccount);

            return creditaccount;
        }
        public CreditAccount getaccount(int i)
        {
            return (CreditAccount)creditaccounts[i];

        }
        public int getaccountsnum()
        {
            return creditaccounts.Count;
        }
        new public CreditAccount FindAccount(string id, string pwd)
        {
            foreach (CreditAccount account in creditaccounts)
            {
                if (account.IsMatch(id, pwd))
                {
                    return account;
                }

            }
            return null;
        }

        class Program
        {
            static void Main(string[] args)
            {
                CreditBank creditbank = new CreditBank();
                creditbank.OpenAccount("2222", "2222", 20);
                creditbank.OpenAccount("3333", "3333", 50);
                creditbank.OpenAccount("4444", "4444", 50, 10);
                ATM atm = new ATM(creditbank);
                while (true)
                {
                    atm.Transaction();
                }
            }
        }
    }
}

 

 

 

  • 8
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值