Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • Software and Tools
    • School Learning
    • Practice Coding Problems
  • Go Premium
  • System Design Tutorial
  • What is System Design
  • System Design Life Cycle
  • High Level Design HLD
  • Low Level Design LLD
  • Design Patterns
  • UML Diagrams
  • System Design Interview Guide
  • Scalability
  • Databases
Open In App
Next Article:
Object Oriented Principles in OOAD
Next article icon

How to Design a Parking Lot using Object-Oriented Principles?

Last Updated : 12 Apr, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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
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.


Next Article
Object Oriented Principles in OOAD

S

Somesh Awasthi
Improve
Article Tags :
  • Misc
  • Design Pattern
  • System Design
Practice Tags :
  • Misc

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
    6 Steps To Approach Object-Oriented Design Questions in Interview
    Object-Oriented Design... is the most important topic to learn in programming.If you're a fresher or a software engineer working in a company, then surely you might be aiming to reach some big companies such as Facebook, Microsoft, Google, or Amazon. These companies conduct the design round to check
    6 min read
`; $(commentSectionTemplate).insertBefore(".article--recommended"); } loadComments(); }); }); function loadComments() { if ($("iframe[id*='discuss-iframe']").length top_of_element && top_of_screen articleRecommendedTop && top_of_screen articleRecommendedBottom)) { if (!isfollowingApiCall) { isfollowingApiCall = true; setTimeout(function(){ if (loginData && loginData.isLoggedIn) { if (loginData.userName !== $('#followAuthor').val()) { is_following(); } else { $('.profileCard-profile-picture').css('background-color', '#E7E7E7'); } } else { $('.follow-btn').removeClass('hideIt'); } }, 3000); } } }); } $(".accordion-header").click(function() { var arrowIcon = $(this).find('.bottom-arrow-icon'); arrowIcon.toggleClass('rotate180'); }); }); window.isReportArticle = false; function report_article(){ if (!loginData || !loginData.isLoggedIn) { const loginModalButton = $('.login-modal-btn') if (loginModalButton.length) { loginModalButton.click(); } return; } if(!window.isReportArticle){ //to add loader $('.report-loader').addClass('spinner'); jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', { PRACTICE_API_URL: practiceAPIURL, PRACTICE_URL:practiceURL },function(responseTxt, statusTxt, xhr){ if(statusTxt == "error"){ alert("Error: " + xhr.status + ": " + xhr.statusText); } }); }else{ window.scrollTo({ top: 0, behavior: 'smooth' }); $("#report_modal_content").show(); } } function closeShareModal() { const shareOption = document.querySelector('[data-gfg-action="share-article"]'); shareOption.classList.remove("hover_share_menu"); let shareModal = document.querySelector(".hover__share-modal-container"); shareModal && shareModal.remove(); } function openShareModal() { closeShareModal(); // Remove existing modal if any let shareModal = document.querySelector(".three_dot_dropdown_share"); shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" })); document.querySelector(".hover__share-modal-container").append( Object.assign(document.createElement('div'), { className: "share__modal" }), ); document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" })); const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"]; socialOptions.forEach((socialOption) => { const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" }); const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` }); const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` }); const shareLink = (socialOption === "Copy Link") ? Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) : Object.assign(document.createElement('a'), { className: "link-container" }); if (socialOption === "LinkedIn") { shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`); shareLink.setAttribute('target', '_blank'); } if (socialOption === "WhatsApp") { shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } if (socialOption === "Twitter") { shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } shareLink.append(icon, socialText); socialContainer.append(shareLink); document.querySelector(".share__modal").appendChild(socialContainer); //adding copy url functionality if(socialOption === "Copy Link") { shareLink.addEventListener("click", function() { var tempInput = document.createElement("input"); tempInput.value = window.location.href; document.body.appendChild(tempInput); tempInput.select(); tempInput.setSelectionRange(0, 99999); // For mobile devices document.execCommand('copy'); document.body.removeChild(tempInput); this.querySelector(".share__option-text").textContent = "Copied" }) } }); // document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu")); } function toggleLikeElementVisibility(selector, show) { document.querySelector(`.${selector}`).style.display = show ? "block" : "none"; } function closeKebabMenu(){ document.getElementById("myDropdown").classList.toggle("show"); }
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences