Originally posted on https://samueleresca.net
The following post describes a parking lot using Typescript and OOP pattern, the scope of the demo is shows some Typescript language features.
Design an OO parking lot using typescript language features
We're going to implement a generic parking lot ticketing system, using the following classes and interfaces:
The Ticket class is used by Vehicle class: it describes the associations between the parked Vehicle and the entering ticket. It defines an id, entry and exit Date. The id is calculated using the combination between current time and license plate of car.
Source code https://gist.github.com/589b0f440e1a84d0ba66
The Vehicle class contains some attributes about Vehicle dimensions, and brand and the license plate. It also contains the methods to park/exit the Vehicles from Parking lot. The Car extends Vehicle class, and it adds additional information: car insurance. The Vehicle class can be eventually extended to add other vehicle types.
Source code https://gist.github.com/3f48c0317ee86589e6d8
The Parking lot is described using a combination between an interface and a class: the interface contains functions signatures, and the concrete class contains an array that is used as a Vehicles container. ParkingLot uses *custom find method * that can find objects inside arrays.