JulianJulianov

08.ObjectsAndClasses-Vehicle Catalogue

Mar 5th, 2020
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. 08.ObjectsAndClasses-Vehicle Catalogue
  2. Your task is to create a Vehicle catalogue, which contains only Trucks and Cars.
  3. Define a class Truck with the following properties: Brand, Model and Weight.
  4. Define a class Car with the following properties: Brand, Model and Horse Power.
  5. Define a class Catalog with the following properties: Collections of Trucks and Cars.
  6. You must read the input until you receive the "end" command. It will be in following format: {type}/{brand}/{model}/{horse power / weight}
  7. In the end you have to print all of the vehicles ordered alphabetical by brand, in the following format:
  8. Cars:
  9. {Brand}: {Model} - {Horse Power}hp
  10. Trucks:
  11. {Brand}: {Model} - {Weight}kg
  12. Examples
  13. Input                                      Output
  14. Car/Audi/A3/110                            Cars:
  15. Car/Maserati/Levante/350                   Audi: A3 - 110hp
  16. Truck/Mercedes/Actros/9019                 Maserati: Levante - 350hp
  17. Car/Porsche/Panamera/375                   Porsche: Panamera - 375hp
  18. end                                        Trucks:
  19.                                            Mercedes: Actros - 9019kg
  20.  
  21. Car/Subaru/Impreza/152                     Cars:
  22. Car/Peugeot/307/109                        Peugeot: 307 - 109hp
  23. end                                        Subaru: Impreza - 152hp
  24.  
  25.  
  26. Hints
  27. This is how your class Catalog should look like.
  28.  
  29. Don’t forget to create instances for the two Lists.
  30. You can do it in the constructor of CatalogueVehicle.
  31.  
  32. using System;
  33. using System.Linq;
  34. using System.Collections.Generic;
  35.  
  36. namespace VehicleCatalogue
  37. {
  38.     class Cars    
  39.     {
  40.         public string Brand { get; set; }
  41.         public string Model { get; set; }
  42.         public int HorsePower { get; set; }
  43.     }
  44.     class Trucks
  45.     {
  46.         public string Brand { get; set; }
  47.         public string Model { get; set; }
  48.         public int TruckWeight { get; set; }
  49.     }
  50. }
  51. namespace VehicleCatalogue
  52. {
  53.     class Program
  54.     {
  55.         static void Main(string[] args)
  56.         {
  57.             List<Cars> carBrand = new List<Cars>();
  58.             List<Trucks> truckBrand = new List<Trucks>();                                            
  59.  
  60.             while (true)
  61.             {
  62.                 string command = Console.ReadLine();
  63.                 string[] parts = command.Split('/');
  64.  
  65.                 if (command == "end")
  66.                 {
  67.                     break;
  68.                 }
  69.                 else if (parts[0] == "Car")
  70.                 {
  71.                     string brand = parts[1];     //Не запазваме в променливи всеки елемент от масива, а само нужните за обекта Cars.
  72.                     string model = parts[2];
  73.                     int horsesPowers = int.Parse(parts[3]);
  74.  
  75.                     var cars = new Cars();        
  76.                                              
  77.                     cars.Brand = brand;
  78.                     cars.Model = model;
  79.                     cars.HorsePower = horsesPowers;
  80.  
  81.                     carBrand.Add(cars);
  82.                 }
  83.                 else
  84.                 {
  85.                     string brand = parts[1];     //Не запазваме в променливи всеки елемент от масива, а само нужните за обекта Trucks.
  86.                     string model = parts[2];
  87.                     int truckWeight = int.Parse(parts[3]);
  88.  
  89.                     var trucks = new Trucks();
  90.                                                                              
  91.                     trucks.Brand = brand;
  92.                     trucks.Model = model;
  93.                     trucks.TruckWeight = truckWeight;
  94.  
  95.                     truckBrand.Add(trucks);
  96.                 }
  97.             }
  98.             List<Cars> sortedCars = carBrand.OrderBy(x => x.Brand).ToList();//Сортираме ги по азбучен ред.
  99.             List<Trucks> sortedTrucks = truckBrand.OrderBy(x => x.Brand).ToList();
  100.        
  101.             Console.WriteLine("Cars:");
  102.             foreach (Cars item in sortedCars)
  103.             {
  104.                     Console.WriteLine($"{item.Brand}: {item.Model} - {item.HorsePower}hp");
  105.             }
  106.             Console.WriteLine("Trucks:");
  107.             foreach (Trucks item in sortedTrucks)
  108.             {
  109.                     Console.WriteLine($"{item.Brand}: {item.Model} - {item.TruckWeight}kg");
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment