The Palos Publishing Company

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

Design a Public Bicycle Docking Station System Using Object-Oriented Design

Overview

A Public Bicycle Docking Station System allows users to rent bicycles at designated docking stations and return them when finished. The system provides an easy and efficient way to manage bicycle availability, rentals, and returns. By using Object-Oriented Design (OOD) principles, we can ensure that the system is modular, flexible, and easy to maintain.

Key Components of the System

  • Bicycle: Represents the bicycle, including its ID, status (available, rented, under maintenance), and location.

  • Docking Station: Represents a docking station where bicycles are stored, with the ability to rent out or return bikes.

  • User: Represents a user who rents and returns bicycles.

  • RentalTransaction: Represents the rental or return of a bicycle by a user.

  • Maintenance: Manages bicycle maintenance and the status of bikes.

  • SystemController: Orchestrates the interactions between the various components.


Class Diagram

  1. Bicycle

    • Attributes:

      • id: String

      • status: String (Available, Rented, Under Maintenance)

      • location: String (Docking station ID or None if rented)

    • Methods:

      • changeStatus(status: String): Change the bicycle’s status (Available, Rented, Under Maintenance)

      • updateLocation(location: String): Update the location where the bicycle is located.

  2. DockingStation

    • Attributes:

      • id: String

      • location: String

      • bikes: List<Bicycle> (List of bikes available at the station)

    • Methods:

      • addBike(bike: Bicycle): Adds a bike to the station.

      • removeBike(bikeId: String): Removes a bike from the station.

      • getAvailableBikes(): Returns a list of available bikes at the station.

      • isBikeAvailable(bikeId: String): Returns true if the bike is available for rent.

  3. User

    • Attributes:

      • userId: String

      • rentedBikes: List<Bicycle>

    • Methods:

      • rentBike(dockingStation: DockingStation, bikeId: String): Rent a bike from the station.

      • returnBike(dockingStation: DockingStation, bikeId: String): Return a bike to the station.

  4. RentalTransaction

    • Attributes:

      • transactionId: String

      • userId: String

      • bikeId: String

      • transactionType: String (Rent, Return)

      • time: Date

    • Methods:

      • createTransaction(): Creates a new rental transaction.

      • updateTransaction(): Updates the transaction status (e.g., completed, canceled).

  5. Maintenance

    • Attributes:

      • maintenanceId: String

      • bikeId: String

      • status: String (In Progress, Completed)

      • time: Date

    • Methods:

      • scheduleMaintenance(bikeId: String): Schedule maintenance for a bike.

      • completeMaintenance(bikeId: String): Mark a bike as ready after maintenance.

  6. SystemController

    • Attributes:

      • stations: List<DockingStation>

      • users: List<User>

      • bikes: List<Bicycle>

    • Methods:

      • findStationByLocation(location: String): Finds a docking station by location.

      • findBikeById(bikeId: String): Finds a bike by its ID.

      • processRentalRequest(user: User, dockingStation: DockingStation, bikeId: String): Processes a rental request from a user.

      • processReturnRequest(user: User, dockingStation: DockingStation, bikeId: String): Processes a return request from a user.

      • scheduleBikeMaintenance(bikeId: String): Schedules maintenance for a bike.


Detailed Design

1. Bicycle Class

python
class Bicycle: def __init__(self, id, status, location=None): self.id = id self.status = status self.location = location def change_status(self, status): self.status = status def update_location(self, location): self.location = location

2. DockingStation Class

python
class DockingStation: def __init__(self, id, location): self.id = id self.location = location self.bikes = [] def add_bike(self, bike): self.bikes.append(bike) def remove_bike(self, bike_id): bike = next(b for b in self.bikes if b.id == bike_id) self.bikes.remove(bike) def get_available_bikes(self): return [bike for bike in self.bikes if bike.status == 'Available'] def is_bike_available(self, bike_id): bike = next((b for b in self.bikes if b.id == bike_id), None) return bike and bike.status == 'Available'

3. User Class

python
class User: def __init__(self, user_id): self.user_id = user_id self.rented_bikes = [] def rent_bike(self, docking_station, bike_id): if docking_station.is_bike_available(bike_id): bike = next(b for b in docking_station.bikes if b.id == bike_id) bike.change_status('Rented') bike.update_location(None) self.rented_bikes.append(bike) docking_station.remove_bike(bike_id) return True return False def return_bike(self, docking_station, bike_id): bike = next(b for b in self.rented_bikes if b.id == bike_id) bike.change_status('Available') bike.update_location(docking_station.location) docking_station.add_bike(bike) self.rented_bikes.remove(bike) return True

4. RentalTransaction Class

python
class RentalTransaction: def __init__(self, transaction_id, user_id, bike_id, transaction_type, time): self.transaction_id = transaction_id self.user_id = user_id self.bike_id = bike_id self.transaction_type = transaction_type self.time = time def create_transaction(self): # Logic to create a new transaction in the system. pass def update_transaction(self): # Logic to update transaction status. pass

5. Maintenance Class

python
class Maintenance: def __init__(self, maintenance_id, bike_id, status, time): self.maintenance_id = maintenance_id self.bike_id = bike_id self.status = status self.time = time def schedule_maintenance(self, bike_id): # Logic to schedule maintenance for the bike. pass def complete_maintenance(self, bike_id): # Logic to mark bike as ready for use after maintenance. pass

6. SystemController Class

python
class SystemController: def __init__(self): self.stations = [] self.users = [] self.bikes = [] def find_station_by_location(self, location): return next(station for station in self.stations if station.location == location) def find_bike_by_id(self, bike_id): return next(bike for bike in self.bikes if bike.id == bike_id) def process_rental_request(self, user, docking_station, bike_id): if user.rent_bike(docking_station, bike_id): transaction = RentalTransaction("12345", user.user_id, bike_id, "Rent", "2023-07-17 10:00:00") transaction.create_transaction() return True return False def process_return_request(self, user, docking_station, bike_id): if user.return_bike(docking_station, bike_id): transaction = RentalTransaction("12346", user.user_id, bike_id, "Return", "2023-07-17 11:00:00") transaction.create_transaction() return True return False def schedule_bike_maintenance(self, bike_id): maintenance = Maintenance("67890", bike_id, "In Progress", "2023-07-17") maintenance.schedule_maintenance(bike_id)

Key OOD Principles Applied

  1. Encapsulation: Each class hides its internal data (e.g., Bicycle’s status and location) and only exposes methods that are necessary for interacting with the object.

  2. Abstraction: We have created abstracted methods for handling bike rentals, returns, and maintenance without needing to know the internal details of how the system operates.

  3. Inheritance: We could extend this design in the future, for example, by creating specific types of bicycles (e.g., ElectricBicycle) that inherit from the Bicycle class.

  4. Polymorphism: Methods like process_rental_request and process_return_request can be extended for different types of users or bicycles in future designs.

This OOD approach ensures that the system can handle scalability and future requirements while keeping the code modular and maintainable.

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