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; } }
----------------作业_银行账户转账------------阿巴阿巴
最新推荐文章于 2024-05-21 10:23:42 发布