简体   繁体   中英

Checking if the ID of an element in an ArrayList is already there before adding it

I'm a beginner in Java and I want to create a small Parking System System. The system has four classes one for the user inputs, one with ArrayList maintaining list of Slots in the Parking, one ParkingSlot and one Car.

At the moment I'm struggling to find out how to compare the user input for slot ID against the existing slot IDs in the ArrayLis. At the moment it just overwrites it if the user enters an existing value.

I added the condition "if (!slots.contains(newId))" in the addParkingSLot method, but it doesn't seem to work.

The part of the Application class responsible for the user ID input:

choice = scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
    System.out.println("Enter an ID of the type \"D##\"");
    input = scanner.nextLine();
    if(input.matches("[D][0-9][0-9]")) {
        System.out.println("Nice");
        carParkObj.addParkingSlot(input, "Visitor", false);                                                                             
    }
    else {
        System.out.println("Invalid Input");
    }
    break;

Car Park Class:

public class CarPark {

    private ArrayList<ParkingSlot> slots;       

    public CarPark() {
        slots = new ArrayList<ParkingSlot>();

    }
    /**
     * storing Parking Slot 
     */

    public void addParkingSlot(String newId, String newType, boolean newOccupied)
    {
        ParkingSlot slotObj = new ParkingSlot(newId, newType, newOccupied);
        if (!slots.contains(newId)) {
            slots.add(slotObj);
        }
        else {
            System.out.println("This slot ID already exists");
        }
    }

When the user enters an existing value it gets added in the arrayList instead of getting the message that the slot ID already exists.

Your ArrayList has type ParkingSlot , but you are using .contains with type String . It returns false anyway. You can add a new method that will check if there is a car with a certain number in the park:

public boolean hasParkingSlot(String searching) {
  for(ParkingSlot slot : slots) {
    if(slot.getID().equals(searching)) return true;
  }
  return false;
}

I hope this helps :)

Your slots ArrayList contains ParkingSlot objects, not String objects, so you are effectively looking for a parking slot that is a string, and there is no such thing (this is colloquially called "comparing apples and oranges").

In an old-fashioned way you could write

boolean found = false;
for (ParkingSlot slot : slots) {
    if (slot.id.equals(newId)) {
        found = true;
        break;
    }
}

if (found) …

(I assumed a ParkingSlot has an accessible field called id , but you didn't show the definition of a parking slot. Still, you can see the intent even if it's not exactly the way I wrote it.)

Wrapping this code as a separate method, as suggested in the other answer by user Vega TV, is a good idea.

There are more concise ways to write this, but I'm suggesting this way since I think it's helpful for a beginner to see it done in small steps like this.

One such more concise way is to use the stream facilities:

boolean found = slots.stream().anyMatch(slot -> slot.id.equals(newId));

I typed that without actually trying it, so bear that in mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM