Hello guys! So I'm trying to create this online shopping system for a project. I was wondering if there is a way to gather user input piece by piece then consolidating them in an ArrayList? The first part I'm working on is the registration part. I'm planning to make the program run on Netbeans. So, if the user registers it would look like this:
"Input information: "
"Name:"
"Username:"

How do I get each input and consolidating the inputs under the correct customer in the correct index of the ArrayList? Or is there an easier way of doing this? Sorry guys still practicing :P

You can use a hashmap, where you have pairs of key->value

For example, you can set the key to be the nickname and because key's cannot exist more than once, that will restrict users to have unique nicknames( as you'd expect it to be). Here's an example of what it would look like:
{Nickname : (name, lastname, other info)}

example
{Slavi : (Slavi , SlavisLastName, welovejava)}
etc ...

then what you do is, you search someone by the nickname and you get the values stored to that nickname

or ... if you insist on using arraylists .. you could create a class that stores all the information and then create object for each customer and store the objects in an array list.

Though I think hashmap is the way to do it as look ups require significantlly less time(As far as I remember)

why not simply make a Customer class and store instances of that in your List?

Here's my attempt at it:

        System.out.println("User registration");
        System.out.println("Full name: ");
        regName = sc.nextLine();

        System.out.println("Username: ");
        regUserName = sc.nextLine();

        System.out.println("Password: ");
        regPassword = sc.nextLine();

        System.out.println("Address: ");
        regAddress = sc.nextLine();

        System.out.println("Contact number: ");
        regContactNo = sc.nextLine();

        System.out.println("Intial money deposit: ");
        regDeposit = sc.nextDouble();

        User user = new User(regName,regUserName, regPassword, regAddress, regContactNo, regDeposit);

        for(int i=0; i<users.size();i++){
            if(users.get(i).getName().equals(regName) && users.get(i).getUsername().equals(regUserName) && users.get(i).getContactNo().equals(regContactNo) )
                System.out.println("User exists!");

            else
                users.add(user);     
        }

Problem is that it keeps printing out "User exists!" even if the registered user isn't.

A couple of suggestions:

override equals(Object 0) for your User class to test if two Users are "the same" by comparing their name, regName etc. Then you can use List's contains method directly

use the enhanced for loop when possible - its easier to read:

for (User u : user) {
   ...

So by overriding, you mean I create an equals(Object o) method under my User class? Sorry I'm still learning all this :P

Yes, that's right.
Preceed it with a @Override annotation so the compiler will check that you've done it right.

    @Override
    public boolean equals(Object o) {
       // first test - if o isn't a User then ity cannot be equal
       if (! (o instanceof User)) return false;
       // now we know o is an instance of User...
       User other = (User) o;
       return (name.equals(other.name) etc etc );
    }

Here's a decent tutorial on the subject: http://tutorials.jenkov.com/java-collections/hashcode-equals.html

Note you should also override hashCode (see tutorial for explanation), but assuming userName is unique you can use its hashcode for a quick solution

    @Override
    public int hashCode() {
        return userName.hashCode();
    }

I have another question might sound stupid though. How long does an object stay in the ArrayList? For example, if I go through an iteration of my program and I register a user; after registering, the program ends. If I run the program again, will I be able to login with the user I registered in the previous iteration? I'm using Netbeans btw.

it stays in there until either you remove it or the List goes out of scope.
That happens whenever the block of code it was declared in goes out of scope, or the JVM terminates.

If you want to persist your data between application runs, you need to write it out to a file, a database, or whatever.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.