0

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!

1
  • This is done by not ending the program (endless loop) and if the user enters "exit" you close the program, otherwise you do an iteration. If you want to permanently store values use a database (e.g. SQLite if you don't want to set up a db). Commented Jul 19, 2015 at 1:30

2 Answers 2

1

Every time you run a command like java Application to run the program you will be starting up a new java process. No data from memory will be carried over from any previous executions of your java program.

If you want to store data so that it stays the same over multiple executions of your process you should consider externalising it to a file or a DB etc.

Sign up to request clarification or add additional context in comments.

Comments

0

When the application is shut down, the objects stored in memory are lost.

You can choose to persist desired objects. One way to do this is to serialize objects to a file before shutdown, then reload them on startup.

In your case, an easy way to do this would be to use Java serialization. You'll need to mark your serializable classes as implementing Serializable, give them a serialVersionUID field, and use ObjectOutputStream and ObjectInputStream.

By the way, your Users class represents two different things - a collection of users and a single user. Usually things will work better if each class represents one concept. Consider splitting your Users class into two classes, one of which holds a list of users, and the other representing a single user.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.