How to Design a Parking Lot using Object-Oriented Principles?
Last Updated :
12 Apr, 2025
Designing a parking lot using object-oriented principles involves breaking down the system into classes, attributes, and methods that reflect real-world entities. Key components like vehicles and parking spaces can be modeled as objects, while interactions such as parking can be handled through methods. This approach promotes modularity, reusability, and maintainability, making the system easy to extend and manage.
How to design a parking lot using object-oriented principles?Assumptions
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.
- The parking lot has multiple levels. Each level has multiple rows of spots.
- The parking lot can park motorcycles, cars, and buses.
- The parking lot has motorcycle spots, compact spots, and large spots.
- A motorcycle can park in any spot.
- A car can park in either a single compact spot or a single large spot.
- 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.
Object-Oriented Design
We begin by creating the necessary classes and ensuring each class has a clear, single responsibility. Let's break down the design with a focus on how each class and method interacts.
1. Vehicle Class
The Vehicle
class defines common attributes and behaviors for all types of vehicles. It will serve as a base class for more specific vehicle types like Bus
, Car
, and Motorcycle
.
Java
public abstract class Vehicle {
protected String licensePlate;
protected int spotsNeeded;
protected VehicleSize size;
public Vehicle(String licensePlate, VehicleSize size) {
this.licensePlate = licensePlate;
this.size = size;
this.spotsNeeded = (size == VehicleSize.Large) ? 5 : 1;
}
public int getSpotsNeeded() {
return spotsNeeded;
}
public VehicleSize getSize() {
return size;
}
public String getLicensePlate() {
return licensePlate;
}
public abstract boolean canFitInSpot(ParkingSpot spot);
}
2. Concrete Vehicle Classes
Bus: A bus requires 5 consecutive large spots.
Java
public class Bus extends Vehicle {
public Bus(String licensePlate) {
super(licensePlate, VehicleSize.Large);
}
public boolean canFitInSpot(ParkingSpot spot) {
return spot.getSpotSize() == VehicleSize.Large;
}
}
Car: A car can park in either compact or large spots.
Java
public class Car extends Vehicle {
public Car(String licensePlate) {
super(licensePlate, VehicleSize.Compact);
}
public boolean canFitInSpot(ParkingSpot spot) {
return spot.getSpotSize() == VehicleSize.Compact || spot.getSpotSize() == VehicleSize.Large;
}
}
Motorcycle: A motorcycle can park in any spot
Java
public class Motorcycle extends Vehicle {
public Motorcycle(String licensePlate) {
super(licensePlate, VehicleSize.Motorcycle);
}
public boolean canFitInSpot(ParkingSpot spot) {
return true; // Can park in any spot
}
}
3. ParkingSpot Class
The ParkingSpot
class represents an individual parking spot in the parking lot. It is responsible for managing its availability and verifying whether a specific vehicle can fit in 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.
Java
public class ParkingSpot {
private Vehicle vehicle;
private VehicleSize spotSize;
private int row;
private int spotNumber;
private Level level;
public ParkingSpot(Level level, int row, int spotNumber, VehicleSize spotSize) {
this.level = level;
this.row = row;
this.spotNumber = spotNumber;
this.spotSize = spotSize;
this.vehicle = null;
}
public boolean isAvailable() {
return vehicle == null;
}
public boolean canFitVehicle(Vehicle vehicle) {
return isAvailable() && vehicle.canFitInSpot(this);
}
public void parkVehicle(Vehicle vehicle) {
if (canFitVehicle(vehicle)) {
this.vehicle = vehicle;
}
}
public void removeVehicle() {
this.vehicle = null;
}
public VehicleSize getSpotSize() {
return spotSize;
}
public int getRow() {
return row;
}
public int getSpotNumber() {
return spotNumber;
}
}
4. ParkingLevel Class
The Level
class represents a level in the parking lot. It manages a collection of parking spots and provides methods to park and remove vehicles.
Java
public class Level {
private int levelNumber;
private ParkingSpot[] spots;
public Level(int levelNumber, int numSpots) {
this.levelNumber = levelNumber;
this.spots = new ParkingSpot[numSpots];
}
public boolean parkVehicle(Vehicle vehicle) {
for (ParkingSpot spot : spots) {
if (spot.canFitVehicle(vehicle)) {
spot.parkVehicle(vehicle);
return true;
}
}
return false;
}
public boolean removeVehicle(Vehicle vehicle) {
for (ParkingSpot spot : spots) {
if (spot.isOccupied() && spot.getVehicle().equals(vehicle)) {
spot.removeVehicle();
return true;
}
}
return false;
}
}
5. ParkingLot Class
The ParkingLot
class represents the entire parking lot. It manages multiple levels and provides methods to park and remove vehicles from the parking lot.
Java
public class ParkingLot {
private Level[] levels;
public ParkingLot(int numLevels, int numSpotsPerLevel) {
levels = new Level[numLevels];
for (int i = 0; i < numLevels; i++) {
levels[i] = new Level(i, numSpotsPerLevel);
}
}
public boolean parkVehicle(Vehicle vehicle) {
for (Level level : levels) {
if (level.parkVehicle(vehicle)) {
return true;
}
}
return false; // Parking failed (no spots available)
}
public boolean removeVehicle(Vehicle vehicle) {
for (Level level : levels) {
if (level.removeVehicle(vehicle)) {
return true;
}
}
return false; // Removal failed (vehicle not found)
}
}
6. Ticket and PaymentService Classes
To manage ticketing and payments, we add the Ticket
and PaymentService
classes.
Ticket Class: Represents the ticket issued when a vehicle parks. It records the time the vehicle enters and exits the parking lot.
Java
public class Ticket {
private Vehicle vehicle;
private Date issueTime;
private Date exitTime;
public Ticket(Vehicle vehicle) {
this.vehicle = vehicle;
this.issueTime = new Date();
}
public void setExitTime(Date exitTime) {
this.exitTime = exitTime;
}
public long getDuration() {
return (exitTime.getTime() - issueTime.getTime()) / 1000; // Time in seconds
}
}
PaymentService Class: Responsible for calculating the parking fee and processing payments.
Java
public class PaymentService {
public double calculateFee(Ticket ticket) {
long duration = ticket.getDuration();
// Simple fee model: $1 per hour
return duration / 3600.0;
}
public void processPayment(Ticket ticket) {
double fee = calculateFee(ticket);
System.out.println("Payment processed for $" + fee);
}
}
Key Design Principles in Action
1. Single Responsibility Principle (SRP): Each class has a single responsibility. The Vehicle
class focuses only on vehicle details, while the ParkingSpot
, Level
, and ParkingLot
classes handle their respective responsibilities.
2. Encapsulation: All details related to parking spots, levels, and payment processing are hidden within their respective classes.
3. Polymorphism: The canFitInSpot()
method is overridden in each subclass of Vehicle
, allowing different behaviors depending on the vehicle type.
4. Separation of Concerns: The system is broken down into smaller components, with the Ticket
, PaymentService
, and ParkingLot
classes each responsible for specific parts of the process
Conclusion
By following object-oriented principles, we have designed a modular, extensible, and maintainable parking lot system. The use of classes like Vehicle
, ParkingSpot
, Level
, ParkingLot
, Ticket
, and PaymentService
ensures that each part of the system is focused on a single responsibility. The design can easily be extended to add features such as handling different types of parking fees, adding more vehicle types, or introducing additional functionalities like electric vehicle charging stations.
Similar Reads
Design Patterns in Object-Oriented Programming (OOP) Software Development is like putting together a puzzle. Object-oriented programming (OOP) is a popular way to build complex software, but it can be tricky when you face the same design problems repeatedly. That's where design patterns come in.Design patterns are like well-known recipes for common pr
15+ min read
Object Oriented Principles in OOAD Object-oriented principles are a set of guidelines for designing and implementing software systems that are based on the idea of objects. Objects are self-contained units of code that have both data and behavior. They can interact with each other to perform tasks. Object-Oriented Analysis and Design
7 min read
Object Oriented System | Object Oriented Analysis & Design Object Oriented System is a type of development model where Objects are used to specify different aspects of an Application. Everything including Data, information, processes, functions, and so on is considered an object in Object-Oriented System. Important Topics for the Object Oriented System Obje
4 min read
Object Oriented Paradigm in Object Oriented Analysis & Design(OOAD) There are two important steps in creating such software. Object-Oriented Analysis (OOA) and Object-Oriented Design (OOD). These steps are like the building blocks for creating software. Important topics for Object-Oriented Paradigm Object Oriented AnalysisObject-Oriented DesignHistorical ContextObje
6 min read
Object-Oriented Programing(OOP) Concepts for Designing Sytems Modeling real-world entities is a powerful way to build systems with object-oriented programming, or OOP. In order to write modular, reusable, and scalable code, it focuses on fundamental ideas like classes, objects, inheritance, and polymorphism. Building effective and maintainable systems is made
15+ min read