Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _06._Vehicle_Catalogue
- {
- class Program
- {
- static void Main(string[] args)
- {
- var catalog = new Catalog();
- while (true)
- {
- string command = Console.ReadLine();
- if (command == "End")
- {
- break;
- }
- string[] tokens = command.Split();
- string type = tokens[0];
- string model = tokens[1];
- string color = tokens[2];
- int horsePower = int.Parse(tokens[3]);
- if (type == "car")
- {
- var car = new Car(model, color, horsePower);
- catalog.Cars.Add(car);
- }
- else if (type == "truck")
- {
- var truck = new Truck(model, color, horsePower);
- catalog.Trucks.Add(truck);
- }
- }
- while (true)
- {
- string command = Console.ReadLine();
- if (command == "Close the Catalogue")
- {
- break;
- }
- var car = catalog.Cars.Where(x => x.Model == command);
- var truck = catalog.Trucks.Where(x => x.Model == command);
- foreach (var item in car)
- {
- Console.WriteLine(item);
- }
- foreach (var item in truck)
- {
- Console.WriteLine(item);
- }
- }
- if (catalog.Cars.Count > 0)
- {
- var carsHp = catalog.Cars.Average(x => x.HorsePower);
- Console.WriteLine($"Cars have average horsepower of: {carsHp:f2}.");
- }
- else
- {
- Console.WriteLine($"Cars have average horsepower of: {0:F2}.");
- }
- if (catalog.Trucks.Count > 0)
- {
- var trucksHp = catalog.Trucks.Average(x => x.HorsePower);
- Console.WriteLine($"Trucks have average horsepower of: {trucksHp:f2}.");
- }
- else
- {
- Console.WriteLine($"Trucks have average horsepower of: {0:F2}.");
- }
- }
- }
- class Catalog
- {
- public Catalog()
- {
- Cars = new List<Car>();
- Trucks = new List<Truck>();
- }
- public List<Car> Cars { get; set; }
- public List<Truck> Trucks { get; set; }
- }
- class Car
- {
- public string Model { get; set; }
- public string Color { get; set; }
- public int HorsePower { get; set; }
- public Car(string model, string color, int horsePower)
- {
- this.Model = model;
- this.Color = color;
- this.HorsePower = horsePower;
- }
- public override string ToString()
- {
- return $"Type: Car\nModel: {this.Model}\nColor: {this.Color}\nHorsepower: {this.HorsePower}";
- }
- }
- class Truck
- {
- public string Model { get; set; }
- public string Color { get; set; }
- public int HorsePower { get; set; }
- public Truck(string model, string color, int horsePower)
- {
- this.Model = model;
- this.Color = color;
- this.HorsePower = horsePower;
- }
- public override string ToString()
- {
- return $"Type: Truck\nModel: {this.Model}\nColor: {this.Color}\nHorsepower: {this.HorsePower}";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement