Advertisement
gospod1978

Object and Class/Ex/Vehicle Catalogue

Oct 30th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TeamworkProjects
  6. {
  7.     class Car
  8.     {
  9.         public string Type { get; set; }
  10.         public string Model { get; set; }
  11.          public string Color { get; set; }
  12.         public int Power { get; set; }
  13.        
  14.          
  15.          public Car (string type, string model, string color, int power)
  16.          {
  17.             this.Type = type;
  18.              this.Model = model;
  19.              this.Color = color;
  20.              this.Power = power;
  21.          }
  22.          
  23.          public override string ToString()
  24.         {
  25.             return $"Type: {this.Type}\nModel: {this.Model}\nColor: {this.Color}\nHorsepower: {this.Power}\n";
  26.         }
  27.     }
  28.    
  29.     class Truck
  30.     {
  31.         public string Type { get; set; }
  32.         public string Model { get; set; }
  33.          public string Color { get; set; }
  34.         public int Power { get; set; }
  35.        
  36.          
  37.          public Truck (string type, string model, string color, int power)
  38.          {
  39.             this.Type = type;
  40.              this.Model = model;
  41.              this.Color = color;
  42.              this.Power = power;
  43.          }
  44.          
  45.          public override string ToString()
  46.         {
  47.             return $"Type: {this.Type}\nModel: {this.Model}\nColor: {this.Color}\nHorsepower: {this.Power}\n";
  48.         }
  49.     }
  50.     class Program
  51.     {
  52.         static void Main(string[] args)
  53.         {
  54.            string input;
  55.            List<Car> cars = new List<Car>();
  56.            List<Truck> trucks = new List<Truck>();
  57.            double carPower = 0;
  58.            double carAdd = 0;
  59.            double truckPower = 0;
  60.            double truckAdd = 0;
  61.            
  62.           while ((input = Console.ReadLine()) != "End")
  63.            {
  64.             string[] array = input.Split();
  65.            
  66.             string type = array[0];
  67.             string model = array[1];
  68.             string color = array[2];
  69.            int power = int.Parse(array[3]);
  70.            //Console.WriteLine(type);
  71.            
  72.            if (type == "car")
  73.               {
  74.                 type = "Car";
  75.              
  76.             Car carList = new Car(type, model, color, power);
  77.                 cars.Add(carList);
  78.                 carPower += power;
  79.                 carAdd++;
  80.               }
  81.            else
  82.               {
  83.                 type = "Truck";
  84.              
  85.             Truck truckList = new Truck(type, model, color, power);
  86.                trucks.Add(truckList);
  87.                truckPower += power;
  88.                truckAdd++;
  89.                //Console.WriteLine(truckAdd);
  90.               }
  91.                  
  92.            }
  93.            
  94.            string output;
  95.            
  96.            while((output = Console.ReadLine()) != "Close the Catalogue")
  97.            {
  98.              Console.Write(cars.Find(x => x.Model == output));
  99.                 Console.Write(trucks.Find(x => x.Model == output));
  100.            }
  101.            //Console.WriteLine();
  102.            double carAv = 0;
  103.            if (carAdd != 0)
  104.            {
  105.              carAv = carPower / carAdd;
  106.            }
  107.            
  108.            double truckAv = 0;
  109.            if (truckAdd != 0)
  110.            {
  111.             truckAv = truckPower / truckAdd;
  112.            }
  113.          
  114.            
  115.            
  116.             Console.WriteLine($"Cars have average horsepower of: {carAv:f2}.");
  117.                Console.WriteLine($"Trucks have average horsepower of: {truckAv:f2}.");
  118.            
  119.            
  120.            
  121.             //Console.WriteLine($"Trucks have average horsepower of: {truckAv:f2}.");
  122.            
  123.             //Console.WriteLine($"Cars have average horsepower of: {carAv:f2}.");
  124.            
  125.          
  126.        
  127.         }
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement