The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Design a Parking Reservation System with OOD Principles

A Parking Reservation System enables users to reserve parking spots in advance, ensuring a smooth experience for both customers and parking lot owners. The design of such a system using Object-Oriented Design (OOD) principles involves creating objects that interact with each other to simulate the reservation process, track availability, and manage payment. Below is the step-by-step design of a Parking Reservation System using OOD principles.

1. Identify the Core Components (Objects)

The first step in Object-Oriented Design is to identify the core components (or classes) of the system. The key objects in a parking reservation system are:

  • ParkingSpot: Represents a parking spot.

  • Reservation: Represents a booking made by a user for a specific parking spot.

  • User: Represents a customer who makes the reservation.

  • Payment: Handles the payment process for a reservation.

  • ParkingLot: Represents a parking lot containing multiple parking spots.

  • ParkingSystem: Acts as a controller that manages the system, including booking, availability checks, and cancellations.

2. Define the Relationships Between Objects

Now that we have identified the objects, we need to define their relationships. Here are some examples:

  • A ParkingLot has many ParkingSpots.

  • A User can create multiple Reservations.

  • A Reservation is associated with exactly one ParkingSpot and is for one User.

  • A Reservation has a corresponding Payment.

  • The ParkingSystem coordinates interactions between all components.

3. Identify Key Attributes and Methods

Let’s break down each class, specifying its attributes and methods:

1. ParkingSpot

Attributes:

  • spotId: Unique identifier for the parking spot.

  • isAvailable: Boolean indicating whether the parking spot is available or not.

  • type: The type of parking spot (e.g., compact, regular, large, etc.).

Methods:

  • markAsAvailable(): Sets the spot as available.

  • markAsOccupied(): Sets the spot as occupied.

  • getDetails(): Returns the details of the parking spot.

2. Reservation

Attributes:

  • reservationId: Unique identifier for the reservation.

  • user: The user who made the reservation.

  • parkingSpot: The parking spot reserved.

  • startTime: The start time of the reservation.

  • endTime: The end time of the reservation.

Methods:

  • createReservation(): Creates a new reservation.

  • cancelReservation(): Cancels the reservation.

  • getReservationDetails(): Provides details about the reservation.

3. User

Attributes:

  • userId: Unique identifier for the user.

  • name: Name of the user.

  • email: Email address of the user.

  • phone: Contact number.

Methods:

  • makeReservation(): Makes a reservation for a parking spot.

  • viewReservations(): Views all the user’s current and past reservations.

  • updateDetails(): Updates the user’s contact information.

4. Payment

Attributes:

  • paymentId: Unique identifier for the payment.

  • amount: Total cost of the reservation.

  • paymentMethod: The method used to pay (e.g., credit card, PayPal).

  • paymentStatus: Status of the payment (e.g., completed, failed, pending).

Methods:

  • processPayment(): Processes the payment.

  • refund(): Initiates a refund if the reservation is canceled.

5. ParkingLot

Attributes:

  • lotId: Unique identifier for the parking lot.

  • location: The location of the parking lot.

  • totalSpots: Total number of spots in the parking lot.

  • availableSpots: Number of available spots in the lot.

  • spots: A collection (list) of ParkingSpot objects.

Methods:

  • getAvailableSpots(): Returns the number of available spots.

  • addSpot(): Adds a new parking spot.

  • removeSpot(): Removes a parking spot from the lot.

6. ParkingSystem

Attributes:

  • parkingLots: A list of ParkingLot objects in the system.

  • users: A list of User objects.

Methods:

  • searchAvailableSpot(): Finds an available parking spot based on user requirements (location, type).

  • bookSpot(): Books a spot for a user.

  • cancelReservation(): Cancels an existing reservation.

  • processPayment(): Processes the payment once a reservation is made.

4. Apply OOD Principles

1. Encapsulation

Each class hides its internal data, exposing only relevant functionality through methods. For instance:

  • The ParkingSpot class encapsulates the availability of a parking spot and allows modifications through markAsAvailable() and markAsOccupied() methods.

2. Inheritance

We may introduce inheritance if we have different types of users or parking spots. For example:

  • A VIPUser might inherit from User and have special privileges like discounts on reservations.

  • A HandicappedParkingSpot might inherit from ParkingSpot but be marked with additional features, such as wider space.

3. Polymorphism

Methods like getDetails() in ParkingSpot and Reservation can be overridden for specialized classes (e.g., HandicappedParkingSpot) to provide different details.

4. Abstraction

The ParkingSystem abstracts the complex logic of searching for available spots and making reservations. Users interact with the system through a simple interface, while the underlying logic for availability checking and booking remains hidden.

5. Composition

  • A Reservation is composed of a User and a ParkingSpot.

  • A ParkingLot contains many ParkingSpots.

5. Sequence Diagram: Example Interaction

Let’s imagine the sequence of interactions when a user wants to reserve a parking spot:

  1. User logs into the system and requests a parking spot.

  2. ParkingSystem searches for available spots in the ParkingLot.

  3. The system finds an available ParkingSpot and creates a Reservation.

  4. The user makes a Payment for the reservation.

  5. Payment is processed and confirmed.

  6. The ParkingSpot is marked as occupied.

  7. A Reservation object is created and linked to the user.

6. Example Code Sketch

Here’s a simplified example of how the classes might look in Python:

python
class ParkingSpot: def __init__(self, spot_id, spot_type): self.spot_id = spot_id self.is_available = True self.type = spot_type def mark_as_occupied(self): self.is_available = False def mark_as_available(self): self.is_available = True class Reservation: def __init__(self, reservation_id, user, parking_spot): self.reservation_id = reservation_id self.user = user self.parking_spot = parking_spot self.start_time = None self.end_time = None def create_reservation(self, start_time, end_time): self.start_time = start_time self.end_time = end_time self.parking_spot.mark_as_occupied() class User: def __init__(self, user_id, name): self.user_id = user_id self.name = name def make_reservation(self, parking_spot): reservation = Reservation("R123", self, parking_spot) reservation.create_reservation("10:00 AM", "12:00 PM") return reservation class ParkingSystem: def __init__(self): self.parking_lots = [] def search_available_spot(self): # Logic to search for available spots return ParkingSpot("S101", "Regular") def book_spot(self, user): spot = self.search_available_spot() reservation = user.make_reservation(spot) return reservation

7. Conclusion

This Object-Oriented Design for a Parking Reservation System organizes the problem into manageable objects with clear responsibilities, enabling smooth interactions and flexibility for future changes. The system follows core OOD principles like encapsulation, inheritance, polymorphism, and abstraction to provide a maintainable and extensible solution.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About