Advertisement
VasilKotsev

06. Vehicle Catalogue | C# Objects and Classes

Jan 20th, 2019
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. namespace SandBox
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.  
  8.     public class EntryPoint
  9.     {
  10.  
  11.         internal enum VehicleType
  12.         {
  13.             Car,
  14.             Truck
  15.         }
  16.  
  17.         internal class Vehicle
  18.         {
  19.             public Vehicle(VehicleType type, string model, string color, int horsepower)
  20.             {
  21.                 this.Type = type;
  22.                 this.Model = model;
  23.                 this.Color = color;
  24.                 this.Horsepower = horsepower;
  25.             }
  26.  
  27.             public VehicleType Type { get; private set; }
  28.             public string Model { get; private set; }
  29.             public string Color { get; private set; }
  30.             public int Horsepower { get; private set; }
  31.  
  32.             public override string ToString()
  33.             {
  34.                 StringBuilder builder = new StringBuilder();
  35.                 builder.AppendLine($"Type: {this.Type}");
  36.                 builder.AppendLine($"Model: {this.Model}");
  37.                 builder.AppendLine($"Color: {this.Color}");
  38.                 builder.AppendLine($"Horsepower: {this.Horsepower}");
  39.  
  40.                 return builder.ToString().TrimEnd();
  41.             }
  42.         }
  43.  
  44.         public static void Main()
  45.         {
  46.  
  47.             List<Vehicle> vehicles = new List<Vehicle>();
  48.  
  49.             while (true)
  50.             {
  51.                 string[] inputArgs = Console.ReadLine()
  52.                     .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  53.  
  54.                 if (inputArgs[0] == "End")
  55.                     break;
  56.  
  57.                 VehicleType vehicleType;
  58.                 bool isVehicleTypeParseSuccessful = Enum.TryParse(inputArgs[0], true, out vehicleType);
  59.  
  60.                 if (isVehicleTypeParseSuccessful)
  61.                 {
  62.  
  63.                     string vehicleModel = inputArgs[1];
  64.                     string vehicleColor = inputArgs[2];
  65.                     int vehicleHorsepower = int.Parse(inputArgs[3]);
  66.  
  67.                     Vehicle currentVehicle = new Vehicle(vehicleType, vehicleModel, vehicleColor, vehicleHorsepower);
  68.                     vehicles.Add(currentVehicle);
  69.                 }
  70.             }
  71.  
  72.             while (true)
  73.             {
  74.                 string inputArg = Console.ReadLine();
  75.  
  76.                 if (inputArg == "Close the Catalogue")
  77.                     break;
  78.  
  79.                 Vehicle desiredVehicle = vehicles.FirstOrDefault(x => x.Model == inputArg);
  80.                 Console.WriteLine(desiredVehicle);
  81.             }
  82.  
  83.             // these have to be materialized
  84.             // or use .Count() on IEnumerable<Vehicle>
  85.             var cars = vehicles
  86.                 .Where(x => x.Type == VehicleType.Car)
  87.                 .ToList();
  88.  
  89.             var trucks = vehicles
  90.                 .Where(x => x.Type == VehicleType.Truck)
  91.                 .ToList();
  92.  
  93.             double carsAvgHorsepower = cars.Count > 0 ? cars.Average(x => x.Horsepower) : 0.0;
  94.             double trucksAvgHorsepower = trucks.Count > 0 ? trucks.Average(x => x.Horsepower) : 0.0;
  95.  
  96.             Console.WriteLine($"Cars have average horsepower of: {carsAvgHorsepower:f2}.");
  97.             Console.WriteLine($"Trucks have average horsepower of: {trucksAvgHorsepower:f2}.");
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement