Swift code for organizing car and motorcycle races

Project name: Rally

An organizer of motorcycle and car rallies asks your help for organizing his races.
Description:

Write your code according to the description that follows:

  • You are asked to create a Swift project for a rally organizer.
  • You should create a Swift protocol that each requested class must adopt
    and implement that protocol and return a printable version of their
    properties.
  • You may create abstract classes wherever is required.
  • In order to test the output, you should follow the execution example.
  1. Vehicle class
    You should firstly implement a class Vehicle that allows to represent a vehicle that
    participates in the races.

A vehicle is characterized by:
• Its name of type String like “Ferrari” for example
• Its maximal speed of type Double
• Its weight in kg of type integer
• And the level of fuel in the tank of type integer

The class vehicle will include:
• An initializer initializing the attributes using values passed as parameters and
a default initializer initializing the name with “Anonym”, the level of fuel with
zero, the maximal speed with 130 and the weight with 1000.

• The class Vehicle must be able to represent itself by producing a String
containing all the characteristics of the Vehicle except for the level of fuel,
representing the following format:
-> speed max = km/h, weight = kg
Where is the name of the Vehicle, is its maximal speed
and , its weight.

• A method name better returning true if the current instance has a better
performance than the other vehicle

• The getters: gets the name, gets the maximal speed, gets the weight, gets the
level of fuel
Finally, this class will include and use a tool method performance. This method must
return an estimation of the performance of the vehicle like the ratio between the
maximal speed and its weight (the lighter and the faster the vehicle is, the better is
its performance because it consumes less energy)
You are asked to implement the class Vehicle respecting a good encapsulation.

  1. Cars and motorcycles
    The vehicles participating in the rally can be either cars or motorcycles. A car
    (class Car) is characterized by additional information indicating its category (“race
    car” or “touring car”).
    A motorcycle (class Moto) is characterized by a Boolean
    indicating if it has sidecar. By default, a Moto does not have a sidecar. The class Car
    will also contain a property category and a getter for that property.

• The representation of a car in the format of String will respect the following
format:
-> speed max = km/h, weight = kg, car category =
Where is the name of the vehicle, is the maximal speed,
its weight and its category.
• The representation of a motorcycle in the format of String will respect the
following format:

-> speed max = km/h, weight = kg, Moto, with sidecar
If it has a sidecar or:
-> speed max = km/h, weight = kg, Moto
If not.
Where is the name of the vehicle, is the maximal speed,
its weight
3) The classes GrandPrix and Rally
you are now asked to code a class GrandPrix as a “heterogenous collection” of
vehicles.
This collection represents the set of vehicles participating in a race.
This class inherits from a class Rally which contains uniquely one method “check”
[check () -> Bool)].
This method must allow to verify if the vehicles have the right to
race together. The method “check” cannot be concretely defined in the class Rally.
The class GrandPrix will have:

• A method “add” allowing to add a vehicle in the set of participants (the
insertion will be done in the end of the collection)

• For a rally of the type GrandPrix the cars don’t have the right to race with
two-wheeled; the motorcycles having a “sidecar” are not considered as
vehicles with two wheels; the two-wheeled have the right to race together.
In order to test the compatibility of the vehicles, you will add to the hierarchy of
Vehicle a method: “isTwoWheeled” [isTwoWheeled() -> Bool] returning true when
the vehicle is of type two-wheeled and false in the contrary case. You will consider
that a basic vehicle is not a two-wheeled.

  1. The race is launched
    Complete the class GrandPrix by adding to it a method “run” [run(turn: Int)]
    simulating the progress of the race according to the following algorithm:

• Start by testing if the vehicles have the right to race together; if not, the
message “Not Grand Prix” will be displayed and the method run should
terminate its execution.

• When the race takes place, for every vehicle deduce as much fuel as the
“turns”; only the vehicles that still have fuel (>0) arrive to finishing line.
• Among the vehicles that reaches the finishing line, select the most efficient
one (the one which is better than all others) and display it respecting the
following format:

The winner of the Grand Prix is:

Where is the representation of the winning vehicle in the format of
String; if no vehicle reaches the starting line, display the message:
All the vehicles failed to finish the rally
Execution example:
Test part 1 :

Anonym -> speed max = 130.0 km/h, weight = 1000 kg
Ferrari -> speed max = 300.0 km/h, weight = 800 kg
Renault Clio -> speed max = 180.0 km/h, weight = 1000 kg
The first car is better than the second
Test part 2 :

Honda -> speed max = 200.0 km/h, weight = 250 kg, with sidecar
Kawasaki -> speed max = 280.0 km/h, weight = 180 kg
Lamborghini -> speed max = 320.0 km/h, weight = 1200 kg, car category = race
car
BMW -> speed max = 190.0 km/h, weight = 2000 kg, car category = touring car
Test part 3 :

true
false
true
false
Test part 4 :

First round:
The winner of the Grand Prix is: Lamborghini -> speed max = 320.0 km/h, weight
= 1200 kg, car category = race car
Second round:
All the vehicles failed to finish the rally
Third round:
Not Grand Prix
Program ended with exit code: 0

Hey I would surely like to help you.But I dont know swift.Can u sugesst any swift tutorials .By the time I will create the program in C or C++

thanks for helping…you can refer this https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html

Is it part of any contest?? Thanks for resource. Where can I try my code??

its my college assignment…try here https://www.tutorialspoint.com/compile_swift_online.php

Hi Aditya,

have you completed the program?