【无标题】

  • 需要ATM机类,该类完成主菜单的相关操作,在取钱和转账时,需判断余额情况

要求:实现数据持久化存储(方式不限)

  • 根据自身需要看是否要创建BankCard银行卡类,如需创建,可以直接用以下属性:

private String account;  //账号

private String password; //密码

private double balance;  //余额

private String bankName; //银行卡名字

private String name;     //持卡人姓名

account:

public class Account {
    private String account;     //账号
    private String password;    //密码
    private double balance;     //余额
    private String bankName;    //银行卡名字
    private String name;        //持卡人姓名

    //构造器,构造方法,生成多个用户信息

    public Account() {
    }

    public Account(String account, String password, double balance, String bankName, String name) {
        this.account = account;
        this.password = password;
        this.balance = balance;
        this.bankName = bankName;
        this.name = name;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void subBalance(double ble){
        balance -= ble;             //余额减少
    }

    public void addBalance(double ble){
        balance += ble;             //余额增加
    }

    @Override
    public String toString() {
        return "Account{" +
                "account='" + account + '\'' +
                ", password='" + password + '\'' +
                ", balance=" + balance +
                ", bankName='" + bankName + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

ATM:

package dome4;

import dome4.Account;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;



public class ATM {
    Account act1;
    Account act2;
    Account act3;
    Account act4;
    public  ATM() {
        act1 = new Account("1", "1", 100, "农商银行", "shr");
        act2 = new Account("2", "2", 200, "农商银行", "szh");
        act3 = new Account("3", "3", 300, "农商银行", "ycx");
        act4 = new Account("4", "4", 400, "农商银行", "qcx");
    }

    //欢迎界面
    public void Welcome() {
        System.out.println( "**欢迎使用ATM**");
    }

    //操作类型
    public void Operate(){
        System.out.println();
        System.out.println("***********************************");
        System.out.println("*******请输入你想要操作类型*******");
        System.out.println("******1.余额查询  2.取款***********");
        System.out.println("******3.存款        4.转账***********");
        System.out.println("******5.打印        6.退卡***********");
        System.out.println("******7.查询转账信息**************");
        System.out.println("**********************************");
    }

    //登录
    public void login() throws IOException {
        String acount, pwd;
        int counter = 0;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        do {
            System.out.println("请输入卡号:");
            acount = reader.readLine();
            System.out.println("请输入密码:");
            pwd = reader.readLine();
            if (!judgment(acount,pwd)) {
                System.out.println("您的卡号或密码有误:");
                counter++;
            }else
                Progress();
        }while (counter < 3);
        System.exit(1);
    }

    private void Progress() throws IOException {
        int num;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        Operate();
        System.out.println("请输入你想要操作类型:");
        num = reader.read();
        switch (num){
            case 49:
                Inquire();             //查询
                break;
            case 50:
                Withdrawal();          //取款
                break;
            case 51:
                Deposit();             //存款
                break;
            case 52:
                Transfer();            //转账
                break;
            case 53:                   //打印
                Print();
                break;
            case 54:                   //退卡
                Exit();
                break;
            case 55:
                Information();         //查询信息
                break;
        }
        System.exit(1);
    }

    //查询信息
    private void Information() throws IOException {
        System.out.print("姓名\t" + "卡号\t" + "银行卡名称");
        System.out.println();
        System.out.println(act1);
        System.out.println(act2);
        System.out.println(act3);
        Progress();
    }

    //打印
    private void Print() {
    }

    //退卡
    private void Exit() {
        System.out.println("感谢您使用本系统,再见!");
        System.exit(1);
    }

    //转账
    private void Transfer() throws IOException {
        String acount, pwd;
        int counter = 0;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        do {
            System.out.println("请输入您要转账的卡号:");
            acount = reader.readLine();
            System.out.println("请输入您的密码:");
            pwd = reader.readLine();
            if (!judgment2(acount,pwd)){
                System.out.println("您的转账卡号或密码有误:");
                counter++;
            }else do {
                System.out.println("请输入转账的金额:");
                String str = reader.readLine();
                double d = Double.valueOf(str).doubleValue();
                if (d > act1.getBalance()){
                    System.out.println("余额不足,请重新输入你要转账的金额:");
                }else {
                    act1.subBalance(d);
                    double d1 = Double.valueOf(str).doubleValue();
                    act2.addBalance(d1);
                    System.out.println("转账成功,您账户的余额为:" + act1.getBalance());
                    System.out.println("收款成功,您的账户余额为:" + act2.getBalance());
                    // Operate();
                    Progress();
                }
            }while (true);
        }while (counter < 3);
        System.exit(1);
    }

    //存款
    private void Deposit() throws IOException {
        String str = null,str1;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        do {
            System.out.println("请输入存款金额:");
            str = reader.readLine();
            double d = Double.valueOf(str).doubleValue();
            act1.addBalance(d);
            System.out.println("存款成功,你账户的余额为:" + act1.getBalance());
            // Operate();
            Progress();
        }while (true);
    }

    //查询余额
    private void Inquire() throws IOException {
        System.out.print("当前账户余额为:" + act1.getBalance());
        Progress();
    }

    //取款
    private void Withdrawal() throws IOException {
        String str = null,str1;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        do {
            System.out.println("请输入取款金额:");
            str = reader.readLine();
            double d = Double.valueOf(str).doubleValue();
            if (d > act1.getBalance()) {
                System.out.println("余额不足:请重新输入取款额:");
            }else {
                act1.subBalance(d);
                System.out.println("取款成功,您的账户余额为:" + act1.getBalance());
                //   Operate();
                Progress();
            }
        }while (true);
    }

    //判断余额
    public boolean isBalance(){
        if (act1.getBalance() < 0){
            return false;
        }
        return true;
    }

    //判断账号密码是否正确
    private boolean judgment(String acount, String pwd) {
        if (act1.getAccount().equals(acount) && act1.getPassword().equals(pwd)) {
            return true;
        }else {
            return false;
        }
    }
    //判断账号密码是否正确
    private boolean judgment2(String acount, String pwd) {
        if (act1.getAccount().equals(acount) && act1.getPassword().equals(pwd)) {
            return true;
        }else {
            return false;
        }
    }

}

测试:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值