antonfotev 3 Точки

## Objects and classes Exercise- 6. Vehicle catalogue

Здравейте. Дава ми 33 точки.
Условието:
Objects and classes Exercise- 6. Vehicle catalogue
You have to make a catalogue for vehicles. You will receive two types of vehicle – car or truck.
Until you receive the command “End” you will receive lines of input in the format:
{typeOfVehicle} {model} {color} {horsepower}
After the “End” command, you will start receiving models of vehicles. Print for every received vehicle
its data in the format:
Type: {typeOfVehicle}

Model: {modelOfVehicle}
Color: {colorOfVehicle}
Horsepower: {horsepowerOfVehicle}
When you receive the command “Close the Catalogue”, stop receiving input and print the average
horsepower for the cars and for the trucks in the format:
{typeOfVehicles} have average horsepower of {averageHorsepower}.
The average horsepower is calculated by dividing the sum of horsepower for all vehicles of the type
by the total count of vehicles from the same type.
Format the answer to the 2 nd decimal point.
Constraints
 The type of vehicle will always be car or truck.
 You will not receive the same model twice.
 The received horsepower will be integer in the interval [1…1000]
 You will receive at most 50 vehicles.
 Single whitespace will be used for separator.

КОДЪТ МИ Е

using System;
using System.Collections.Generic;
using System.Linq;

public class Vehicle
{
public Vehicle(){}
public Vehicle(string type, string model, string color, int horsepower)
{
this.Type = type;
this.Model= model;
this.Color = color;
this.Horsepower = horsepower;
}

public string Type {get; set;}
public string Model {get; set;}
public string Color {get; set;}
public int Horsepower {get; set;}
}

public class Program
{
public static void Main()
{
var orderVehiclkles = new List<Vehicle>();

string inputPrim = null;

while ((inputPrim = Console.ReadLine()) != "End")
{
string[] input = inputPrim
.Split();

} // end while

while ((inputPrim = Console.ReadLine()) != "Close the Catalogue")
{
foreach (var kvp in orderVehiclkles)
{
if(kvp.Model == inputPrim)
{
var currnetVehicle = new Vehicle(kvp.Type, kvp.Model, kvp.Color, kvp.Horsepower);
GetPrintTisVehickle(currnetVehicle);
break;
}
} // first foreach
} // end while

decimal CarsHorsepower = 0;
decimal TrucksHorsepower = 0;

foreach (var kvp in orderVehiclkles)
{
if(kvp.Type == "car")
{
CarsHorsepower += kvp.Horsepower;
}
else
{
TrucksHorsepower += kvp.Horsepower;
}

} // first foreach
int n = orderVehiclkles.Where(s => s.Type == "car").Count();
int x = orderVehiclkles.Where(s => s.Type == "truck").Count();
if(n > 0)
{
CarsHorsepower = CarsHorsepower / n;
Console.WriteLine("Cars have average horsepower of: {0:0.00}.", CarsHorsepower);
}
else
{
Console.WriteLine("Cars have average horsepower of: 0.00");
}

if (x > 0)
{
TrucksHorsepower = TrucksHorsepower / x;
Console.WriteLine("Trucks have average horsepower of: {0:0.00}.", TrucksHorsepower);
}
else
{
Console.WriteLine("Trucks have average horsepower of: 0.00");
} // end dable if-else

}

public static void GetPrintTisVehickle(Vehicle currnetVehicle)
{
if(currnetVehicle.Type == "car")
{
Console.WriteLine("Type: Car");
}
else
{
Console.WriteLine("Type: Truck");
}

Console.WriteLine("Model: {0}", currnetVehicle.Model);
Console.WriteLine("Color: {0}", currnetVehicle.Color);
Console.WriteLine("Horsepower: {0}", currnetVehicle.Horsepower);
}
}

// truck Man red 200
// truck Mercedes blue 300
// car Ford green 120
// car Ferrari red 550
// car Lamborghini orange 570
// End
// Ferrari
// Ford
// Man
// Close the Catalogue

// Type: Car
// Model: Ferrari
// Color: red
// Horsepower: 550
// Type: Car
// Model: Ford
// Color: green
// Horsepower: 120
// Type: Truck
// Model: Man
// Color: red
// Horsepower: 200
// Cars have average horsepower of: 413.33.
// Trucks have average horsepower of: 250.00.

Тагове:
0
grafcheto 2 Точки

using System;

using System.Collections.Generic;

using System.Linq;

namespace Teamwork_Projects

{

class Program

{

static void Main(string[] args)

{

List<CarsAndTrucks> vehicles = new List<CarsAndTrucks>();

while (true)

{

if (input == "End")

{

break;

}

string[] tokens = input.Split();

string type = tokens[0];

if (type == "car")

{

type = "Car";

}

else if (type == "truck")

{

type = "Truck";

}

string model = tokens[1];

string color = tokens[2];

int horsePower = int.Parse(tokens[3]);

CarsAndTrucks vehicle = new CarsAndTrucks()

{

Type = type,

Model = model,

Color = color,

HorsePower = horsePower

};

}

while (command != "Close the Catalogue")

{

string currentVehicle = command;

foreach (CarsAndTrucks item in vehicles.Where(x => x.Model == currentVehicle))

{

Console.WriteLine(\$"Type: {item.Type}");

Console.WriteLine(\$"Model: {item.Model}");

Console.WriteLine(\$"Color: {item.Color}");

Console.WriteLine(\$"Horsepower: {item.HorsePower}");

}

}

List<CarsAndTrucks> cars = vehicles.Where(x => x.Type == "Car").ToList();

List<CarsAndTrucks> trucks = vehicles.Where(x => x.Type == "Truck").ToList();

int countOfCars = cars.Count;

double totalCarsHp = 0;

foreach (var car in cars)

{

totalCarsHp += car.HorsePower;

}

double carsHpAverage = totalCarsHp / countOfCars;

Console.WriteLine(\$"Cars have average horsepower of: {carsHpAverage:f2}.");

int countOfTrucks = trucks.Count;

double totalTrucksHp = 0;

foreach (var truck in trucks)

{

totalTrucksHp += truck.HorsePower;

}

double trucksHpAverage = totalTrucksHp / countOfTrucks;

Console.WriteLine(\$"Trucks have average horsepower of: {trucksHpAverage:f2}.");

}

class CarsAndTrucks

{

public string Type { get; set; }

public string Model { get; set; }

public string Color { get; set; }

public int HorsePower { get; set; }

}

}

}

0