Design a parking lot using object-oriented principles.
Asked In : Amazon, Apple, Google and many more interviews
Solution: For our purposes right now, we’ll make the following assumptions. We made these specific assumptions to add a bit of complexity to the problem without adding too much. If you made different assumptions, that’s totally fine.
1) The parking lot has multiple levels. Each level has multiple rows of spots.
2) The parking lot can park motorcycles, cars, and buses.
3) The parking lot has motorcycle spots, compact spots, and large spots.
4) A motorcycle can park in any spot.
5) A car can park in either a single compact spot or a single large spot.
6) A bus can park in five large spots that are consecutive and within the same row. It cannot park in small spots.
In the below implementation, we have created an abstract class Vehicle, from which Car, Bus, and Motorcycle inherit. To handle the different parking spot sizes, we have just one class ParkingSpot which has a member variable indicating the size.
Main Logic in Java given below
The ParkingSpot is implemented by having just a variable which represents the size of the spot. We could have implemented this by having classes for LargeSpot, CompactSpot, and MotorcycleSpot which inherit from ParkingSpot, but this is probably overkilled. The spots probably do not have different behaviors, other than their sizes.
More References :
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Amazon SDE-2 Interview Experience
- Design Video Sharing System Like Youtube
- Design Scalable System like Instagram
- Design a movie ticket booking system like Bookmyshow
- Design an online hotel booking system like OYO Rooms
- Design the Data Structures(classes and objects)for a generic deck of cards
- Chain of Responsibility Design Pattern
- Design data structures and algorithms for in-memory file system
- How to prevent Singleton Pattern from Reflection, Serialization and Cloning?
- Java Singleton Design Pattern Practices with Examples
- Decorator Pattern | Set 1 (Background)
- Singleton Design Pattern | Implementation
- Observer Pattern | Set 1 (Introduction)
- Design Patterns | Set 2 (Factory Method)
- Design Patterns | Set 1 (Introduction)