1. 先創建Customer類
2.聲明三個私有屬性 firstName lastName account
3.提供一個共有構造器 編寫獲取get方法 放回相應屬性
4. 聲明setaccount方法對account賦值
5. 聲明getaccount()方法來獲取account值
class Customer{
private String firstName ;
private String lastName ;
private Account account;
public Customer(String firstName,String lastName){
this.firstName = firstName;
this.lastName= lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setAccount(Account account){
this.account = account;
}
public void getAccount(){
return account;
}
}
測試類
class test {
public static void main(String[] args) {
System.out.println("Creating the customer Jane Smith.");
Customer customer = new Customer("Jane", "Smith");
//code
System.out.println("Creating her account with a 500.00 balance.");
Account account = new Account(500);
//code
System.out.println("Withdraw 150.00");
account.withdraw(150);
//code
System.out.println("Deposit 22.50");
account.Deposit (150);
//code
System.out.println("Withdraw 47.62");
account.withdraw(47.62);
//code
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account1.getBalance());
}
}