22 มกราคม 2555

[JAVA]การใช้ตัวแปร Collection Vector ของข้อมูลชนิดคลาส แบบฝึกหัดที่ 6.3


------------------------------------
 Customer
------------------------------------

package Banking;
import java.util.*;

public class Customer {
 private String firstName;
 private String lastName;
 private Account acct[];
 private Vector v = new Vector();
 private Enumeration e = v.elements();
 private int NumAccount=0;
 public Customer(String fName,String lName){
 firstName = fName;
 lastName = lName;
 acct = new Account[20];
 }
 public void setFirstName(String fName){
 firstName = fName;
 }
 public String getFirstName(){
 return firstName;
 }
 public void setLastName(String lName){
 lastName = lName;
 }
 public String getLastName(){
 return lastName;
 }
 public void setAccount(Account acct){
 acct = acct;
 }
 public Account getAccount(int index){
 return acct[index];
 }
 public void addAccount(Account acct){
 this.acct[NumAccount]=acct;
 NumAccount++;
 }
public int getNumAccount(){
 return NumAccount;
 }
 public void setVector(Account acct){
 this.v.add(acct.getBalance());
 }
 public void printReport()
 {
 for(int i=0;i
 System.out.println("Account"+(i+1)+" Balance :"+getAccount(i).getBalance());
 }
 public void printReport2(){
 int i=0;
 while(e.hasMoreElements()){
 System.out.println("Account"+(i + 1)+" Balance :"+e.nextElement());
 i++;
 }
 }
}

------------------------------------
 Account
------------------------------------

package Banking;

public class Account {
 protected double balance;
 public Account(double amount){
 balance = amount;
 }
 public void deposit(double amount){
 balance = balance + amount;
 }
 public boolean withdraw(double amount){
 boolean result = true;
 if(amount < balance){
 balance = balance - amount;
 }
 else{
 result = false;
 }
 return result;
 }
 public double getBalance(){
 return balance;
 }
 public void showBalance(){
 System.out.println(balance);
 }
 public Account(){

}
}

------------------------------------
 Teller
------------------------------------

import Banking.*;

public class Teller {
 public static void main(String args[]){

Customer cust = new Customer("Somchai","Sommut");
 Account acct1 = new Account(5000);
 Account acct2 = new Account(3000);

cust.addAccount(acct1);
 cust.addAccount(acct2);

acct1.deposit(4200);
 acct1.withdraw(3000);

acct2.deposit(3200);
 acct2.withdraw(1000);

cust.setVector(acct1);
 cust.setVector(acct2);

System.out.println(cust.getFirstName()+" "+cust.getLastName());
 System.out.println("Number of Account : "+cust.getNumAccount());

cust.printReport();
 cust.printReport2();
 }
}