2013-03-15 104 views
0
import java.text.DecimalFormat; 
     import java.util.ArrayList; 

     public class AccountOwner { 
      private Account account; 
      private String firstname; 
      private String lastname; 
      private String address; 
      private double currentBalance; 
      private ArrayList<Integer> withdrawAmount = new ArrayList<Integer>(5); 
      private ArrayList<Double> deposits = new ArrayList<Double>(); 
      private ArrayList<Double> purchases = new ArrayList<Double>(5); 
      private DecimalFormat formatter = new DecimalFormat("##0.00"); 

      public AccountOwner(String firstname, String lastname, String address) { 
       this.firstname = firstname; 
       this.lastname = lastname; 
       this.address = address; 
      } 

      public String getFirstName() { 
       return firstname; 
      } 

      public String getLatName() { 
       return lastname; 
      } 

      public String getAddress() { 
       return address; 
      } 


      public String checkBalance() { 

       for (Double deposit : deposits) { 
        this.currentBalance += deposit; 
       } 

       return formatter.format(currentBalance); 
      } 

      public void makeDeposit(double amount) { 
       deposits.add(amount); 
      } 

      public void viewAllDeposits() { 

       double allDeposits = 0.0; 
       System.out.println("All deposits till today:"); 
       for (int i = 0; i < deposits.size(); i++) { 
        allDeposits = deposits.get(i); 

        System.out.print("\t"+"$"+allDeposits); 
       } 
       System.out.println(); 
      } 

      public void withdrawMoney(int amount) { 
       withdrawAmount.add(amount); 
       currentBalance -= amount; 
      } 

      public String getActualBalance() { 
       return formatter.format(currentBalance); 
      } 

      public void withdrawAmounts(){ 
       System.out.println("All Withdrawls till today"); 
       for(int i = 0; i < withdrawAmount.size(); i++){ 
        System.out.print("\t"+"$"+withdrawAmount.get(i)); 
       } 
       System.out.println(); 
      } 

      public void makePurchase(double itemPrice){ 
       purchases.add(itemPrice); 
       this.currentBalance -= itemPrice; 
      } 

      public void viewAllmadePurchases() { 

       double purchase = 0.0; 
       System.out.println("All purchases made till today:"); 
       for (int i = 0; i < purchases.size(); i++) { 
        purchase = purchases.get(i); 

        System.out.print("\t"+"$"+purchase); 
       } 

      } 

      public void viewMyPersonalInformation(){ 
       System.out.println("Firstname:" + this.firstname); 
       System.out.println("LastName:" +this.lastname); 
       System.out.println("Address:" +this.address); 
       System.out.println("Balance:" +this.checkBalance()); 
       viewAllDeposits(); 
       withdrawAmounts(); 
       viewAllmadePurchases(); 

      } 



     public class Account { 
      private AccountOwner customer; 
      private int accountNumber; 


      public Account(){ 
       customer = null; 
       accountNumber = 0000000; 


      } 

      public Account(int accountNumber, AccountOwner owner){ 
         this.accountNumber = accountNumber; 
         customer = owner; 


      } 

      public int accountNumberIs(){ 
       return accountNumber; 
      } 

     public class BankManager { 
      private Account account; 
      private AccountOwner accountOwner; 
      private int accountNumber; 


       public void createNewAccount(int accountNumber, AccountOwner owner){ 
       account = new Account(accountNumber, owner); 
       this.accountNumber = accountNumber; 
       this.accountOwner = owner; 
       } 



      public int getaccountNumber(){ 

       return accountNumber; 
      } 


      public void setAccountNumber(int newaccountnumber){ 
       accountNumber = newaccountnumber; 
      } 

      public void customerInformation(){ 
       accountOwner.viewMyPersonalInformation(); 
      } 
public class MainProgram { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     BankManager manager = new BankManager(); 
     AccountOwner owner = new AccountOwner("Tom", "Smith", "208 W 119th St"); 
     manager.createNewAccount(389745, owner); 
     Account acc = new Account(); 





     owner.makeDeposit(550); 
     owner.makeDeposit(700); 
     owner.makeDeposit(122.93); 
     owner.makeDeposit(195.93); 
     owner.withdrawMoney(200); 
     owner.makePurchase(200); 
     owner.makeDeposit(100); 
     owner.makePurchase(1220); 
     owner.withdrawMoney(30); 



     owner.viewMyPersonalInformation(); 
     System.out.println(); 
     System.out.println(); 
     System.out.println(); 
     System.out.println(acc.accountNumberIs()); 

我的問題是我試圖從帳戶所有者訪問帳戶號碼,而不涉及到bankmanager類的引用..我怎麼才能得到它的工作。我已經 一直試圖使用該帳戶類本身的原因,我創建一個構造函數和分配這些 PARAMATERS在賬戶類的領域,但似乎沒有工作無法訪問帳戶號碼

回答

0
class AccountOwner { 
//... 
    private Account account; 
//... 
    public Integer getAccountNumber() { 
     return this.account != null ? this.account.accountNumberIs() : null; 
    } 
} 
1

該代碼包含了一個錯誤:

public Account(int accountNumber, AccountOwner owner){ 
    AccountOwner cstomer = owner; 
    int acctNumber = accountNumber; 
    accountNumber = acctNumber; 
    //System.out.println(accountNumber); 
} 

你傳遞到您的構造函數的參數accountNumber正在優先於類的accountNumber場。您AccountaccountNumber場從來沒有真正得到設定。

這相當於:

public Account(int accountNumber, AccountOwner owner){ 
    AccountOwner cstomer = owner; 
    accountNumber = accountNumber; 
} 

,以確保您所訪問的領域使用this關鍵字爲:this.accountNumber

public Account(int accountNumber, AccountOwner owner){ 
    AccountOwner cstomer = owner; 
    this.accountNumber = accountNumber; 
    //System.out.println(accountNumber); 
} 

還有一個BUG,該Account.customer未分配:

public Account(int accountNumber, AccountOwner owner){ 
    customer = owner; 
    this.accountNumber = accountNumber; 
} 

我建議你要學會用調試器或猶未寫單元測試(第一次)。

+0

如果你更清楚地解釋OP的代碼出了什麼問題,爲什麼你的代碼會修復它,我會+1。 – 2013-03-15 14:47:49

+0

感謝@SamIam。 – gontard 2013-03-15 14:55:53

+0

好吧,這是怎麼回事..當我嘗試從帳戶類獲得帳號我得到0而不是實際的帳號.. – user1535963 2013-03-15 14:57:27

0

你已經設置你的了accountNumber類型爲INT並將其設置爲0000000(等於只是0 int類型),因此你得到0,當你調用getAccountNumber()方法。

相反,改變了accountNumber類型字符串並初始化爲 '0000000'。您將看到正在打印0000000。

+0

您提出的解決方案似乎不起作用 – user1535963 2013-03-15 14:58:00