Advertisement
Grimmjow1

Catalog

Jul 12th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Catalog
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<Vehicle> cars = new List<Vehicle>();
  12.            
  13.             while (true)
  14.             {
  15.                 string input = Console.ReadLine();
  16.                 if (input=="End")
  17.                 {
  18.                     break;
  19.                 }
  20.                 //{typeOfVehicle} {model} {color} {horsepower}
  21.                 string[] data = input.Split();
  22.                 Vehicle vehicle = new Vehicle(data[0], data[1], data[2], int.Parse(data[3]));
  23.                 cars.Add(vehicle);
  24.             }
  25.             while (true)
  26.             {
  27.                 string input = Console.ReadLine();
  28.                 if (input== "Close the Catalogue")
  29.                 {
  30.                     break;
  31.                 }
  32.                 int index = cars.FindIndex(x => x.Model == input);
  33.                 Console.WriteLine(cars[index].ToString());
  34.             }
  35.             var onlyCars = cars.Where(x => x.Type == "car").ToList();
  36.             var onlyTrucks = cars.Where(x => x.Type == "truck").ToList();
  37.  
  38.             if (onlyCars.Count!=0)
  39.             {
  40.                 Console.WriteLine("Cars have average horsepower of: {0:F2}.",onlyCars.Select(x=>x.Horsepower).Sum()/(onlyCars.Count*1.00));
  41.             }
  42.             else
  43.             {
  44.                 Console.WriteLine($"Cars have average horsepower of: {0:f2}.");
  45.             }
  46.             if (onlyTrucks.Count!=0)
  47.             {
  48.                 Console.WriteLine("Trucks have average horsepower of: {0:f2}.", onlyTrucks.Select(x => x.Horsepower).Sum() / (onlyTrucks.Count * 1.00));
  49.             }
  50.             else
  51.             {
  52.                 Console.WriteLine($"Trucks have average horsepower of: {0:f2}.");
  53.             }
  54.  
  55.  
  56.         }
  57.     }
  58.     class Vehicle
  59.     {
  60.         public Vehicle(string type,string model,string color,int horsepower)
  61.         {
  62.             this.Type = type;
  63.             this.Model = model;
  64.             this.Color = color;
  65.             this.Horsepower = horsepower;
  66.  
  67.         }
  68.         public string Type { get; set; }
  69.         public string Model { get; set; }
  70.         public string Color { get; set; }
  71.         public int Horsepower { get; set; }
  72.  
  73.         public override string ToString()
  74.         {
  75.             string type = this.Type;
  76.             type =  type.First().ToString().ToUpper() + type.Substring(1);
  77.             return $"Type: {type} \nModel: {this.Model}\nColor: {this.Color}\nHorsepower: {this.Horsepower}";
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement