Inheritance and polymorphism: a vehicle management system
Swift
Hard
classes
Task «Inheritance and polymorphism: a vehicle management system»
Develop a class hierarchy for managing a fleet of vehicles. The base class Vehicle contains properties brand (String) and year (Int), as well as a method description() that returns a string in the format "Vehicle: brand year". The Car class inherits from Vehicle and adds a property fuelType (String, which can be "petrol", "diesel", or "electric") and overrides description() to return "Car: brand year, fuel: fuelType". The Bicycle class inherits from Vehicle and adds a property type (String, which can be "mountain", "road", or "city") and overrides description() to return "Bicycle: brand year, type: type". The Fleet class manages an array of Vehicle and has a method addVehicle(_: Vehicle) to add a vehicle, a method listDescriptions() to print (via print) descriptions of all vehicles in the order they were added (each on a new line), and a method countByYear(year: Int) -> Int that returns the number of vehicles of the specified year. Input data: first, an integer N (1 ≤ N ≤ 100) — the number of vehicles. Then, for each vehicle, a type string ("Car" or "Bicycle") and the corresponding properties are entered: brand (a string without spaces), year (an integer from 1900 to 2023), and for Car — fuelType (string), for Bicycle — type (string). After entering all vehicles, output a year (integer) separated by a space for counting. It is guaranteed that all data is correct. First, output the results of listDescriptions(), then on a new line, the number of vehicles of the specified year.
Input Format
Integer N. Then N blocks: a string type, a string brand, an integer year, for Car a string fuelType, for Bicycle a string type. Then an integer yearForCount.
Output Format
First, N lines of descriptions (each description on a separate line), then on a new line an integer — the number of vehicles with a year equal to yearForCount.
Examples
Example 1
INPUT
10
OUTPUT
30
Example 2
INPUT
6
4 4 1 1 2 3
OUTPUT
1 1 4 4 2 3
main.swift