feat: GUID
This commit is contained in:
@@ -7,7 +7,7 @@ namespace CarManagerV3
|
||||
/// </summary>
|
||||
public class Car
|
||||
{
|
||||
private int id;
|
||||
private string id;
|
||||
private string make;
|
||||
private string model;
|
||||
private int year;
|
||||
@@ -16,14 +16,11 @@ namespace CarManagerV3
|
||||
private decimal price;
|
||||
private int order;
|
||||
|
||||
public int Id { get => id;
|
||||
set {
|
||||
if (value < 0) throw new ArgumentException("Id cannot be negative.");
|
||||
id = value;
|
||||
}
|
||||
}
|
||||
public string Id { get => id; }
|
||||
|
||||
public string Make { get => make;
|
||||
public string Make
|
||||
{
|
||||
get => make;
|
||||
set
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Make cannot be empty.");
|
||||
@@ -31,7 +28,9 @@ namespace CarManagerV3
|
||||
}
|
||||
}
|
||||
|
||||
public string Model { get => model;
|
||||
public string Model
|
||||
{
|
||||
get => model;
|
||||
set
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Model cannot be empty.");
|
||||
@@ -39,7 +38,9 @@ namespace CarManagerV3
|
||||
}
|
||||
}
|
||||
|
||||
public int Year { get => year;
|
||||
public int Year
|
||||
{
|
||||
get => year;
|
||||
set
|
||||
{
|
||||
if (value < 1886 || value > DateTime.Now.Year + 1) throw new ArgumentException("Year must be between 1886 and next year.");
|
||||
@@ -47,7 +48,9 @@ namespace CarManagerV3
|
||||
}
|
||||
}
|
||||
|
||||
public string Color { get => color;
|
||||
public string Color
|
||||
{
|
||||
get => color;
|
||||
set
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Color cannot be empty.");
|
||||
@@ -55,7 +58,9 @@ namespace CarManagerV3
|
||||
}
|
||||
}
|
||||
|
||||
public int Mileage { get => mileage;
|
||||
public int Mileage
|
||||
{
|
||||
get => mileage;
|
||||
set
|
||||
{
|
||||
if (value < 0) throw new ArgumentException("Mileage cannot be negative.");
|
||||
@@ -63,7 +68,9 @@ namespace CarManagerV3
|
||||
}
|
||||
}
|
||||
|
||||
public decimal Price { get => price;
|
||||
public decimal Price
|
||||
{
|
||||
get => price;
|
||||
set
|
||||
{
|
||||
if (value < 0) throw new ArgumentException("Price cannot be negative.");
|
||||
@@ -102,10 +109,21 @@ namespace CarManagerV3
|
||||
/// <param name="mileage">The current mileage on the car.</param>
|
||||
/// <param name="price">The selling-price of the car.</param>
|
||||
/// <param name="order">The order.</param>
|
||||
public Car(int id, string make, string model, int year, string color, int mileage, decimal price, int order = 0)
|
||||
public Car(string id, string make, string model, int year, string color, int mileage, decimal price, int order = 0)
|
||||
{
|
||||
// is ID just a number? Then it is legacy and needs a new ID string.
|
||||
int numericId = 0;
|
||||
if ((string.IsNullOrWhiteSpace(id) || int.TryParse(id, out numericId)) && id != "0")
|
||||
{
|
||||
id = Guid.NewGuid().ToString();
|
||||
if (numericId > 0)
|
||||
{
|
||||
order = numericId + order;
|
||||
}
|
||||
|
||||
}
|
||||
// Sets the properties using the setters to ensure validation is applied.
|
||||
this.Id = id;
|
||||
this.id = id;
|
||||
this.Make = make;
|
||||
this.Model = model;
|
||||
this.Year = year;
|
||||
@@ -137,7 +155,7 @@ namespace CarManagerV3
|
||||
/// <returns></returns>
|
||||
public string ToCsvString()
|
||||
{
|
||||
return $"{this.Id};{this.Make};{this.Model};{this.Year};{this.Color};{this.Mileage};{this.Price}";
|
||||
return $"{this.Id};{this.Make};{this.Model};{this.Year};{this.Color};{this.Mileage};{this.Price};{this.Order}";
|
||||
}
|
||||
|
||||
//TODO: Add error handling for malformed CSV strings and detection for missing fields.
|
||||
@@ -156,10 +174,23 @@ namespace CarManagerV3
|
||||
try
|
||||
{
|
||||
string[] parts = csv.Split(';');
|
||||
Car temp = new Car(int.Parse(parts[0]), parts[1], parts[2], int.Parse(parts[3]), parts[4], int.Parse(parts[5]), decimal.Parse(parts[6]));
|
||||
if (temp.Year < 1886 || temp.Year > DateTime.Now.Year + 1) throw new Exception($"Invalid year: {temp.Year}");
|
||||
if (temp.Mileage < 0) throw new Exception($"Mileage cannot be negative: {temp.Mileage}");
|
||||
if (temp.Price < 0) throw new Exception($"Price cannot be negative: {temp.Price}");
|
||||
// is part 7 a valid int? if not set it to 0 and log a warning.
|
||||
if (parts.Length == 7)
|
||||
{
|
||||
Console.Error.WriteLine($"Warning: CSV string has only 7 fields, expected 8. Setting Order to 0. CSV: {csv}");
|
||||
if (!StateManager.askForMigration())
|
||||
{
|
||||
throw new Exception("User declined migration. Cannot parse CSV string with missing Order field.");
|
||||
}
|
||||
Array.Resize(ref parts, 8);
|
||||
parts[7] = "0";
|
||||
}
|
||||
else if (parts.Length != 8)
|
||||
{
|
||||
throw new FormatException($"CSV string has {parts.Length} fields, expected 8. CSV: {csv}");
|
||||
}
|
||||
|
||||
Car temp = new Car(parts[0], parts[1], parts[2], int.Parse(parts[3]), parts[4], int.Parse(parts[5]), decimal.Parse(parts[6]), int.Parse(parts[7]));
|
||||
return temp;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -187,7 +218,14 @@ namespace CarManagerV3
|
||||
/// <returns>An identical but seperate <see cref="Car"/></returns>
|
||||
public Car Clone()
|
||||
{
|
||||
return new Car(this.Id, this.Make, this.Model, this.Year, this.Color, this.Mileage, this.Price);
|
||||
return new Car(this.Id, this.Make, this.Model, this.Year, this.Color, this.Mileage, this.Price, this.Order);
|
||||
}
|
||||
|
||||
|
||||
public static bool isLegacyCsvString(string csv)
|
||||
{
|
||||
string[] parts = csv.Split(';');
|
||||
return parts.Length == 7; // Legacy format has 7 fields, new format has 8 fields (with Order).
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user