Initial commit
This commit is contained in:
@@ -4,16 +4,17 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CarManagerV2
|
||||
namespace CarManagerV3
|
||||
{
|
||||
internal class StateManager
|
||||
{
|
||||
|
||||
static List<Car> cars = new List<Car>();
|
||||
static string filePath = "cars.csv";
|
||||
|
||||
public static Car GetCarById(int id)
|
||||
{
|
||||
cars = SafeManager.ReadCars("cars.csv");
|
||||
cars = SafeManager.ReadCars(filePath);
|
||||
return cars.FirstOrDefault(c => c.Id == id);
|
||||
}
|
||||
|
||||
@@ -21,18 +22,18 @@ namespace CarManagerV2
|
||||
|
||||
public static void AddCar(Car car)
|
||||
{
|
||||
cars = SafeManager.ReadCars("cars.csv");
|
||||
cars = SafeManager.ReadCars(filePath);
|
||||
cars.Add(car);
|
||||
SafeManager.SaveCars("cars.csv", cars);
|
||||
SafeManager.SaveCars(filePath, cars);
|
||||
}
|
||||
|
||||
public static void RemoveCar(Car car)
|
||||
{
|
||||
cars = SafeManager.ReadCars("cars.csv");
|
||||
cars = SafeManager.ReadCars(filePath);
|
||||
Car existingCar = GetCarById(car.Id);
|
||||
if (existingCar == null) return;
|
||||
cars.Remove(existingCar);
|
||||
SafeManager.SaveCars("cars.csv", cars);
|
||||
SafeManager.SaveCars(filePath, cars);
|
||||
}
|
||||
|
||||
public static void UpdateCar(Car car)
|
||||
@@ -43,17 +44,22 @@ namespace CarManagerV2
|
||||
int index = cars.IndexOf(existingCar);
|
||||
cars[index] = car;
|
||||
Console.WriteLine("Updated car: " + existingCar.Id);
|
||||
SafeManager.SaveCars("cars.csv", cars);
|
||||
SafeManager.SaveCars(filePath, cars);
|
||||
}
|
||||
}
|
||||
|
||||
public static Car CreateCar(string make, string model, int year, string color, int mileage, decimal price)
|
||||
{
|
||||
cars = SafeManager.ReadCars("cars.csv");
|
||||
cars = SafeManager.ReadCars(filePath);
|
||||
int newId = cars.Count > 0 ? cars.Max(c => c.Id) + 1 : 1;
|
||||
Car newCar = new Car(newId, make, model, year, color, mileage, price);
|
||||
AddCar(newCar);
|
||||
return newCar;
|
||||
}
|
||||
|
||||
public static void setFilePath(string path)
|
||||
{
|
||||
filePath = path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user