Parking Lot Management System in Rust
This challenge asks you to design and implement a simple parking lot management system in Rust. The system should allow for creating a parking lot with a specified number of parking spaces, parking vehicles, unparking vehicles, and querying the status of the parking lot (e.g., number of available spaces). This is a classic problem that tests your ability to manage data structures, handle user input, and implement basic object-oriented principles.
Problem Description
You are tasked with building a command-line application that simulates a parking lot. The application should accept commands from the user to manage the parking lot. The core functionalities are:
- Create Parking Lot: Initialize a parking lot with a given number of parking spaces.
- Park Vehicle: Park a vehicle (identified by a unique license plate) in the parking lot. If the lot is full, the operation should fail.
- Unpark Vehicle: Remove a vehicle from the parking lot based on its license plate. If the vehicle is not found, the operation should fail.
- Get Available Spaces: Return the number of available parking spaces in the lot.
- Get Vehicle Details: Return the license plate of all vehicles parked in the lot.
The system should handle invalid input gracefully and provide informative error messages.
Key Requirements:
- Data Structure: Choose an appropriate data structure to represent the parking lot and its vehicles. Consider using a
VecorHashMap. - Error Handling: Implement robust error handling to deal with invalid commands, full parking lots, and non-existent vehicles.
- Command Parsing: Parse user input to determine the command and its arguments.
- Clear Output: Provide clear and concise output to the user, indicating the success or failure of each operation.
Expected Behavior:
The application should continuously accept commands from the user until a "quit" command is entered. Each command should be processed, and the appropriate output should be displayed.
Edge Cases to Consider:
- Empty parking lot.
- Parking lot with zero spaces.
- Attempting to park a vehicle when the lot is full.
- Attempting to unpark a vehicle that is not in the lot.
- Duplicate license plates (should be handled appropriately - either prevent or handle).
- Invalid command input.
Examples
Example 1:
Input:
create 3
park "ABC-123"
park "DEF-456"
park "GHI-789"
park "JKL-012"
get_available_spaces
get_vehicle_details
unpark "ABC-123"
get_available_spaces
get_vehicle_details
quit
Output:
Parking lot created with 3 spaces.
Vehicle ABC-123 parked successfully.
Parking lot is full.
Available spaces: 3
Vehicles parked: ["ABC-123", "DEF-456", "GHI-789"]
Vehicle ABC-123 unparked successfully.
Available spaces: 2
Vehicles parked: ["DEF-456", "GHI-789"]
Explanation: The parking lot is created with 3 spaces. Three vehicles are parked successfully. The fourth parking attempt fails because the lot is full. Then, "ABC-123" is unparked, and the available spaces are updated.
Example 2:
Input:
create 2
park "XYZ-789"
park "UVW-012"
unpark "XYZ-789"
unpark "XYZ-789"
get_available_spaces
quit
Output:
Parking lot created with 2 spaces.
Vehicle XYZ-789 parked successfully.
Vehicle UVW-012 parked successfully.
Vehicle XYZ-789 unparked successfully.
Vehicle not found.
Available spaces: 2
Explanation: The parking lot is created. Two vehicles are parked. One is unparked. Attempting to unpark the same vehicle again results in a "Vehicle not found" error.
Example 3: (Edge Case)
Input:
create 0
park "ABC-123"
get_available_spaces
quit
Output:
Parking lot created with 0 spaces.
Parking lot is full.
Available spaces: 0
Explanation: The parking lot is created with zero spaces. Attempting to park a vehicle immediately results in the parking lot being full.
Constraints
- The number of parking spaces will be between 0 and 1000 (inclusive).
- License plates will be strings of alphanumeric characters (e.g., "ABC-123", "XYZ789").
- The application should be able to handle at least 1000 parking operations without significant performance degradation.
- Input will be provided via standard input (stdin).
- Output should be printed to standard output (stdout).
Notes
- Consider using Rust's
Stringtype for license plates and other text-based data. - You can use Rust's
matchstatement or other control flow structures to handle different commands. - Think about how to represent the state of the parking lot in a clear and efficient way.
- Focus on writing clean, readable, and well-documented code.
- Error handling is crucial; provide informative error messages to the user.
- You can use a simple command-line parsing library if you wish, but it's not required. Focus on the core parking lot logic.
- The "quit" command should terminate the program gracefully.