题目:
代码:
package com.yy;
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("存钱成功");
}
}
public void withdraw(double amt) {
if(amt > 0) {
balance -= amt ;
System.out.println("取钱成功");
}else {
System.out.println("余额不足");
}
}
}
........................................................................................................................
package com.yy;
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 Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
.................................................................................................................
package com.yy;
public class Bank {
private Customer[] customers ;
private 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 Customer getCustomers(int index) {
// return customers[index];
if(index >= 0 && index < numberOfCustomer) {
return customers[index];
}
return null ;
}
public int getNumberOfCustomer() {
return numberOfCustomer;
}
}
.....................................................................................................
package com.yy;
public class test {
public static void main(String[] args) {
Bank bank = new Bank();
bank.addCustomer("yan", "xu");
bank.getCustomers(0).setAccount(new Account(2000));
bank.getCustomers(0).getAccount().deposit(100);
double y = bank.getCustomers(0).getAccount().getBalance();
System.out.println("余额:" + y);
System.out.println("客户:" + bank.getCustomers(0).getFirstName() + bank.getCustomers(0).getLastName());
}
}