有以下UML类图
![]()
![]()
![]()
Account类
package exer;
public class Account {
//余额
private double balance;
//构造器
public Account(double init_balance){
this.balance=init_balance;
}
//获取余额
public double getBalance(){
return balance;
}
//存款
public void deposit(double amt){
if(amt>0){
balance+=amt;
System.out.println("存钱成功!存了"+amt+"元!");
}
}
//取款
public void withdraw(double amt){
if(balance>=amt){
balance-=amt;
System.out.println("取款成功!取走了"+amt+"元!");
}
else{
System.out.println("余额不足!");
}
}
}
Customer类
package exer;
public class Customer {
//姓
private String firstName;
//名
private String lastName;
//账户
private Account account;
//构造器
public Customer(String f,String l){
this.firstName=f;
this.lastName=l;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public Account getAccount(){
return account;
}
public void setAccount(Account acct){
this.account=acct;
}
}
Bank类
package exer;
public class Bank {
private Customer[] customers;
int numberOfCustomer;
public Bank(){
customers=new Customer[10];
}
public void addCustomer(String f,String l){
Customer cust=new Customer(f,l);
customers[numberOfCustomer++]=cust;
}
public int getNumOfCustomers(){
return numberOfCustomer;
}
public Customer getCustomer(int index){
if(index>=0&&index<this.numberOfCustomer){
return customers[index];
}
return null;
}
}
BankTest类
package exer;
public class BankTest {
public static void main(String[] args) {
Bank bank=new Bank();
bank.addCustomer("Jane","Smith");
bank.getCustomer(0).setAccount(new Account(2000));
bank.getCustomer(0).getAccount().withdraw(500);
double balance = bank.getCustomer(0).getAccount().getBalance();
System.out.println("客户:"+bank.getCustomer(0).getFirstName()+"的账户余额为:"+balance);
System.out.println("*******************");
bank.addCustomer("三", "张");
int numOfCustomers = bank.getNumOfCustomers();
System.out.println(numOfCustomers);
}
}