Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to detect If this class object is duplicated or not

OK so I have 2 classes that are related to each other without the use of inheritance
class1:

public class Userformat {
    private String name;
    private int id;

    public Userformat(String name, int id){
        this.name = name;
        this.id = id;
    }

    public String toString(){
        return "\nname: "+ name + "\nID: " + id + "\nbooks borrowed: " + booksborrowed + "\n";
    }
}

class2 :

import java.util.ArrayList;
public class UserList {
    private ArrayList<Userformat> Users;

    public UserList() {
        Users = new ArrayList<Userformat>();
    }

    public void adduser(Userformat userformat) {
        Users.add(userformat);
    }
   
   public boolean checkuserduplicated(String usertitle) {
        boolean pass = false;
        String dieusertitle = usertitle.toLowerCase();
        for (int i = 0; i < Users.size(); i++){
            Userformat u = Users.get(i);
            if (u.getName().toLowerCase().equals(dieusertitle)) {
                pass = true;
            } else {
                pass = false;
            }
        }
        return pass;
    }
}

so basically what happens here is whenever I create a new class object using Userformat() I automatically store it in the UserList()’s array list
what I am struggling with how to stop the .add() method from work when the Userfomat() object has a similar name. TO MAKE IT MORE CLEAR this is my main

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

UserList users = new UserList();

Scanner input = new Scanner(System.in);
System.out.println("Enter your Name: ");
String nameValue = input.nextLine();

Scanner input2 = new Scanner(System.in);
System.out.println("Enter your ID: ");
int idValue = Integer.parseInt(input2.nextLine());

if (!users.checkuserduplicated(nameValue)) {
           users.adduser(new Userformat(nameValue, idValue));
        }

but this doesn’t work can someone please help me Thanks

>Solution :

The issue with the current implementation of checkuserduplicated method is that it returns false as soon as it finds a user with a different name than the one being checked. This means that if there are multiple users with same names but the last name is different it will return false.

To fix this, you can update the checkuserduplicated method as follows:

public boolean checkuserduplicated(String usertitle) {
    String dieusertitle = usertitle.toLowerCase();
    for (int i = 0; i < Users.size(); i++) {
        Userformat u = Users.get(i);
        if (u.getName().toLowerCase().equals(dieusertitle)) {
            return true; // Found a user with the same name, so return true
        }
    }
    return false; // Did not find any user with the same name, so return false
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading