Проблем със задача 8Vehicle Catalogue
Някой може ли да ми обясни защо като добавям с листа нова кола променя и вече добавените - най вероятно има нещо с референциите, но не го откривам. Може ли за обяснение и решение
Много ви благодаря
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Reflection;
namespace ConsoleApp7
{
class Program
{
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split('/').ToArray();
Catalog myCatalog = new Catalog();
while (input[0] != "end")
{
string brand;
string model;
string weight;
string hoursePower;
if (input[0] == "Car")
{
brand = input[1];
model = input[2];
hoursePower = input[3];
myCatalog.car.Brand = brand;
myCatalog.car.Model = model;
myCatalog.car.HoursPower = hoursePower;
myCatalog.cars.Add(myCatalog.car);
}
else
{
brand = input[1];
model = input[2];
weight = input[3];
myCatalog.truck.Brand = brand;
myCatalog.truck.Model = model;
myCatalog.truck.Weight = weight;
myCatalog.trucks.Add(myCatalog.truck);
}
input = Console.ReadLine().Split('/').ToArray();
}
myCatalog.cars.Sort();
myCatalog.trucks.Sort();
Console.WriteLine("Cars:");
foreach (var item in myCatalog.cars)
{
Console.WriteLine("{0}: {1} - {2}hp", item.Brand, item.Model, item.HoursPower);
}
Console.WriteLine("Trucks:");
foreach (var item in myCatalog.trucks)
{
Console.WriteLine("{0}: {1} - {2}kg", item.Brand, item.Model, item.Weight);
}
}
class Truck
{
public string Brand { get; set; }
public string Model { get; set; }
public string Weight { get; set; }
}
class Car
{
public string Brand { get; set; }
public string Model { get; set; }
public string HoursPower { get; set; }
}
class Catalog
{
public Catalog()
{
truck = new Truck();
car = new Car();
}
public List<Truck> trucks = new List<Truck>();
public List<Car> cars = new List<Car>();
public Car car { get; set; }
public Truck truck { get; set; }
}
}
}