2016-03-08 91 views
0

我想寫一個銀行帳戶,詢問用戶是否想查看記錄,刪除記錄和其他內容。 This is what I'm trying to produce使用鏈接列表作爲輸出?

我的程序差不多完成了唯一的問題,我得到的是輸出。當我嘗試產生輸出時,出現了一些奇怪的錯誤。當輸出有效時。我會把它放在一個鏈表中。

public class Bank 
{ 

public static void main(String args[]){ 
    ArrayList<Customer> accounts = new ArrayList<Customer>(); 

    Customer customer1 = new Customer(1, "Savings", "Dollars", 800); 
    Customer customer2 = new Customer(2, "Checking", "Euro", 1900); 
    Customer customer3 = new Customer(3, "Checking", "Pound", 8000); 

    accounts.add(customer1); 
    accounts.add(customer2); 
    accounts.add(customer3); 


    int customerID=4; 
    String choice; 
    int deposit; 
    int withdraw; 
    Scanner in = new Scanner(System.in); 
    Customer operation = new Customer(); 
    boolean flag = true; 

    String accountType; 
    String currencyType; 
    int balance; 

    while(flag){ 
     System.out.println("Select a choice:"); 
     System.out.println("1. Existing customer"); 
     System.out.println("2. New customer"); 
     System.out.println("3. Quit"); 


     String input = in.next(); 

     //existing user 
     if(input.equals("1")){ 

      System.out.println("Enter CustomerID: "); 
      customerID = in.nextInt(); 


      System.out.println("Would you like to: "); 
      System.out.println("1. Deposit "); 
      System.out.println("2. Withdraw "); 
      System.out.println("3. Display account info "); 
      System.out.println("4. Check balance "); 

      choice = in.next(); 

      if(choice.equals("1")){ 
      System.out.println("How much would you like to deposit?"); 
      deposit = in.nextInt(); 
      operation.deposit(deposit); 
      } 

      else if (choice.equals("2")){ 
      System.out.println("How much would you like to withdraw?"); 
      withdraw = in.nextInt(); 
      operation.withdraw(withdraw); 

      } 

      else if (choice.equals("3")){ 
      operation.display(accounts.get(customerID)); 
      } 

      else if (choice.equals("4")) 
      operation.balance(accounts.get(customerID)); 

      else 
      System.out.println("Invalid"); 
     } 

     //new user 
     else if(input.equals("2")){ 
      //add new user to arraylist 

      int newID = customerID + 1; 

      System.out.println("Enter account type: "); 
      accountType = in.next(); 
      System.out.println("Enter currency type: "); 
      currencyType = in.next(); 
      System.out.println("Enter initial balance: "); 
      balance = in.nextInt(); 

      System.out.println("Your customer ID will be: " + newID); 


      accounts.add(new Customer(newID, accountType, currencyType, balance)); 


     } 

     else if(input.equals("3")){ 

      System.out.println("Thanks for using this bank!"); 
      flag = false; 
     } 


     else{ 

      System.out.println("Invalid"); 

     } 
     } 

    } 
} 

我的第二類:

public class Customer 
{ 

String accountType, currencyType, info; 
public int customerID, balance, amount; 
Scanner input = new Scanner(System.in); 


public Customer(int customerID, String accountType, String currencyType, int balance) 
{ 
    this.accountType = accountType; 
    this.currencyType = currencyType; 
    this.customerID = customerID; 
    this.balance = balance; 
    this.amount = amount; 
} 

public Customer() { 
    // TODO Auto-generated constructor stub 
} 

public int deposit(int amount){ 

    amount = input.nextInt(); 
    if (amount >= 500) { 
     System.out.println("Invalid"); 

    } 
    else{ 
     balance = balance + amount; 

    } 
    return balance; 
} 

public int withdraw(int amount){ 

    if (balance < amount) { 
     System.out.println("Invalid amount."); 
    } 
    else if (amount >= 500) { 
     System.out.println("Invalid"); 
    } 
    else { 
     balance = balance - amount; 

    } 
    return balance; 
} 


public void display(ArrayList<Customer> accounts) 
{ 
    System.out.println("CustomerID:" + accounts.customerID); 
    System.out.println("Account Type:" + accounts.accountType); 
    System.out.println("Currency Type: " + accounts.currencyType); 
    System.out.println("Balance:" + accounts.balance); 

} 

public void balance(ArrayList<Customer> accounts) { 
    System.out.println("Balance:" + accounts.balance); 
} 

} 
+0

運行應用程序時遇到的錯誤是什麼。更好地發佈它的堆棧跟蹤 – Bunti

+0

'java.util.ArrayList'不會具有諸如'customerID','accountType'等屬性或方法。 – MikeCAT

+0

錯誤:找不到或加載主類count.Numbers1 –

回答

0

java.util.ArrayList中不會有性能或如customerIDaccountTypecurrencyTypebalance和方法。

我想你的方法display()balance()應該沒有任何參數,應該使用實例的屬性來打印。

public void display() 
{ 
    System.out.println("CustomerID:" + customerID); 
    System.out.println("Account Type:" + accountType); 
    System.out.println("Currency Type: " + currencyType); 
    System.out.println("Balance:" + balance); 

} 

public void balance() { 
    System.out.println("Balance:" + balance); 
} 

然後,這些線路

operation.display(accounts.get(customerID)); 
operation.balance(accounts.get(customerID)); 

應該

accounts.get(customerID).display(); 
accounts.get(customerID).balance(); 

似乎有很多事情要糾正 - 例如,withdraw()使用。

+0

謝謝,它修復了大部分。由於某些原因,它仍然沒有打印 –

+0

謝謝,它修復了大部分。唯一的問題是,當我嘗試運行它並按3並輸入客戶ID時,它給我一個巨大的錯誤。 –