dimanche 21 avril 2019

How do I search for null in this ArrayList and then replace it with another Object?

I have been writing code of a car parking structure for a bit now and i've gotten kinda stuck. So far I have an ArrayList that the user adds Vehicle properties to aswell as an ArrayList for a parking space with a "SpaceID" and the "Vehicle" from earlier.

So far I can make it so that the user adds the vehicle, and the vehicle gets added to a parking space, However I have made a temporary parking space with the index 0: and the element (vehicle) being null.

From here, I wanted to check the parking space ArrayList for "null" and then if it's found, replace null with the vehicle, However, I do not know how to go about implementing this. I will attach the current code i'm using, or at least a minimal version of it.

I have already tried using a Contains(null) method, but I can't seem to get that to work properly, I'm not sure if it even can work, but I have since then removed it from my code.

First of all, I have the function to create the vehicle and store it in an Array.

```
public void carInfo(Vehicle tempVehicle, parkingSpace vehicle) {

    array = new MCP();

    System.out.println("Please enter number plate: ");
    input = new Scanner(System.in);
    String plate = input.nextLine();

    System.out.println("Please enter car make: ");
    input = new Scanner(System.in);
    String make = input.nextLine();

    System.out.println("How would you best describe your Vehicle? ");
    System.out.println("(Car, Small Van, Tall Van, Long Van, Coach, 
    Motorbike)");
    type = new Scanner(System.in);
    String type1 = input.nextLine();

    if (type1.equalsIgnoreCase(vehicleType.CAR.toString())) {
        tempVehicle.setPlate(plate);
        tempVehicle.setCarMake(make);
        tempVehicle.setVehicle(vehicleType.CAR);
        inv.createInvoice();
        tempVehicle.setInvoiceNumber(inv.invoiceNumber);

        array.addStandard(tempVehicle);
        array.parkVehicle(vehicle);

        System.out.println(tempVehicle.toString());
        System.out.println(vehicle.toString()); 
```

I also have my Arrays, which are on another class.

```
    public MCP(){
       vehicles = new ArrayList<>();
       parkingSpaces = new ArrayList<>();

       parkingSpaces.add(0, null);
       parkingSpaces.add(1, null);

     }


    public void addStandard(Vehicle tempVehicle) {
       vehicles.add(tempVehicle);
     }

     public void parkVehicle(parkingSpace vehicle) {
       parkingSpaces.add(vehicle);
     }
```

This is the way I tried to do it, but I couldn't figure this way out, so I stopped, i'm open to any other ways too. // public void checkIfEmpty(parkingSpace vehicle){ // if(parkingSpaces.contains(null)){ // parkingSpaces.add(vehicle); // } // else{ // System.out.println("There is no room in this Zone"); // } // }

I am also looking for a better way to populate parking spaces, but that's not the main concern, just something else just incase someone has any ideas

Thanks in Advance.

Aucun commentaire:

Enregistrer un commentaire