Advertisement
gospod1978

Object and Class/Vehicle Catalogue

Oct 24th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Globalization;
  6. using Cataloga;
  7.  
  8. namespace Cataloga
  9. {
  10.     class Car
  11.     {
  12.  
  13.         public string Brand { get; set; }
  14.         public string Model { get; set; }
  15.         public string Weight { get; set; }
  16.  
  17.         public Car(string brand, string model, string weight)
  18.         {
  19.             this.Brand = brand;
  20.             this.Model = model;
  21.             this.Weight = weight;
  22.         }
  23.  
  24.         public override string ToString()
  25.         {
  26.             return $"{this.Brand}: {this.Model} - {this.Weight}hp";
  27.         }
  28.     }
  29.  
  30.     class Truck
  31.     {
  32.         public string Brand { get; set; }
  33.         public string Model { get; set; }
  34.         public string Weight { get; set; }
  35.  
  36.         public Truck(string brand, string model, string weight)
  37.         {
  38.             this.Brand = brand;
  39.             this.Model = model;
  40.             this.Weight = weight;
  41.         }
  42.  
  43.         public override string ToString()
  44.         {
  45.             return $"{this.Brand}: {this.Model} - {this.Weight}kg";
  46.         }
  47.     }
  48.  
  49.  
  50.     class MainClass
  51.     {
  52.         public static void Main(string[] args)
  53.         {
  54.             List<Car> cars = new List<Car>();
  55.             List<Truck> trucks = new List<Truck>();
  56.  
  57.             string input;
  58.             int sum = 0;
  59.             int sum2 = 0;
  60.  
  61.             while ((input = Console.ReadLine()) != "end")
  62.             {
  63.                 string[] data = input.Split("/");
  64.  
  65.                 string type = data[0];
  66.                 if (type == "Car")
  67.                 {
  68.                     sum++;
  69.  
  70.                     string brand = data[1];
  71.                     string model = data[2];
  72.                     string weight = data[3];
  73.                     Car newCar = new Car(brand, model, weight);
  74.                     cars.Add(newCar);
  75.                 }
  76.                 else
  77.                 {
  78.                     sum2++;
  79.  
  80.                     string brand = data[1];
  81.                     string model = data[2];
  82.                     string weight = data[3];
  83.                     Truck newTruck = new Truck(brand, model, weight);
  84.                     trucks.Add(newTruck);
  85.                 }
  86.  
  87.             }
  88.  
  89.             cars = cars.OrderBy(x => x.Brand).ToList();
  90.             trucks = trucks.OrderBy(y => y.Brand).ToList();
  91.  
  92.             if (sum != 0)
  93.             {
  94.  
  95.                 Console.WriteLine("Cars:");
  96.                 Console.WriteLine(string.Join(Environment.NewLine, cars));
  97.             }
  98.             if (sum2 != 0)
  99.             {
  100.                 Console.WriteLine("Trucks:");
  101.                 Console.WriteLine(string.Join(Environment.NewLine, trucks));
  102.             }
  103.  
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement