Code: Select all
cars.cs
Code: Select all
public class Auto
{
public int Id { get; set; }
public string Brand { get; set; }
public int Year { get; set; }
public int Price
{
get; set;
}
public Auto(int id, string brand, int year, int price)
{
Id = id;
Brand = brand;
Year = year;
Price = price;
}
}
< /code>
Program.cs
namespace proba2
{
internal class Program
{
public static List cars = new List();
static void Main(string[] args)
{
read();
foreach (var car in cars)
{
Console.WriteLine($"{car.Id} {car.Year} {car.Price} {car.Brand}");
}
Auto younger = cars.OrderBy(a => a.Year).Last();
Auto older = cars.OrderBy(a => a.Year).First();
double avg = cars.Average(a => a.Price);
Console.WriteLine($"The oldest:{older.Year}");
Console.WriteLine($"The oldest:{younger.Year}");
Console.WriteLine($"Avg price:{avg}");
Console.WriteLine("how many km: ");
string km = Console.ReadLine();
Console.WriteLine("Fuel price: ");
string fuelprice = Console.ReadLine();
Console.WriteLine("Fuel using: ");
string fuelusing = Console.ReadLine();
Total(int.Parse(km), int.Parse(fuelprice), int.Parse(fuelusing));
}
public static void Total(int km, int fuelprice, int fuelusing)
{
int totalfuel = (km * fueusing) / 100;
int cost = totalfuel * fuelprice;
Console.WriteLine($"The fuel cost: {cost}");
}
public static void read()
{
string filename = "C:\\Users\\heten\\Desktop\\proba2\\proba2\\autok.csv";
string[] lines = File.ReadAllLines(filename);
foreach (var line in lines.Skip(1))
{
var tag = line.Split(',');
if (tag.Length == 4)
{
cars.Add(new Auto(int.Parse(tag[0]), tag[1], int.Parse(tag[2]), int.Parse(tag[3])));
}
}
}
}
}
< /code>
Warum erhalte ich einen Fehler, wenn ich versuche, ein Auto -Objekt durch Durchwanderung (a => a.price) zuzuweisen? Ich möchte entweder den Durchschnittspreis oder das Auto finden, das dem Durchschnitt am nächsten liegt. Was ist der richtige Weg, dies zu tun?