题目要求见如上PDF,代码如下:
1.初始化Account类:
package com.mybank.domain;
public class Account {
private double balance;//添加实例变量,维护客户账户存款额
//初始化账户余额
public Account(double initBalance) {
balance = initBalance;
}
//存款操作
public boolean deposit(double amt) {
if (amt > 0) {
balance += amt;
System.out.println("存款"+amt+"元成功!");
return true;
}
else {
System.out.println("存款失败!");
return false;
}
}
//取款操作
public boolean withdraw(double amt) {
if (amt > 0 && balance >= amt) {
balance -= amt;
System.out.println("取款"+amt+"元成功!");
return true;
}
else {
System.out.println("取款失败!");
return false;
}
}
//返回balance值
public double getBalance() {
return balance;
}
}
2.初始化Bank类:
package com.mybank.domain;
public class Bank {
private static Customer[] customers;//创建用户对象数组
private static int numberOfCustomers;//记录实际客户数目
static {
customers = new Customer[10];//初始化数组,长度为10
numberOfCustomers = 0;
}
//创建新用户
public static void addCustomer(String f, String l) {
customers[numberOfCustomers] = new Customer(f, l);
numberOfCustomers++;
}
//获取实际客户数目
public static int getNumberOfCustomers() {
return numberOfCustomers;
}
//获取指定客户
public static Customer getCustomer(int index) {
return customers[index];
}
}
3.初始化Customer类:
package com.mybank.domain;
public class Customer {
private String firstName;//创建实例变量记录用户的姓氏
private String lastName;//创建实例变量记录用户的名
private Account[] accounts;//创建账户对象数组
private int numberOfAccounts;//记录实际账户数目
//初始化客户姓名和账户数组
public Customer(String f, String l) {
firstName = f;
lastName = l;
accounts = new Account[10];
numberOfAccounts = 0;
}
//返回用户的姓氏
public String getFirstName() {
return firstName;
}
//返回用户的名
public String getLastName() {
return lastName;
}
//创建新账户
public void addAccount(Account acc) {
accounts[numberOfAccounts++] = acc;
}
public void addAccount(double amt) {
accounts[numberOfAccounts++] = new Account(amt);
}
//返回实际账户数目的值
public int getNumberOfAccounts() {
return numberOfAccounts;
}
//根据传进来的数组下标返回该下标对应的 Account 对象
public Account getAccount(int index) {
return accounts[index];
}
public Account getAccounts(int index) {
return accounts[index];
}
@Override
//重写 toString() 方法,将 Customer 对象以客户名字的字符串形式输出
public String toString() {
return "Customer: " + firstName + " " + lastName;
}
}
4.进行测试:
package com.mybank.test;
import com.mybank.domain.Account;
import com.mybank.domain.Bank;
import com.mybank.domain.Customer;
public class TestBanking {
public static void main(String[] args) {
Bank.addCustomer("张", "三");
Bank.getCustomer(0).addAccount(new Account(1000));
System.out.println(Bank.getCustomer(0));
System.out.println(" 账户 1 余额为: " + Bank.getCustomer(0).getAccount(0).getBalance()+"元");
Bank.addCustomer("李", "四");
Bank.getCustomer(1).addAccount(new Account(1000));
Bank.getCustomer(0).getAccounts(0).withdraw(30);
Bank.getCustomer(0).getAccounts(0).deposit(300);
for (int i = 0; i < Bank.getNumberOfCustomers(); i++) {
Customer customer = Bank.getCustomer(i);
System.out.println(customer);
for (int j = 0; j < customer.getNumberOfAccounts(); j++) {
Account account = customer.getAccount(j);
System.out.println(" 账户 " + (j + 1) + " 余额为: " + account.getBalance()+" 元");
}
}
}
}
测试数据及操作为:
(1)初始化两个用户为:张三、李四。
(2)初始化每个用户1个账户。
(3)每个账户余额初始化为1000元。
(4)对张三的账户进行一次取款为30元的操作。
(5)对张三的账户进行一次存款为300元的操作。
(6)最终输出所有用户账户的余额。
测试结果如图: