我正在创建银行演示并有一个菜单选项,用户可以选择开设帐户。当用户选择该选项(来自主页类)时,我会调用另一个名为account的类,该类已设置并获取名字和姓氏的方法以及帐号。然后我有一个accountDemo告诉用户输入他们的信息,然后将它存储在arrayList中并返回到home。问题是当我再次选择相同的选择并输入新信息时,arrayList中的先前旧信息不存在,概率被覆盖。当用户选择打开新帐户的选项时,如何继续更新arrayList。我在订单中看到他们有一个ArrayList getList方法,它返回arrayList,但我仍然卡住了。这是我的代码;感谢
public class account
{
private String firstName, lastName;
private int accountNumber;
public account()
{
firstName = " ";
lastName = " ";
accountNumber = 0;
}
public account(String firstName, String lastName, int accountNumber)
{
this.firstName = firstName;
this.lastName = lastName;
this.accountNumber = accountNumber;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public void setAccountNumber(int accountNumber)
{
this.accountNumber = accountNumber;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public int getAccountNumber()
{
return accountNumber;
}
public String toString()
{
return "Account Information [First Name = " + firstName + " Last Name = " + lastName + " account Number = " + accountNumber + "]";
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class accountDemo
{
private ArrayList<account> arry = new ArrayList<account>();
public void makeNewAccount()
{
String firstName, lastName;
int accountNumber;
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to opening a new account");
System.out.println("--------------------------------");
System.out.println("Please enter your first name");
firstName = keyboard.nextLine();
System.out.println("Please enter you last name");
lastName = keyboard.nextLine();
System.out.println("Please enter a 4 digit account number");
accountNumber = keyboard.nextInt();
String validityOfAccountNum = Integer.toString(accountNumber); // converting the integer account number to a string so we can check the length of the user input
if(validityOfAccountNum.length() != 4) // checking if the length of the given length is greater or less than 4 digits
{
System.out.println("The length of your account number is invalid");
System.out.println("Please try again");
makeNewAccount();
}
else
{
//account info = new account(firstName, lastName, accountNumber); // creating a new account that passes in first, last name, and account number
System.out.println("Storing account number......");
System.out.println("You have successfully opened a new account");
/**
arry = new ArrayList<account>(); // creating a new array list
arry.add(new account(firstName, lastName, accountNumber)); // adding the given information to the list
for(int i = 0; i < arry.size(); i++)
{
System.out.println("In the list is");
System.out.println(arry.get(i));
}
**/
System.out.println("--------------------------------");
System.out.println("Testing");
accountDemo list = new accountDemo();
System.out.println(list.getList());
System.out.println("--------------------------------");
System.out.println("--------------------------------");
System.out.println("Testing");
storeInfo(firstName, lastName, accountNumber);
System.out.println("Going to Home Page......");
System.out.println("--------------------------------");
ATMHomePage home = new ATMHomePage();
home.menuChoice();
}
}
public ArrayList<account> getList()
{
return this.arry;
}
public ArrayList<account> storeInfo(String firstName, String lastName, int accountNumber)
{
arry.add(new account(firstName,lastName,accountNumber));
for(int i = 0; i < arry.size(); i++)
{
System.out.println("In the list is");
System.out.println(arry.get(i));
}
return arry;
}
}
import java.util.Scanner;
public class ATMHomePage
{
public static void main(String [] args)
{
ATMHomePage homePage = new ATMHomePage();
homePage.menuChoice();
}
public void menuChoice()
{
Scanner keyboard = new Scanner(System.in);
int userInputChoice;
System.out.println("Welcome to the ATM" + "\n");
System.out.println("Please choose from one of the following choices:");
System.out.println("1. Deposit " + "\n"
+ "2. Withdraw " + "\n"
+ "3. Check Balance " + "\n"
+ "4. Open a New Account " + "\n"
+ "5. Quit" + "\n");
userInputChoice = keyboard.nextInt();
if(userInputChoice == 1)
{
Deposit deposit = new Deposit();
deposit.Introduction();
}
else if(userInputChoice == 2)
{
// go to a new class
}
else if(userInputChoice == 3)
{
// go to a new class
}
else if(userInputChoice == 4)
{
accountDemo info = new accountDemo();
info.makeNewAccount();
}
else if(userInputChoice == 5)
{
System.out.println("Good-Bye");
System.exit(0);
}
else
{
System.out.println("You have entered a wrong chocie");
System.out.println("Please try again");
System.out.println("--------------------------------------------------");
menuChoice();
}
}
}
答案 0 :(得分:0)
您不断创建新的accountDemo()对象。每次这样做都会成为一个新对象。
您需要创建一次对象,并一次又一次地引用它。研究一个称为变量范围的概念。
答案 1 :(得分:0)
您应该使用正确的Java语法并将所有类名称大写:
public class Account
public class AccountDemo
从第4项中删除accountDemo info = new accountDemo()。
else if(userInputChoice == 4)
{
info.makeNewAccount();
}
将该语句放在公共类ATMHomePage的开头。
public class ATMHomePage
{
AccountDemo info = new AccountDemo();
...
这将创建一个且只能在ATMHomePage类中引用的AccountDemo对象。
删除accountDemo list = new accountDemo();对于您的AccountDemo类,方法为makeNewAccount()。 将list.getList()更改为getList()。
System.out.println("Testing");
System.out.println(getList());
我甚至会更进一步,将帐户信息列入自己的方法。
答案 2 :(得分:0)
else if(userInputChoice == 4)
{
// This line should not occur here since you are creating a new object
// accountDemo info = new accountDemo();
// every time a new. You should be creating it once and reference it as follows
info.makeNewAccount();
}