----------------作业_银行账户转账------------阿巴阿巴

import java.util.Scanner;

// 创建两个银行账户 将其中一个账户的余额转到另一个账户
public class task01 {
    public static void main(String[] args) {
//        调用键盘
        Scanner input = new Scanner(System.in);
//        创建customer类的对象
        Customer lex = new Customer();
        lex.name = "lex";
        lex.tel = "5418894188";
        lex.cid = "430521************";

//      创建account类的对象
        Account lexAccount = new Account();
        lexAccount.id = "001";
        lexAccount.balance = 190000;
//      创建bankclerk类的对象
        BankClerk sky = new BankClerk();
//      调用bankclerk的open方法
        sky.open(lex, lexAccount);
//
        Customer ghost = new Customer();
        ghost.name = "ghost";
        ghost.tel = "5413894138";
        ghost.cid = "430521************";
//      创建account类的对象
        Account ghostAccount = new Account();
        ghostAccount.id = "002";
        ghostAccount.balance = 100000;
//
        sky.open(ghost, ghostAccount);

//      调用bankclerk的open方法
        boolean flag = true;
        int choice;
        while (flag == true) {
            System.out.println("============ ATM  ============");
            System.out.println("请选择: 1.存钱 2.取钱 3.转账 4.查询 5.退出");
            choice = input.nextInt();
//            Switch选择操作
            switch (choice) {
                case 1:
                    System.out.println("输入您要存款的金额:");
                    int saveMoney = input.nextInt();
                    lexAccount.save(saveMoney);
                    break;
//
                case 2:
                    System.out.println("输入您要取款的金额:");
                    int withdrawMoney = input.nextInt();
                    lexAccount.withdraw(withdrawMoney);
                    break;
//
                case 3:
                    System.out.println("请输入您要转账的金额:");
                    int transferMoney = input.nextInt();
                    lexAccount.balance += transferMoney;
                    ghostAccount.balance -= transferMoney;
                    break;
                case 4:
                    System.out.println("请输入您要查询的账户:");
                    int qureId = input.nextInt();
                    if (qureId == 001) {
                        System.out.println("账号:" + lexAccount.id);
                        System.out.println("余额:" + lexAccount.balance);
                        System.out.println("客户:" + lex.name);
                    } else if (qureId == 002) {
                        System.out.println("账号:" + ghostAccount.id);
                        System.out.println("余额:" + ghostAccount.balance);
                        System.out.println("客户:" + ghost.name);
                    } else {
                        System.out.println("您输入的账户不存在");
                    }
                    break;
                case 5:
                    flag = false;
                    System.out.println("感谢使用,祝您生活愉快");
            }
        }
    }
}

// 账户类
class Account {
    String id;
    double balance;

    //    存钱方法
    public void save(double money) {
//        money>0存钱成功
        if (money > 0) {
            balance += money;
        }
//        money<0存钱失败
        else {
            System.out.println("参数有误");
        }
    }

    //    取钱方法
    public void withdraw(double money) {
//        money<o取钱失败
        if (money < 0) {
            System.out.println("参数有误");
        }
//        mone<balane取钱失败
        else if (money > balance) {
            System.out.println("余额不足");
        }
//        money>=blance取钱成功
        else {
            balance -= money;
        }
    }
}

// 客户类
class Customer {
    //姓名
    String name;
    //    电话
    String tel;
    //    身份证
    String cid;
    //    客户的账户
    Account account;
}

// 银行类
class BankClerk {
    //    customer c 客户类的对象
//    account a 账户类的对象
    public void open(Customer c, Account a) {
//        客户和账户进行绑定
        c.account = a;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值