所以我創建了一個使用ArrayList的銀行賬戶程序。該程序顯示一個菜單,客戶可以存入,提取,顯示帳戶信息並查看餘額。數組列表存儲客戶對象,並且應該能夠在用戶存款/取款等情況下更新。Java ArrayList銀行賬戶
這是我到目前爲止,我不確定如何調用存款,取款,顯示帳戶信息,並檢查Customer類中的餘額,並讓它們正確訪問數組列表對象。
任何關於我失去的幫助或建議都會非常有幫助。
現在我收到的錯誤是:找不到符號 符號:變量的customerID 位置:類java.util.ArrayList所有在顯示和平衡法的變量,
找不到符號 符號:構造客戶() 位置:類taylor.Customer當我嘗試創建一個客戶實例,
顯示器(java.util.ArrayList中)在taylor.Customer不能適用於(taylor.Customer),當我嘗試調用方法並傳入數組列表帳戶
測試類:
package taylor;
import java.util.ArrayList;
import java.util.Scanner;
public class TestBank{
public static void main(String args[]){
ArrayList<Customer> accounts = new ArrayList<Customer>();
Customer customer1 = new Customer(1, "Savings", "US Dollars", 800);
Customer customer2 = new Customer(2, "Checking", "Euro", 1900);
Customer customer3 = new Customer(3, "Checking", "US Dollars", 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");
}
}
}
}
客戶類:
package taylor;
import java.util.Scanner;
import java.util.ArrayList;
public class Customer{
String accountType, currencyType, info;
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 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);
}
}
這是很多代碼......究竟是什麼問題? – Mureinik
在使用點(。)運算符實例調用Java操作/方法時。所以在你的情況下,只需調用BankAccount instanceName.methodName(如果需要,傳遞任何參數) –
如果您在查找* BankAccount實例時遇到問題,請考慮使用Map而不是ArrayList。使用'customerID'映射到'BankAccount'。 (雖然這可以通過使每個'customerID'順序並簡單地使用它們索引到你的ArrayList中來實現)。 – River