Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 08.ObjectsAndClasses-Vehicle Catalogue
- Your task is to create a Vehicle catalogue, which contains only Trucks and Cars.
- Define a class Truck with the following properties: Brand, Model and Weight.
- Define a class Car with the following properties: Brand, Model and Horse Power.
- Define a class Catalog with the following properties: Collections of Trucks and Cars.
- You must read the input until you receive the "end" command. It will be in following format: {type}/{brand}/{model}/{horse power / weight}
- In the end you have to print all of the vehicles ordered alphabetical by brand, in the following format:
- Cars:
- {Brand}: {Model} - {Horse Power}hp
- Trucks:
- {Brand}: {Model} - {Weight}kg
- Examples
- Input Output
- Car/Audi/A3/110 Cars:
- Car/Maserati/Levante/350 Audi: A3 - 110hp
- Truck/Mercedes/Actros/9019 Maserati: Levante - 350hp
- Car/Porsche/Panamera/375 Porsche: Panamera - 375hp
- end Trucks:
- Mercedes: Actros - 9019kg
- Car/Subaru/Impreza/152 Cars:
- Car/Peugeot/307/109 Peugeot: 307 - 109hp
- end Subaru: Impreza - 152hp
- Hints
- This is how your class Catalog should look like.
- Don’t forget to create instances for the two Lists.
- You can do it in the constructor of CatalogueVehicle.
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace VehicleCatalogue
- {
- class Cars
- {
- public string Brand { get; set; }
- public string Model { get; set; }
- public int HorsePower { get; set; }
- }
- class Trucks
- {
- public string Brand { get; set; }
- public string Model { get; set; }
- public int TruckWeight { get; set; }
- }
- }
- namespace VehicleCatalogue
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Cars> carBrand = new List<Cars>();
- List<Trucks> truckBrand = new List<Trucks>();
- while (true)
- {
- string command = Console.ReadLine();
- string[] parts = command.Split('/');
- if (command == "end")
- {
- break;
- }
- else if (parts[0] == "Car")
- {
- string brand = parts[1]; //Не запазваме в променливи всеки елемент от масива, а само нужните за обекта Cars.
- string model = parts[2];
- int horsesPowers = int.Parse(parts[3]);
- var cars = new Cars();
- cars.Brand = brand;
- cars.Model = model;
- cars.HorsePower = horsesPowers;
- carBrand.Add(cars);
- }
- else
- {
- string brand = parts[1]; //Не запазваме в променливи всеки елемент от масива, а само нужните за обекта Trucks.
- string model = parts[2];
- int truckWeight = int.Parse(parts[3]);
- var trucks = new Trucks();
- trucks.Brand = brand;
- trucks.Model = model;
- trucks.TruckWeight = truckWeight;
- truckBrand.Add(trucks);
- }
- }
- List<Cars> sortedCars = carBrand.OrderBy(x => x.Brand).ToList();//Сортираме ги по азбучен ред.
- List<Trucks> sortedTrucks = truckBrand.OrderBy(x => x.Brand).ToList();
- Console.WriteLine("Cars:");
- foreach (Cars item in sortedCars)
- {
- Console.WriteLine($"{item.Brand}: {item.Model} - {item.HorsePower}hp");
- }
- Console.WriteLine("Trucks:");
- foreach (Trucks item in sortedTrucks)
- {
- Console.WriteLine($"{item.Brand}: {item.Model} - {item.TruckWeight}kg");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment