JAVA ATM机操作

控制台流程参考

*****************

**欢迎使用ATM**

*****************

***********************************

*******请输入你想要操作类型*******

******1.余额查询  2.取款***********

******3.存款        4.转账***********

******5.打印        6.退卡***********

******7.查询转账信息**************

package cn.hp.demo7;

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

    @Override
    public String toString() {
        return "Account{" +
                "account='" + account + '\'' +
                ", password='" + password + '\'' +
                ", balance=" + balance +
                ", bankName='" + bankName + '\'' +
                ", name='" + 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 d) {
        balance -= d;
    }

    public void addBalance(double d1) {
        balance -= d1;
    }
}
package cn.hp.demo7;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ATM {
    Account act1;
    Account act2;
    Account act3;

    public  ATM() {
        act1 = new Account("10086", "10086", 100000, "工商银行", "王八");

        act2 = new Account("10010", "10010", 80000, "中国银行", "张三");

        act3 = new Account("10000", "10000", 110000, "交通银行", "李六");
    }

    //欢迎界面
    public void Welcome() {
        String str = "*****************\n" + "**欢迎使用ATM**\n" + "*****************\n";
        System.out.println(str + "\n");
    }

    //操作类型
    public void Operate(){
        /**
         * ***********************************
         * *******请输入你想要操作类型*******
         * ******1.余额查询  2.取款***********
         * ******3.存款        4.转账***********
         * ******5.打印        6.退卡***********
         * ******7.查询转账信息**************
         * **********************************
         */
        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;
        }
    }
}
package cn.hp.demo7;

import java.io.IOException;

public class AtmTest {
    public static void main(String[] args) throws IOException {
        ATM atm = new ATM();
        atm.Welcome();
        atm.login();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 账户类(满分50分) 版本1:满分10 分 设计Account1 类,包含: ■ 一个名为 id 的int 类型的私有数据域(默认值为 0),长度为 6 位。 ■ 一个名为 balance的double 类型的私有数据域(默认值为 0)。 ■ 一个名为 annualInterestRate 的double 类型的私有数据域存储当前利率(默认值为 0)。 假设所有的账户都有相同的利率。 ■ 一个名为 dateCreated 的Date 类型的私有数据域存储账户的开户日期。 ■ 一个能创建默认账户的无参构造方法。 ■ 一个能创建带特定 id 和初始余额的构造方法,初始余额不能为负数。 ■ id 、balance和annualInterestRate 的访问器和修改器。 ■ dateCreated 的访问器。 ■ 一个名为 getMonthlyInterestRate 的方法返回月利率。 ■ 一个名为 withDraw 的方法从账户提取特定金额。 ■ 一个名为 deposit 的方法向账户存人特定金额。 ■ double 类型的数据域保留 2 位小数。 ■ 成员方法和数据域应进行基本的合理性检查。 设计测试类 ATMMachine1: ■ 创建一个有 100 个账户的数组,其 id 为0,1,2,...99, 并初始化收支为 1000 美元。 ■ 主菜单如下(可参考教材中文版 P296或英文版 P367): Main menu 1: check balance 2: withdraw 3: deposit 版本2:满分20 分 扩展Account1 类为Account2 类: ■ Account2 类继承 Account1 类。 ■ 为Account2 类新增一个名为 password 的String 类型的私有数据域存储账号密码。 password 只能为字母或数字,长度不能小于 6 且不能大于 10。密码显示时为*******。 ■ 为Account2 类新增一个名为 name的String 类型的私有数据域存储客户名字。 ■ 为Account2 类新增一个名为 transactions 的ArrayList 类型的新数据域,其为客户存 储交易记录。这要求新建一个名为 Transaction 的类,类的定义请参照教材中文版 P327或英 文版P404。每笔交易都是 Transaction 类的一个实例。 ■ 新增一个带初始余额的构造方法,其 id 随产生,但不能与当前系统的 id 重复。 若初始余额的参数为负数,则抛出一个自定义异常并在当前构造方法中进行处理。 ■ 重写方法 withDraw ,要求支取的金额为 100 的整数倍,并且当日支取金额不能超过 5000,支取金额不允许透支。每进行一次操作应向 transactions 数组线性表添加一笔交易。 ■ 重写方法 deposit ,要求每进行一次操作应向 transactions 数组线性表添加一笔交易。 ■ 新增一个方法 changePassword ,只有旧密码正确,新密码符合要求,且两次输入相 同的情况下才可以成功修改密码 设计测试类 ATMMachine2,其主菜单如下(可参考教材中文版 P296或英文版 P367): Main menu 0:create a account 1: check balance 2: withdraw 3: deposit 4:details of the transaction 5: change password 6:exit ■ 若用户选择新建一个账号,则应提示用户输入账号 password 、balance 和 annualInterestRate ,其中 id 随产生。新产生的账户应序列化到名为 accounts.dat 的文件中。 所有账户只能通过这种方式产生。 ■ 所有用户操作结果应同步到 accounts.dat 文件中相应账户中。 ■ 所有用户操作应有友好、简介的提示语。 版本3:满分20 分 请参照银行的 AT M界面,在 Account2 类的基础上开发一个 GUI 界面的AT M系统。 要求界面应模拟小键盘,并且账户信息读、写于文件 accounts.dat 。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值