collision934
collision934

Reputation: 31

Car Park in Java, decrementing spaces

I'm creating a car park in Java, I have one entrance and one exit. The car park has 20 spaces and and I think I've coded the entrance/exit correctly, I have been asked to use threading.

import java.io.*;

public class CarPark {
    private int spaces;

    public CarPark(int spaces)
    {
        if (spaces < 0) {
            spaces = 0;
        }

        this.spaces = spaces;
    }

    public synchronized void entrance() //enter car park
    {
        while (spaces == 0) { 
            try {
                wait();
            } catch (InterruptedException e)
            {

            }
        }
        spaces --;
    }

    public synchronized void exit() // exit car park
    {
        spaces++;
        notify();
    }
}   

I'm just a bit stuck on how to represent the actual parking garage itself and create the spaces which can be decremented or incremented as a car approaches or leaves. I have coded the below so far for the main method:

public static void main(String[] args){     
    CarPark parkingGarage = new CarPark(20); //20 spaces    
}

Also when the spaces are full, cars need to queue for a space, it would be best to represent this as an integer I think. You may assume that cars cannot enter and leave simultaneously (i.e. the system will prevent this anyway from happening as a consequence of locking)

Eventually I need to make this into a client - server system.

Any advice is much appreciated!

Upvotes: 4

Views: 3403

Answers (3)

James
James

Reputation: 1501

What you've done so far looks okay to me. All you need to do is use your CarPark object 'parkingGarage' to simulate a car entering or exiting.

For example:

parkingGarage.entrance();

This would simulate a car entering the garage and thus spaces would decrement.

Similarly, .exit() would do the same thing but increment the number of spaces. Although you would need to add checks to ensure that an exit can only occur if an entrance has occurred. For example, you could not have 10 entrances and 20 exits and the number of spaces would become negative.

Also, you could implement some sort of 'queue' so that when there are no spaces, the car waiting the longest will get a space first.

Threading allows you to run bits of code 'concurrently', could you please explain further what it is exactly you want/are trying to do.

EDIT:

After reading Akshay's comment, it cleared things up for me. Like they said, you could create two threads, one which handles entrances and one which handles exits and have each of these generate a random amount of time before the next car enters or exits. You would do this using:

Thread.sleep(randomAmountOfTime)

Note: The parameter for this is handled in milliseconds. Again, make note of my previous argument of not having more exits than entrances.

After this you could print out some sort of log which outputs a message when a car exits or enters or even have a dynamic 'sign' which displays the number of free spaces which would change when cars enter or exit.

Hope this helps.

Upvotes: 1

John Hascall
John Hascall

Reputation: 9416

My opinion is each thread should represent a car. So I would have the main thread go into a loop:

for (;;) {
    Thread.sleep(randomAmountOfTime);
    // a new car has shown up
    spawn a new carThread
}

And each car thread would be something like:

System.out.println("Car " + carId + " has arrived");
parkingGarage.entrance();
System.out.println("Car " + carId + " has parked");
Thread.sleep(randomAmountOfTime);
parkingGarage.exit();
System.out.println("Car " + carId + " has left");

Upvotes: 1

Akhi Youngisthan
Akhi Youngisthan

Reputation: 33

i think your code and logic is fine.. i have some suggessions,

  1. use multiple entry and exit door for car, so that your system will become advanced one
  2. Use the concept of arraylist so that you can dynamicaly increase the limit for your number of car. i this particular code you use only 20 cars.

Upvotes: 0

Related Questions