/* 写一个名为:Account的类模拟账户 属性有:账户id,余额balance,年利率annualInterestRate 方法有:各个属性的get和set方法。取款方法:withdraw(),存款方法deposit(). 写一个测试程序 创建一个Customer,名字叫Jane Smith,他有一个账号为1000,余额为2000,年利率为1.23%的账户 对Jane Smith进行操作: 存入100;再取出960,再取出2000 打印信息为: 成功存入:100 成功取出:960 余额不足,取钱失败! */ class Account{ private String id; private double balance; private double annualInterestRate; // 无参构造方法 public Account() { } // 有参构造方法 public Account(String id, double balance, double annualInterestRate) { this.id = id; this.balance = balance; this.annualInterestRate = annualInterestRate; } // setter and getter public String getId() { return id; } public void setId(String id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } // 取款方法 public void withdraw(double money){ if (money <= 0){ System.out.println("取款金额不正确!"); return; } if (getBalance() < money){ System.out.println("余额不足,取钱失败!"); return; } this.setBalance(this.getBalance() - money); System.out.println("成功取出:" + money); } // 存款方法 public void deposit(double money){ if (money <= 0){ System.out.println("取款金额不正确!"); return; } balance += money; System.out.println("成功存入:" + money); } } //客户类 class Customer{ // 客户姓名 private String name; // 客户手中银行账户 private Account account; // 无参构造方法 public Customer() { } // 有参构造方法 public Customer(String name, Account account) { this.name = name; this.account = account; } // setter and getter public String getName() { return name; } public void setName(String name) { this.name = name; } public Account getAccount() { return account; } public void setAccount(Account account) { this.account = account; } } class CustomerText{ public static void main(String[] args) { // 创建账户对象 Account account = new Account("1000",2000,0.0123); // 创建客户对象,将account传给客户对象,表明这个账户是客户的 Customer customer = new Customer("Jane Smith",account); // 以下是对Jane Smith 账户的操作 // 存入100 customer.getAccount().deposit(100); // 取出960 customer.getAccount().withdraw(960); // 再取出2000 customer.getAccount().withdraw(2000); System.out.println("顾客:" + customer.getName() + "有一个账户,账号是:" + customer.getAccount().getId() + "账号年利率为:" + customer.getAccount().getAnnualInterestRate() + "账户余额是:" + customer.getAccount().getBalance()); } }
模拟客户在银行存取款
于 2022-11-17 14:14:57 首次发布