Advertisement
Guest User

Vehicle catalog

a guest
Feb 25th, 2020
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Vehicle_Catalogue
  5. {
  6.     class Program
  7.     {
  8.         class Catalog
  9.         {
  10.             public Catalog()
  11.             {
  12.                 Cars = new List<Car>();
  13.                 Trucks = new List<Truck>();
  14.             }
  15.             public List<Car> Cars { get; set; }
  16.             public List<Truck> Trucks { get; set; }
  17.         }
  18.         class Car
  19.         {
  20.             public string Brand { get; set; }
  21.             public string Model { get; set; }
  22.             public string HorsePower { get; set; }
  23.         }
  24.         class Truck
  25.         {
  26.             public string Brand { get; set; }
  27.             public string Model { get; set; }
  28.             public string weight { get; set; }
  29.         }
  30.         static void Main(string[] args)
  31.         {
  32.             Catalog catalog = new Catalog();
  33.             while (true)
  34.             {
  35.                 string[] command = Console.ReadLine()
  36.                     .Split('/');
  37.                 if (command[0] == "end")
  38.                 {
  39.                     break;
  40.                 }
  41.                 string type = command[0];
  42.                 string brand = command[1];
  43.                 string model = command[2];
  44.                 string horsePowerOrWeight =command[3];
  45.  
  46.                 switch (type)
  47.                 {
  48.                     case "Truck":
  49.                         Truck currentTruck = new Truck();
  50.                         currentTruck.Brand = brand;
  51.                         currentTruck.Model = model;
  52.                         currentTruck.weight = horsePowerOrWeight;
  53.                         catalog.Trucks.Add(currentTruck);
  54.                         break;
  55.                     case "Car":
  56.                         Car currentCar = new Car();
  57.                         currentCar.Brand = brand;
  58.                         currentCar.Model = model;
  59.                         currentCar.HorsePower = horsePowerOrWeight;
  60.                         catalog.Cars.Add(currentCar);
  61.                         break;
  62.                     default:
  63.                         break;
  64.                 }
  65.                
  66.             }
  67.             List<string> carBrands = new List<string>();
  68.             List<Car> sortedCars = new List<Car>();
  69.             for (int i = 0; i < catalog.Cars.Count; i++)
  70.             {
  71.                 carBrands.Add(catalog.Cars[i].Brand);
  72.             }
  73.             carBrands.Sort();
  74.             for (int i = 0; i < carBrands.Count; i++)
  75.             {
  76.                 for (int j = 0; j < catalog.Cars.Count; j++)
  77.                 {
  78.                     if (catalog.Cars[j].Brand == carBrands[i])
  79.                     {
  80.                         sortedCars.Add(catalog.Cars[j]);
  81.                     }
  82.                 }
  83.             }
  84.             List<string> trucksBrands = new List<string>();
  85.             List<Truck> sortedTrucks = new List<Truck>();
  86.             for (int i = 0; i < catalog.Trucks.Count; i++)
  87.             {
  88.                 trucksBrands.Add(catalog.Trucks[i].Brand);
  89.             }
  90.             trucksBrands.Sort();
  91.             for (int i = 0; i < trucksBrands.Count; i++)
  92.             {
  93.                 for (int j = 0; j < catalog.Trucks.Count; j++)
  94.                 {
  95.                     if (catalog.Trucks[j].Brand == trucksBrands[i])
  96.                     {
  97.                         sortedTrucks.Add(catalog.Trucks[j]);
  98.                     }
  99.                 }
  100.             }
  101.             if (sortedCars.Count > 0)
  102.             {
  103.                 Console.WriteLine("Cars:");
  104.                 foreach (Car current in sortedCars)
  105.                 {
  106.                     Console.WriteLine($"{current.Brand}: {current.Model} - {current.HorsePower}hp");
  107.                 }
  108.             }
  109.             if (sortedTrucks.Count > 0)
  110.             {
  111.                 Console.WriteLine("Trucks:");
  112.                 foreach (Truck current in sortedTrucks)
  113.                 {
  114.                     Console.WriteLine($"{current.Brand}: {current.Model} - {current.weight}kg");
  115.                 }
  116.             }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement