Home > OS >  While trying to use contain() in Arraylist it doesn't find what I'm looking
While trying to use contain() in Arraylist it doesn't find what I'm looking

Time:12-23

So I have a class named Student like this:

public class Student {
    public int usercounter;
    public String username,password,studentname;
    Student(String studentname ,String username, String password, int usercounter){
        this.studentname = studentname;
        this.username = username;
        this.password = password;
        this.usercounter = usercounter;
    }

and I'm trying to write a library login code using Arraylists.

static ArrayList<Student> users = new ArrayList<Student>();

My main menu looks like this:

public static void MainMenu() {
        while (menubreak){
            System.out.println("ULIS Main Menu\nPlease choose the feature you want to access:\n1-Login\n2-Create User\n3-Delete User\n4-Exit");
            gir = Input.nextInt();
            Input.nextLine();
                switch (gir) {
                    case 1:
                        Login();
                        break;

and I create users like this:

public static void CreateUser() {
        while (userbreak) {
            System.out.println("You are creating a new User");
            System.out.println("Please enter the name of the Student:");
            tempStudentname = Input.nextLine();
            System.out.println("Please enter a new username:");
            tempUsername = Input.nextLine();
            System.out.println("Please enter a new password:");
            tempPassword = Input.nextLine();
            users.add(new Student(tempStudentname, tempUsername, tempPassword, usercounter));
            usercounter  ;

and my login screen looks like this:

public static void Login() {
        System.out.println("Please choose the user type you want to login:\n1-Admin\n2-Student");
        giris = Input.nextInt(); Input.nextLine();
            switch (giris) {
                    case 2:
                    System.out.println("Please enter your username:");
                    tempUsername = Input.nextLine();
                    searchUsername = tempUsername;
                    System.out.println("Please enter your password:");
                    tempPassword = Input.nextLine();
                    if(users.contains(searchUsername) && users.contains(tempPassword) ){
                        System.out.println("User found, logging in now");
                        UserMenu();
                    }
                    else{
                        System.out.println("User cannot be found, returning to Main Menu");
                    }
            }
    }

When I run this it goes to "User cannot be found" I'm not allowed to use an offline database or smth like that that's why I'm creating usernames on the go and don't store them in something. What could be the reason it doesn't find the username and password it's looking for? btw I'm a first year student if you can please try to explain things without using complex techniques

CodePudding user response:

You are currently comparing the Student object with the name of the student, but theres an easy fix, suppose we had this code:

List<Student> students = new ArrayList<>();
students.add(new Student("Bob", "password"));
students.add(new Student("Alice","wonderland"));

Then when you get tempUsername and tempPassword from the user input, you could loop through the list and check if there is a student with that name and password:

Student loggedIn = null;
for(Student student : students){
    if(student.getName().equals(tempUsername) && student.checkPassword(tempPassword)){
        loggedIn = student; 
        break;
    }
}

(Note here I added some getter and setter methods for student, which looks like this now for better encapsulation)

class Student {
    private String name;
    private String password;

    public Student(String name, String password){
        this.name = name;
        this.password = password;
    }

    public String getName(){
        return name;
    }

    public boolean checkPassword(String password){
        return this.password.equals(password);
    }
}

If you're specifically asked to do this with an ArrayList thats all good, but if you can use other data structures you could look into using HashMaps (you could have a HashMap<String,Student> where you could pass in the username of the student and get back the student if its in the HashMap, which would be better time complexity, or better yet, define your own comparable interface and use a HashSet, that way you could make the comparable compare usernames and that would also help prevent users with the same username being added).

  • Related