StoimenK

C#_Objects_and_Classes_1.6.Vehicle_Catalogue

Feb 25th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _1._6.Vehicle_Catalogue
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             List<Vehicle> listOfVehicles = new List<Vehicle>();
  14.             while (true)
  15.             {
  16.                 string input = Console.ReadLine();
  17.                 if (input == "End")
  18.                 {
  19.                     break;
  20.                 }
  21.                 string[] tokens = input.Split();
  22.                 string type = tokens[0].ToLower();
  23.  
  24.                 if (type == "car")
  25.                 {
  26.                     type = "Car";
  27.                 }
  28.                 else
  29.                 {
  30.                     type = "Truck";
  31.                 }
  32.  
  33.                 string model = tokens[1];
  34.                 string colour = tokens[2];
  35.                 int power = int.Parse(tokens[3]);
  36.                 var vehicle = new Vehicle();
  37.                 vehicle.vehicleType = type;
  38.                 vehicle.vehicleModel = model;
  39.                 vehicle.vehicleColour = colour;
  40.                 vehicle.vehicleHorsepower = power;
  41.                 listOfVehicles.Add(vehicle);
  42.             }
  43.             string command = Console.ReadLine();
  44.             while (true)
  45.             {
  46.                 if (command == "Close the Catalogue")
  47.                 {
  48.                     break;
  49.                 }
  50.                 var vehicle = listOfVehicles.Where(x => x.vehicleModel == command).First();
  51.                 Console.WriteLine($"Type: {vehicle.vehicleType}");
  52.                 Console.WriteLine($"Model: {vehicle.vehicleModel}");
  53.                 Console.WriteLine($"Color: {vehicle.vehicleColour}");
  54.                 Console.WriteLine($"Horsepower: {vehicle.vehicleHorsepower}");
  55.                 command = Console.ReadLine();
  56.             }
  57.  
  58.             if (listOfVehicles.Where(x => x.vehicleType == "Car").Count() > 0)
  59.             {
  60.                 Console.WriteLine($"Cars have average horsepower of: {listOfVehicles.Where(x => x.vehicleType == "Car").Select(x => x.vehicleHorsepower).Average():f2}.");
  61.             }
  62.             else
  63.             {
  64.                 Console.WriteLine($"Cars have average horsepower of: 0.00.");
  65.             }
  66.             if (listOfVehicles.Where(x => x.vehicleType == "Truck").Count() > 0)
  67.             {
  68.                 Console.WriteLine($"Trucks have average horsepower of: {listOfVehicles.Where(x => x.vehicleType == "Truck").Select(x => x.vehicleHorsepower).Average():f2}.");
  69.             }
  70.             else
  71.             {
  72.                 Console.WriteLine($"Trucks have average horsepower of: 0.00.");
  73.             }
  74.         }
  75.     }
  76.     public class Vehicle
  77.     {
  78.         public string vehicleType { get; set; }
  79.         public string vehicleModel { get; set; }
  80.         public string vehicleColour { get; set; }
  81.  
  82.         public int vehicleHorsepower { get; set; }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment