views:

350

answers:

4
public class Start {
    public Register theReg = new Register();
    public static Start go = new Start();

    public static void main(String[] args) {
        Register theReg = new Register();
        go.regUsers();

        if(theReg.logIn("jsmith","password")) {
            System.out.println("You're logged in as " +
                               theReg.userLoggedIn.getName());
        } else {
            System.out.println("dang");
        }
    }

    public void regUsers() {
        Student regJoe =
            theReg.regSeniorStaff("Joe smith", "password", "jsmith", 1);
    }
}

public class Register {
    public ArrayList<People> users;
    public People userLoggedIn;

    public Register () {
        users = new ArrayList<People>();
        users.add(new Student("john","password","jo",1));
        userLoggedIn = null;
    }

    public Student regStudent(String name, String password,
            String username, int stuId) {
        Student s = new Student(name, password, username, stuId);
        users.add(s);
        return s;
    }
}

I'm thinking I've just missed out something silly. Like....

The start methods will create a new Register object, which has its ArrayList. Then a bit down the line it starts registering users using the methods like regStudent. But, only the constructor on Register lets me add objects to the ArrayList; calling the methods to do the same thing later on just doesn't add them. It creates the object but can't add them. Also can't remove things, only .get works in them.

Any help would be great, thanks!

+1  A: 

Do you know how to use classes? The naming in this example doesn't make a lot of sense. Why does a "Register" have a "userLoggedIn" "People" object? Wouldn't it make more sense if that was a "Person" object?

Register r = new Register().
r.regStudent( ... );
r.regStudent( ... );

Should work fine.

My guess is that you have something like this:

public static void registerStudent()
{
   Register r = new Register();
   r.add( ... );
}

Am I right?

You assume we can read minds, so I gave it a shot. It sounds like you may be repeatedly instantiating your register object, rather than actually holding a single reference and calling methods on it. That would create the behavior you described.

Edit: It looks like you don't know how to use classes. Here's a tutorial:
http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html

Stefan Kendall
No, actually the way you did it first is how i'm doing it (shown in the updated question, i added more code). The code i've put up isn't the whole 2 classes, the rest of them shouldn't really help in solving the problem i don't think and kind of over complicates things. Thanks for the input though.
graeme
I still think you should read some kind of OOP tutorial. Your design makes no sense. If Register holds a list of registered students, can only one person log in at a time? The class names "Register", "Start", and "People" don't correspond to their actual real-life meanings, and your method and variable naming conventions need help.I was trying to be nice, but your code needs serious work.
Stefan Kendall
+2  A: 

This would be a good time to learn how to use a debugger. Eclipse has a good debugger; you can set a breakpoint on the line users.add(s) and inspect the contents of the list users.

You might also want to double-check that your regStudent method is being called during execution. If you're using Eclipse, you can right-click and find all references to that method in your workspace, and make sure one of those is being called during execution.

rob
Thanks, i had tried running the debugger earlier (i'm using eclipse). I'll have another go.
graeme
+1  A: 

Your error is that you have two variables names theReg - one is an instance member of the Start class, the other a local variable in the main() method. Then you add a person to one of these Register instances and ask the other one whether the person has been added.

Michael Borgwardt
You are correct sir! Thank you and much love!
graeme
+3  A: 

It looks as though you are hiding your theReg global var. You are most likely querying the global and seeing that only the constructor-added item is in your ArrayList.

public class Start {
    public Register theReg = new Register(); //<<---- your global var
    public static Start go = new Start();

    public static void main(String[] args) {
        Register theReg = new Register();  // <<---- hiding the global
        go.regUsers();

        if(theReg.logIn("jsmith","password")) 
          System.out.println("You're logged in as " + theReg.userLoggedIn.getName());
        else 
          System.out.println("dang");
    }
}
akf
This is the problem, thanks for pointing it out. Something i didn't know/didn't realise before!
graeme