-1

Does anyone has any idea on how can I get the value of a function that indicates if there are any empty parking spot on the block or not. I am using a Queue, and I tried the function nFree() and spaceIndex(), but I get that the two functions are not defined for the type

XPD
  • 1,121
  • 1
  • 13
  • 26

1 Answers1

-1

Hope i understood you question, below is the code that may return all the parking empty spot.

public class Parking {

    static int parkingSize = 10;

    static Object[] object = new Object[parkingSize];

    public static void main(String[] args) {

        add("java", 10);

        System.out.println(checkEmptyParkingSpot());

        emptyParkingSpot(10);

        System.out.println(checkEmptyParkingSpot());

    }

    // add the object in specicif location
    public static void add(String info, int position) {

        // if the position does not exist an erro will be thow
        if (position > object.length ) {
            throw new RuntimeException("The possition indicated does not exist in the park");
        }

        if (object[position - 1] != null) {
            throw new RuntimeException("The possition is ocupied");
        }

        object[position - 1] = info;

    }

    // to remove an object from the park and free the space
    public static void emptyParkingSpot(int position) {

        if (position > object.length ) {
            throw new RuntimeException("The possition indicated does not exist in the park");
        }

        if (object[position - 1] == null) {
            throw new RuntimeException("The possition isalready empty");
        }
        object[position - 1] = null;
    }

    // lists the empty spot 
    public static String checkEmptyParkingSpot() {

        String emptySpace = "".trim();

        for (int i = 0; i < object.length; i++) {

            // list the empty space
            if (object[i] == null) {
                emptySpace = emptySpace + (i + 1) + "\n";
            }

        }

        // if there is no space the message will be park full
        if (emptySpace.isEmpty()) {
            emptySpace = "Park is Full";
        }

        return emptySpace;
    }

}
lczapski
  • 4,026
  • 3
  • 16
  • 32
bie
  • 1
  • 1
    sorry, is this in any way related to AnyLogic? The question is about that tool and it's specific road-traffic library, your code seems some generic code that won't help here. Or am I missing something – Benjamin Nov 19 '19 at 10:15
  • thank you so much, but I was thinking if I can add some codes in "on enter", "on exit" field. because to be honest I am very new to Anylogic, and I still do not know how to link java code with the model I am developing. So Sorry – dihaben Nov 22 '19 at 02:36