Menu

A parking system in C#: singleton, generics, and the ID rollback problem
📰

A parking system in C#: singleton, generics, and the ID rollback problem

DEV Community: csharp·Ahmed Gamal·7 days ago
#QzWFxFFq
#devtocsharp#devto#dev#vehicle#length
Long article (1001 words) — no AI summary yet.
Reading 0:00
15s threshold

A parking lot is a good exercise for object-oriented design, because the rules are simple to state and awkward to implement: a vehicle takes a number of slots based on its length, the slots have to be next to each other, and the system has to keep track of who is parked where. I built a console application in C# to work through that, and it taught me more than the usual inheritance exercises.

The repository is here: https://github.com/AGA7474/Vehicle-Parking-Management-System

The model

Everything starts from one abstract class:

internal abstract class Vehicle : IPrintable { private static int StaticID ;

public int Id { get ; private set ; } public int Price { get ; private set ; } public int Length { get ; private set ; }

public Vehicle ( int price , int length ) { Id = ++ StaticID ; Price = price ; Length = length ; }

public virtual double CalcMoney ( double NumOfHours ) { return NumOfHours * Price ; }

public abstract void PrintReceipt ( double hours , double fees ) ; }

Enter fullscreen mode

Exit…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.