57 lines
1.6 KiB
C#
57 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CarManagerV2
|
|
{
|
|
internal class StateManager
|
|
{
|
|
|
|
static List<Car> cars = new List<Car>();
|
|
|
|
public static Car getCarById(int id)
|
|
{
|
|
cars = SafeManager.readCars("cars.csv");
|
|
return cars.FirstOrDefault(c => c.Id == id);
|
|
}
|
|
|
|
public static List<Car> Cars { get { return cars; } set { cars = value; } }
|
|
|
|
public static void addCar(Car car)
|
|
{
|
|
cars = SafeManager.readCars("cars.csv");
|
|
cars.Add(car);
|
|
SafeManager.saveCars("cars.csv", cars);
|
|
}
|
|
|
|
public static void removeCar(Car car)
|
|
{
|
|
cars = SafeManager.readCars("cars.csv");
|
|
Console.WriteLine(cars);
|
|
cars.Remove(car);
|
|
Console.WriteLine(cars);
|
|
SafeManager.saveCars("cars.csv", cars);
|
|
}
|
|
|
|
public static void updateCar(Car car)
|
|
{
|
|
Car existingCar = getCarById(car.Id);
|
|
if (existingCar != null)
|
|
{
|
|
cars.Remove(existingCar);
|
|
cars.Add(car);
|
|
}
|
|
}
|
|
|
|
public static void createCar(string make, string model, int year, string color, int mileage, decimal price)
|
|
{
|
|
cars = SafeManager.readCars("cars.csv");
|
|
int newId = cars.Count > 0 ? cars.Max(c => c.Id) + 1 : 1;
|
|
Car newCar = new Car(newId, make, model, year, color, mileage, price);
|
|
cars.Add(newCar);
|
|
}
|
|
}
|
|
}
|