我已经搜索了互联网上的问题的答案,我有和似乎无法得到我正在寻找的答案,我有两个类和应用程序类和一个用户类,我提示用户输入一个新用户,或者返回已设置为保存用户对象的数组列表中的用户的结果。应用程序结束后,我期望数组列表继续存放对象,以便在应用程序类中每次连续运行主方法时,都可以引用arrayList以便稍后进行交叉检查。在arrayList中存储用户创建的对象多次迭代
所以我的问题是,当主方法完成,我再次运行它,它是否重新创建所有我的对象和arrayList从头开始?
下面是我正在使用的两个类。首先是应用程序类,其次是用户。
import java.util.ArrayList;
import java.util.Scanner;
public class Application{
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
//Creating admin user object that will be able to access everything
Users admin = new Users();
Users result = null;
//creating a new user that is not an admin
System.out.println("Are you a new user?");
String answer = null;
answer = in.nextLine();
if(answer.equalsIgnoreCase("YES") || answer.equalsIgnoreCase("Y")) {
result = admin.addNewUser();
result.addUsertoArrayList(result);
}else {
result.displayUsers(result.users);
}
}//End of Main Method.
}//End of Application Class
import java.util.ArrayList;
public class Users extends Application {
private String username;
private double biWeeklyIncome = 0;
private String password;
private String email;
// ArrayList to store all the objects of Type Users.
ArrayList<Users> users = new ArrayList<Users>();
// Default Constructor.
Users() {
}
// Constructor that takes a string as a name parameter.
public String name;
Users(String name) {
this.name = name;
}
// Setter Methods.
// User name
public void setUsername() {
System.out.println("Enter the username that you wish to go by:\n Ex. bigBoss45");
username = in.nextLine();
}
// Income
public void setBiWeeklyIncome() {
System.out.println("Enter your current bi-weekly income: \n Ex. 4500.00");
try {
biWeeklyIncome = Double.parseDouble(in.nextLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Password
public void setPassword() {
System.out.println("Enter the password that you wish to access your account with:\n Ex. bigBoss45");
password = in.nextLine();
}
// Email
public void setEmail() {
System.out.println("Enter a valid email address: \n Ex. [email protected]");
email = in.nextLine();
}
// Getter Methods
// User name
public String getUsername() {
return username;
}
// Income
public double getBiWeeklyIncome() {
return biWeeklyIncome;
}
// Password
public String getPassword() {
return password;
}
// Email
public String getEmail() {
return email;
}
// Method to create a new user
public Users addNewUser() {
String name = null;
System.out.println("Enter the name of the new user\n Ex.John Smith");
name = in.nextLine();
// Creating the new
Users newUser = new Users(name);
// Setting the new users information
newUser.setUsername();
newUser.setPassword();
newUser.setBiWeeklyIncome();
newUser.setEmail();
//adding the new user to the users arrayList
displayUsers(users);
return newUser;
}// end of addNewUser method.
//Method that is going to add a new user to the array List.
public void addUsertoArrayList(Users nUser) {
users.add(nUser);
}
public void displayUsers(ArrayList<Users> users) {
// Printing out the user added to the array list for testing purposes.
for (Users user : users) {
System.out.println(user.getUsername());
}
}//End of displayUser method.
}
我是一种新的Java和所有的面向对象的,所以任何的帮助深表感谢,我感谢你抽出时间来看看我的代码!
这是通过不结束程序(无限循环)完成的,如果用户输入“exit”,则关闭程序,否则执行迭代。如果要永久存储值,请使用数据库(例如,如果不想设置数据库,则使用SQLite)。 – maraca