2012-11-29 81 views
0

我想製作一個銀行帳戶程序,但我無法弄清楚如何讓我的所有變量都可見,或者如何使我的每個類都可見,或者如何使取款和存款方法我的代碼可見。任何人都可以看看我的代碼並告訴我什麼是錯的?我只想在客戶端類中輸入和輸出。java中有多個類的銀行帳戶客戶端

感謝

客戶端類

public class Client { 
    public static void main(String args[]) { 
    Scanner input = new Scanner(System.in);  
    System.out.println("Enter your Name: "); 
    String cusName = input.nextLine(); 
    System.out.println("Enter Account Type: "); 
    String type = input.next(); 
    System.out.println("Enter Initial Balance: "); 
    int bal = input.nextInt(); 
    BankAccount b1 = new BankAccount(cusName, num, type, bal); 
    int menu; 
    System.out.println("Menu"); 
    System.out.println("1. Deposit Amount"); 
    System.out.println("2. Withdraw Amount"); 
    System.out.println("3. Display Information"); 
    System.out.println("4. Exit"); 
    boolean quit = false; 
    do { 
     System.out.print("Please enter your choice: "); 
     menu = input.nextInt(); 
     switch (menu) { 
     case 1: 
     b1.deposit(); 
     break; 

     case 2: 
     b1.withdraw(); 
     System.out.println("Current Account Balance=" + Balance); 
     System.out.print("Enter withdrawal amount:"); 
     amount = input.nextInt(); 
     break; 

     case 3: 
     b1.display(); 
     break; 

     case 4: 
     quit = true; 
     break; 
     } 
    } while (!quit); 
    } 
} 

Money類

public class Money 
{ 

    static int accountNumber, Balance, amount; 
    Scanner input = new Scanner(System.in); 
    static String name, actype; 
    public int deposit() { 
    System.out.print("Enter depost amount:"); 
    amount = input.nextInt(); 
    if (amount < 0) { 
     System.out.println("Invalid"); 
     return 1; 
    } 
    Balance = Balance + amount; 
    return 0; 
    } 

    int withdraw() { 

    if (Balance < amount) { 
     System.out.println("Not enough funds."); 
     return 1; 
    } 
    if (amount < 0) { 
     System.out.println("Invalid"); 
     return 1; 
    } 
    Balance = Balance - amount; 
    return 0; 
    } 

} 

BankAccount類

class BankAccount { 
    Scanner input = new Scanner(System.in); 
    static String name, actype; 
    static int bal, amt; 
    Random randomGenerator = new Random(); 
    int accNo = randomGenerator.nextInt(100); 

    BankAccount(String name, int accNo, String actype, int bal) { 
    this.name = name; 
    this.accNo = accNo; 
    this.actype = actype; 
    this.bal = bal; 
    } 
    void display() { 
    System.out.println("Name:" + name); 
    System.out.println("Account No:" + accNo); 
    System.out.println("Balance:" + bal); 

    } 

    void dbal() { 
    System.out.println("Balance:" + bal); 
    } 
} 
+2

上週這是學生家庭作業,這周它似乎是BankAccount作業 – AlexWien

回答

2
  1. 添加MoneyBankAccount並創建一個getter方法爲:

    class BankAccount { 
        Scanner input = new Scanner(System.in); 
        static String name, actype; 
        static int bal, amt; 
        Random randomGenerator = new Random(); 
        int accNo = randomGenerator.nextInt(100); 
        Money money; 
    
        BankAccount(String name, int accNo, String actype, int bal) { 
        this.name = name; 
        this.accNo = accNo; 
        this.actype = actype; 
        this.bal = bal; 
        this.money = new Money(); 
        } 
    
        public Money getMoney(){ 
        return this.money; 
        } 
        ..... 
    } 
    
  2. 使用bankaccount.getMoney()調用depositwithdraw爲:

    b1.getMoney().deposit(); 
    b1.getMoney().withdraw(); 
    

此外,我會建議,使Money類屬性,例如金額,accntType ...非靜態並通過構造函數進行設置。靜態變量與類定義相關聯,因此您不能通過abl來維護每個銀行賬戶。

+0

謝謝,這工作。忘記使用set/get方法。 –

+0

@RickyMcQuesten如果有幫助,那就別忘了接受答案。 –

0

屬性的一個對象不應該是一成不變的,比如你的「名稱,動作類型, bal和amt「。此外,我認爲您的錢類應該存在,這些方法可以在銀行賬戶(您從銀行賬戶存入/取出)。

1

我不會爲你回答這個問題。相反,我將建議您閱讀更多關於Java編程概念的知識,默認情況下會向您解釋。

如果你不想讀這些鏈接,@YogendraSingh回答了這個問題非常好,使用的答案。

+0

謝謝,我想我只是對封裝有點困惑。第一個鏈接幫助我清除它。 –

+0

如果您認爲這是最佳答案,請隨時選中它;) – durron597