In this exercise, you will simulate a parking lot system where vehicles arrive and depart asynchronously. The parking lot has a fixed capacity, and your program will manage parking and departure processes concurrently using goroutines and channels.
len()
and cap()
functions to dynamically manage and log the parking lot's current occupancy and total capacity.When the program runs, the output might look like this:
[INFO] Parking lot initialized with 3 spaces. [ARRIVAL] Vehicle 1 is attempting to park... [PARKED] Vehicle 1 has parked. (Spaces left: 2) [ARRIVAL] Vehicle 2 is attempting to park... [PARKED] Vehicle 2 has parked. (Spaces left: 1) [ARRIVAL] Vehicle 3 is attempting to park... [PARKED] Vehicle 3 has parked. (Spaces left: 0) [ARRIVAL] Vehicle 4 is attempting to park... [WAITING] Parking lot full. Vehicle 4 is waiting for a space. [DEPARTURE] Vehicle 1 has left. (Spaces left: 1) [PARKED] Vehicle 4 has parked. (Spaces left: 0) ... [INFO] Simulation ended.
Project structure should look like this:
. ├── main.go
Your program should have the following structure and implement these functions:
main.go
package main import ( "fmt" "time" ) // newParkingLot creates a new parking lot with the specified capacity func newParkingLot(capacity int) chan int { fmt.Printf("[INFO] Parking lot initialized with %d spaces.\n", capacity) return make(chan int, capacity) } // parkVehicles simulates vehicles arriving and parking in the parking lot func parkVehicles(parkingLot chan int) { } // leaveVehicle simulates vehicles leaving the parking lot func leaveVehicle(parkingLot chan int) { } func main() { parkingLot := newParkingLot(3) // Create a parking lot with 3 spaces // Start goroutines for parking and departure go parkVehicles(parkingLot) go leaveVehicle(parkingLot) // Run the simulation for 15 seconds time.Sleep(15 * time.Second) fmt.Println("[INFO] Simulation ended.") }
cap()
and len()
functions to check the capacity and current occupancy of the channel.time.Sleep()
to simulate random delays for arrivals and departures.main()
exits, so there’s no need for explicit shutdown logic.