Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 14.ObjectsAndClasses-Vehicle Catalogue
- You have to create a vehicle catalogue. You will store only two types of vehicles – a car and a truck. Until you receive the “End” command you will be receiving lines of input in the following format:
- {typeOfVehicle} {model} {color} {horsepower}
- After the “End” command, you will start receiving models of vehicles. Print the data for every received vehicle in the following format:
- Type: {typeOfVehicle}
- Model: {modelOfVehicle}
- Color: {colorOfVehicle}
- Horsepower: {horsepowerOfVehicle}
- When you receive the command “Close the Catalogue”, print the average horsepower for the cars and for the trucks in the following format:
- {typeOfVehicles} have average horsepower of {averageHorsepower}.
- The average horsepower is calculated by dividing the sum of the horsepower of all vehicles from the certain type by the total count of vehicles from the same type. Round the answer to the 2nd digit after the decimal separator.
- Constraints
- • The type of vehicle will always be either a car or a truck.
- • You will not receive the same model twice.
- • The received horsepower will be an integer in the range [1…1000]
- • You will receive at most 50 vehicles.
- • The separator will always be a single whitespace.
- Examples
- Input Output
- truck Man red 200 Type: Car
- truck Mercedes blue 300 Model: Ferrari
- car Ford green 120 Color: red
- car Ferrari red 550 Horsepower: 550
- car Lamborghini orange 570 Type: Car
- End Model: Ford
- Ferrari Color: green
- Ford Horsepower: 120
- Man Type: Truck
- Close the Catalogue Model: Man
- Color: red
- Horsepower: 200
- Cars have average horsepower of: 413.33.
- Trucks have average horsepower of: 250.00.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Vehicle_Catalog
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Cars> catalogue = new List<Cars>();
- double counterCars = 0;
- double counterTrucks = 0;
- double sumCars = 0;
- double sumTrucks = 0;
- while (true)
- {
- string[] input = Console.ReadLine().Split();
- if (input[0] == "End")
- {
- break;
- }
- string typeOfvehicle = input[0];
- string model = input[1];
- string color = input[2];
- double horsepower = double.Parse(input[3]);
- if (typeOfvehicle == "car")
- {
- typeOfvehicle = "Car";
- counterCars += 1;
- sumCars += horsepower;
- }
- if (typeOfvehicle == "truck")
- {
- typeOfvehicle = "Truck";
- counterTrucks += 1;
- sumTrucks += horsepower;
- }
- Cars cars = new Cars(typeOfvehicle, model, color, horsepower);
- catalogue.Add(cars);
- }
- //foreach (var car in catalogue)
- //{
- // Console.WriteLine("Type: " + car.TypeOfVehicle);
- // Console.WriteLine("Model: "+ car.Model);
- // Console.WriteLine("Color: " + car.Color);
- // Console.WriteLine("Horsepower: "+car.HorsePower) ;
- //}
- while (true)
- {
- string input = Console.ReadLine();
- if (input == "Close the Catalogue")
- {
- double averageCar = sumCars / counterCars;
- double averageTruck = sumTrucks / counterTrucks;
- if (averageTruck > 0 && averageCar > 0)
- {
- Console.WriteLine($"Cars have average horsepower of: {averageCar:f2}.");
- Console.WriteLine($"Trucks have average horsepower of: {averageTruck:f2}.");
- break;
- }
- else if (counterTrucks == 0)
- {
- Console.WriteLine($"Cars have average horsepower of: {averageCar:f2}.");
- Console.WriteLine($"Trucks have average horsepower of: {0:f2}.");
- break;
- }
- else if (counterCars == 0)
- {
- Console.WriteLine($"Cars have average horsepower of: {0:f2}.");
- Console.WriteLine($"Trucks have average horsepower of: {averageTruck:f2}.");
- break;
- }
- }
- foreach (var car in catalogue)
- {
- if (car.Model == input)
- {
- Console.WriteLine("Type: " + car.TypeOfVehicle);
- Console.WriteLine("Model: " + car.Model);
- Console.WriteLine("Color: " + car.Color);
- Console.WriteLine("Horsepower: " + car.HorsePower);
- }
- }
- }
- }
- }
- class Cars
- {
- public Cars(string typeOfvehicle, string model, string color, double horsepower)
- {
- TypeOfVehicle = typeOfvehicle;
- Model = model;
- Color = color;
- HorsePower = horsepower;
- }
- public string TypeOfVehicle { get; set; }
- public string Model { get; set; }
- public string Color { get; set; }
- public double HorsePower { get; set; }
- public override string ToString()
- {
- return $"Type: {TypeOfVehicle} Model: {Model} Color: {Color} Horsepower: {HorsePower}";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment