Search code examples
javaoopcomputer-science

prevent duplicate (Unique Passport)


I'm trying to prevent user to add 2 attendee with the same passport I tried many things as making Boolean variable to check if attendee was added before or no but I have failed all of my attempts can't really think of a way to make this

public class Test {
    public static void main(String[] args) {
        try {
            Workshop Available_Work_Shops = new Workshop();
            Scanner sc = new Scanner(System.in);
            String passportNumber, workshop;
            boolean isAttendeeMatch;
            ArrayList<Attendee> attendeeList = new ArrayList<>();
            int choice;
            do {
                System.out.println("1. Add New Attendee");
                System.out.println("2. Add Existing Attendee to Workshop");
                System.out.println("3. Remove Attendee from Workshop");
                System.out.println("4. Print WorkShop List");
                System.out.println("5. Print All Attendees");
                System.out.println("0. Close Program");
                System.out.println("Please Enter a number to choose");
                choice = sc.nextInt();
                sc.nextLine();
                switch (choice) {

                    case 1 -> {
                        System.out.println();
                        System.out.println("Adding New Attendee");
                        System.out.println("____________________");
                        System.out.print("Please Enter Attendee Name: ");
                        String Attendee_name = sc.nextLine();
                        System.out.print("Please Enter Attendee Passport Number: ");
                        String Attendee_passport = sc.nextLine();
                        System.out.print("Please Enter Attendee Age: ");
                        String Attendee_Age = sc.nextLine();
                        System.out.print("Please Enter Attendee Phone Number: ");
                        String phone = sc.nextLine();
                        attendeeList.add(new Attendee(Attendee_name, Attendee_Age, Attendee_passport, phone));
                        for (Attendee attendee : attendeeList) {
                            if (attendeeList.contains(attendee.PassportNumber)) {
                                System.out.println("Existing User Found please enter '3' to remove the duplicate user");
                            }
                        }
                    }

Solution

  • The Issue

    Let's focus on these two lines

    ...
    for (Attendee attendee : attendeeList) {
      if (attendeeList.contains(attendee.PassportNumber)) {
    ...
    

    Since you've defined the attendeeList to be an ArrayList holding Attendee objects, you should be passing Attendee objects to attendeeList.contains(). While the contains() function does accept any Object, it will only return true if one of the List's elements .equals() the object you pass in.

    The Solution

    I'd recommend changing this block of code...

    attendeeList.add(new Attendee(Attendee_name, Attendee_Age, Attendee_passport, phone));
    for (Attendee attendee : attendeeList) {
      if (attendeeList.contains(attendee.PassportNumber)) {
        System.out.println("Existing User Found please enter '3' to remove the duplicate user");
      }
    }
    

    ... to something like this

    Attendee potentialAttendee = new Attendee(Attendee_name, Attendee_Age, Attendee_passport, phone)
    // Check if the potentialAttendee's passport number is already used by someone in the list.
    boolean passportRegistered = false;
    for (Attendee attendee : attendeeList) {
      if (attendee.PassportNumber.equals(potentialAttendee.PassportNumber) {
        System.out.println("That user passport number has already been registered. It can't be added again.");
        passportRegistered = true;
      }
    }
    // Only add the potentialAttendee if his/her passport number wasn't already used by someone in the list.
    if (!passportRegistered) {
      attendeeList.add(potentialAttendee);
    }
    

    The key difference is that you shouldn't add the potentialAttendee to the list until you've first validated that he/she meets the requirements to be added to that list.

    Potential Enhancements

    • Store the Attendees in a HashMap instead of an ArrayList. If you key off of the PassportNumber, then by definition you can't have two participants with the same PassportNumber in the collection of attendees.
    • Java has a nice Streaming API that could make the validation even closer to a one liner.