Advertisement
Spocoman

06. Vehicle Catalogue

Apr 4th, 2023
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Channels;
  6.  
  7. namespace VehicleCatalogue
  8. {
  9.     class Vehicle
  10.     {
  11.         public Vehicle(string type, string model, string color, int horsepower)
  12.         {
  13.             this.Type = type;
  14.             this.Model = model;
  15.             this.Color = color;
  16.             this.Horsepower = horsepower;
  17.         }
  18.  
  19.         public string Type { get; set; }
  20.         public string Model { get; set; }
  21.         public string Color { get; set; }
  22.  
  23.         public int Horsepower { get; set; }
  24.  
  25.     }
  26.  
  27.     class Program
  28.     {
  29.         public static void Main()
  30.         {
  31.             List<Vehicle> vehicles = new List<Vehicle>();
  32.  
  33.             string command;
  34.  
  35.             while ((command = Console.ReadLine()) != "End")
  36.             {
  37.                 string[] info = command.Split(" ");
  38.  
  39.                 string type = info[0];
  40.                 string model = info[1];
  41.                 string color = info[2];
  42.                 string horsepower = info[3];
  43.  
  44.                 vehicles.Add(new Vehicle(type, model, color, int.Parse(horsepower)));
  45.             }
  46.  
  47.             while ((command = Console.ReadLine()) != "Close the Catalogue")
  48.             {
  49.                 int index = IndexFinder(vehicles, command);
  50.                 string type = vehicles[index].Type;
  51.                 Console.WriteLine($"Type: {type[0].ToString().ToUpper() + type.Substring(1)}\nModel: {vehicles[index].Model}\nColor: {vehicles[index].Color}\nHorsepower: {vehicles[index].Horsepower}");
  52.             }
  53.  
  54.             var cars = vehicles.Where(x => x.Type == "car");
  55.             var trucks = vehicles.Where(x => x.Type == "truck");
  56.  
  57.             double carsHorsepower = cars.Count() > 0 ? (double)(cars.Select(x => x.Horsepower).ToList().Sum()) / cars.Count() : 0;
  58.             double trucksHorsepower = trucks.Count() > 0 ? (double)(trucks.Select(x => x.Horsepower).ToList().Sum()) / trucks.Count() : 0;
  59.  
  60.             Console.WriteLine($"Cars have average horsepower of: {carsHorsepower:f2}.");
  61.             Console.WriteLine($"Trucks have average horsepower of: {trucksHorsepower:f2}.");
  62.         }
  63.  
  64.         static int IndexFinder(List<Vehicle> v, string m)
  65.         {
  66.             return v.FindIndex(x => x.Model == m);
  67.         }
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement