I have searched the internet for an answer to the problem that I am having and cannot seem to get the answer that I am looking for, I have two classes and application class and a users class, I am prompting the user to enter either a new user or return the results of the users already in an array list that is set up to hold users objects. After the application ends, I expect the array list to continue to house the objects so that with each successive run of the main method in the application class I can refer to the arrayList for cross checking later on.
So my question is, When the main method is completed and I run it again, does it re-create all my objects and arrayList starting from scratch?
Below are my two classes that I am working with. Application class first, and Users second.
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.
}
I am kind of new to Java and all the object orientation's so any help is much appreciated and I thank you for taking the time to look at my code!