JulianJulianov

14.ObjectsAndClasses-Vehicle Catalogue

Mar 5th, 2020
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.18 KB | None | 0 0
  1. 14.ObjectsAndClasses-Vehicle Catalogue
  2. 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:
  3. {typeOfVehicle} {model} {color} {horsepower}
  4. After the “End” command, you will start receiving models of vehicles. Print the data for every received vehicle in the following format:
  5. Type: {typeOfVehicle}
  6. Model: {modelOfVehicle}
  7. Color: {colorOfVehicle}
  8. Horsepower: {horsepowerOfVehicle}
  9.  
  10. When you receive the command “Close the Catalogue”, print the average horsepower for the cars and for the trucks in the following format:
  11. {typeOfVehicles} have average horsepower of {averageHorsepower}.
  12. 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.
  13. Constraints
  14. • The type of vehicle will always be either a car or a truck.
  15. • You will not receive the same model twice.
  16. • The received horsepower will be an integer in the range [11000]
  17. • You will receive at most 50 vehicles.
  18. • The separator will always be a single whitespace.
  19. Examples
  20. Input                                      Output
  21. truck Man red 200                          Type: Car
  22. truck Mercedes blue 300                    Model: Ferrari
  23. car Ford green 120                         Color: red
  24. car Ferrari red 550                        Horsepower: 550
  25. car Lamborghini orange 570                 Type: Car
  26. End                                        Model: Ford
  27. Ferrari                                    Color: green
  28. Ford                                       Horsepower: 120
  29. Man                                        Type: Truck
  30. Close the Catalogue                        Model: Man
  31.                                            Color: red
  32.                                            Horsepower: 200
  33.                                            Cars have average horsepower of: 413.33.
  34.                                            Trucks have average horsepower of: 250.00.
  35.  
  36.  
  37. using System;
  38. using System.Collections.Generic;
  39. using System.Linq;
  40.  
  41. namespace Vehicle_Catalog
  42. {
  43.     class Program
  44.     {
  45.         static void Main(string[] args)
  46.         {
  47.             List<Cars> catalogue = new List<Cars>();
  48.             double counterCars = 0;
  49.             double counterTrucks = 0;
  50.             double sumCars = 0;
  51.             double sumTrucks = 0;
  52.  
  53.             while (true)
  54.             {
  55.                 string[] input = Console.ReadLine().Split();
  56.                 if (input[0] == "End")
  57.                 {
  58.                     break;
  59.                 }
  60.                 string typeOfvehicle = input[0];
  61.                 string model = input[1];
  62.                 string color = input[2];
  63.                 double horsepower = double.Parse(input[3]);
  64.                 if (typeOfvehicle == "car")
  65.                 {
  66.                     typeOfvehicle = "Car";
  67.                     counterCars += 1;
  68.                     sumCars += horsepower;
  69.  
  70.                 }
  71.                 if (typeOfvehicle == "truck")
  72.                 {
  73.                     typeOfvehicle = "Truck";
  74.                     counterTrucks += 1;
  75.                     sumTrucks += horsepower;
  76.  
  77.                 }
  78.  
  79.  
  80.                 Cars cars = new Cars(typeOfvehicle, model, color, horsepower);
  81.                 catalogue.Add(cars);
  82.             }
  83.             //foreach (var car in catalogue)
  84.             //{
  85.  
  86.             //    Console.WriteLine("Type: " + car.TypeOfVehicle);
  87.             //    Console.WriteLine("Model: "+ car.Model);
  88.             //    Console.WriteLine("Color: " + car.Color);
  89.             //    Console.WriteLine("Horsepower: "+car.HorsePower)    ;
  90.  
  91.             //}
  92.             while (true)
  93.             {
  94.                 string input = Console.ReadLine();
  95.                 if (input == "Close the Catalogue")
  96.                 {
  97.                     double averageCar = sumCars / counterCars;
  98.                     double averageTruck = sumTrucks / counterTrucks;
  99.                     if (averageTruck > 0 && averageCar > 0)
  100.                     {
  101.                         Console.WriteLine($"Cars have average horsepower of: {averageCar:f2}.");
  102.                         Console.WriteLine($"Trucks have average horsepower of: {averageTruck:f2}.");
  103.                         break;
  104.                     }
  105.                     else if (counterTrucks == 0)
  106.                     {
  107.                         Console.WriteLine($"Cars have average horsepower of: {averageCar:f2}.");
  108.                         Console.WriteLine($"Trucks have average horsepower of: {0:f2}.");
  109.                         break;
  110.                     }
  111.                     else if (counterCars == 0)
  112.                     {
  113.                         Console.WriteLine($"Cars have average horsepower of: {0:f2}.");
  114.                         Console.WriteLine($"Trucks have average horsepower of: {averageTruck:f2}.");
  115.                         break;
  116.                     }
  117.  
  118.                 }
  119.  
  120.                 foreach (var car in catalogue)
  121.                 {
  122.                     if (car.Model == input)
  123.                     {
  124.                         Console.WriteLine("Type: " + car.TypeOfVehicle);
  125.                         Console.WriteLine("Model: " + car.Model);
  126.                         Console.WriteLine("Color: " + car.Color);
  127.                         Console.WriteLine("Horsepower: " + car.HorsePower);
  128.                     }
  129.  
  130.                 }
  131.  
  132.             }
  133.         }
  134.  
  135.  
  136.     }
  137.     class Cars
  138.     {
  139.         public Cars(string typeOfvehicle, string model, string color, double horsepower)
  140.         {
  141.             TypeOfVehicle = typeOfvehicle;
  142.             Model = model;
  143.             Color = color;
  144.             HorsePower = horsepower;
  145.  
  146.         }
  147.         public string TypeOfVehicle { get; set; }
  148.         public string Model { get; set; }
  149.         public string Color { get; set; }
  150.         public double HorsePower { get; set; }
  151.         public override string ToString()
  152.         {
  153.             return $"Type: {TypeOfVehicle} Model: {Model} Color: {Color} Horsepower: {HorsePower}";
  154.  
  155.         }
  156.  
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment