Java ArrayList银行账户
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java ArrayList银行账户相关的知识,希望对你有一定的参考价值。
所以我正在创建一个使用ArrayList的银行帐户程序。该程序显示一个菜单,客户可以在其中存放,取款,显示帐户信息和查看余额。数组列表存储客户对象,如果用户存款/取款等,应该能够更新。
这是我到目前为止所做的,我不确定如何调用存入,取款,显示帐户信息和检查Customer类中的余额的方法,并让他们正确访问数组列表对象。
对于我所缺少的任何帮助或建议都会非常有帮助。
我现在得到的错误是:找不到符号符号:变量customerID位置:类java.util.ArrayList表示显示和平衡方法中的所有变量,
找不到符号符号:构造函数Customer()位置:类taylor.Customer当我尝试创建一个Customer实例时,
taylor.Customer中的display(java.util.ArrayList)不能应用于(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);
}
}
问题是你永远不会初始化BankAccount中的任何字段。从BankAccount构造函数中删除void
,使其成为构造函数,并在TestBank中构建类时使用该构造函数。
由于这些字段与Customer中的字段重复,似乎最好像customer.deposit(amount)
等一样。删除BankAccount类并将方法移动到Customer并将参数int amount
添加到使用amount的方法中。然后在您的TestBank类中更改存款等方法调用以获得金额参数。
此外,您可以摆脱Customer中的getter和setter方法。
以上是关于Java ArrayList银行账户的主要内容,如果未能解决你的问题,请参考以下文章