C
C#11mo ago
Stask

Problem with a Vehicle Catalogue

I am trying to solve a problem where i need to create something like a vehicle catalogue it works for the most part but im still not getting the right answers on some inputs this is my code: https://pastebin.com/n82eac2U
Pastebin
using System;using System.Collections.Generic;using System.Linq;usi...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
No description
14 Replies
Angius
Angius11mo ago
On which inputs? And how are the results wrong?
Stask
StaskOP11mo ago
thats the thing i don't know the inputs it just tells me im wrong
Stask
StaskOP11mo ago
No description
Angius
Angius11mo ago
Does it work for the example input?
Stask
StaskOP11mo ago
yes
Angius
Angius11mo ago
One of the requirements is to print $"{typeOfVehicle} have average horsepower of: {horsePower}." You have hardcoded Trucks and Cars What if the user creates a new vehicle of type Scooter?
Stask
StaskOP11mo ago
its said in the constraints that it will either be a car or a truck
Angius
Angius11mo ago
I wonder if it maybe expects you to print the average HP even if there are no vehicles of that type The exercise says print one and the other So it might just expect the evrage of trucks to be 0.00 when there are no trucks It's mostly just guesswork, though, without knowing what the tests are ¯\_(ツ)_/¯
Stask
StaskOP11mo ago
i will try without them yeah pretty annoying i thought someone could maybe spot a rookie mistake here nope still not working
Sir Rufo
Sir Rufo11mo ago
You can try it with this code
c#
class Program
{
private const string END_OF_PARSE = "End";
private const string END_OF_QUERY = "Close the Catalogue";

public static void Main( string[] args )
{
var vehicles = ParseVehicles().ToList();
QueryVehicles( vehicles );
PrintStatistics( vehicles );
}

private static IEnumerable<Vehicle> ParseVehicles()
{
while ( true )
{
var input = Console.ReadLine();
if ( input == END_OF_PARSE ) yield break;
if ( input is null ) continue;

var parts = input.Split( " " );
yield return new Vehicle
{
Type = Enum.Parse<VehicleType>( parts[0], true ),
Model = parts[1],
Color = parts[2],
Horsepower = int.Parse( parts[3] ),
};
}
}

private static void QueryVehicles( ICollection<Vehicle> vehicles )
{
while ( true )
{
var input = Console.ReadLine();
if ( input == END_OF_QUERY ) return;

var v = vehicles.FirstOrDefault( e => e.Model.Equals( input ) );
if ( v is null )
continue;

foreach ( var prop in typeof( Vehicle ).GetProperties() )
{
Console.WriteLine( "{0}: {1}", prop.Name, prop.GetValue( v ) );
}
}
}

private static void PrintStatistics( ICollection<Vehicle> vehicles )
{
foreach ( var group in vehicles.GroupBy( e => e.Type ).OrderBy( g => g.Key ) )
{
Console.WriteLine( "{0}s have average horsepower of: {1:0.00}.", group.Key, group.Average( e => e.Horsepower ) );
}
}
}

class Vehicle
{
public VehicleType Type { get; set; }
public string Model { get; set; } = string.Empty;
public string Color { get; set; } = string.Empty;
public int Horsepower { get; set; }
}

enum VehicleType
{
Car,
Truck,
}
c#
class Program
{
private const string END_OF_PARSE = "End";
private const string END_OF_QUERY = "Close the Catalogue";

public static void Main( string[] args )
{
var vehicles = ParseVehicles().ToList();
QueryVehicles( vehicles );
PrintStatistics( vehicles );
}

private static IEnumerable<Vehicle> ParseVehicles()
{
while ( true )
{
var input = Console.ReadLine();
if ( input == END_OF_PARSE ) yield break;
if ( input is null ) continue;

var parts = input.Split( " " );
yield return new Vehicle
{
Type = Enum.Parse<VehicleType>( parts[0], true ),
Model = parts[1],
Color = parts[2],
Horsepower = int.Parse( parts[3] ),
};
}
}

private static void QueryVehicles( ICollection<Vehicle> vehicles )
{
while ( true )
{
var input = Console.ReadLine();
if ( input == END_OF_QUERY ) return;

var v = vehicles.FirstOrDefault( e => e.Model.Equals( input ) );
if ( v is null )
continue;

foreach ( var prop in typeof( Vehicle ).GetProperties() )
{
Console.WriteLine( "{0}: {1}", prop.Name, prop.GetValue( v ) );
}
}
}

private static void PrintStatistics( ICollection<Vehicle> vehicles )
{
foreach ( var group in vehicles.GroupBy( e => e.Type ).OrderBy( g => g.Key ) )
{
Console.WriteLine( "{0}s have average horsepower of: {1:0.00}.", group.Key, group.Average( e => e.Horsepower ) );
}
}
}

class Vehicle
{
public VehicleType Type { get; set; }
public string Model { get; set; } = string.Empty;
public string Color { get; set; } = string.Empty;
public int Horsepower { get; set; }
}

enum VehicleType
{
Car,
Truck,
}
Stask
StaskOP11mo ago
No description
Stask
StaskOP11mo ago
Stask
StaskOP11mo ago
link to the problem
Want results from more Discord servers?
Add your server