feature/cuid #1

Merged
frozd merged 5 commits from feature/cuid into master 2026-03-03 11:33:29 +01:00
7 changed files with 158 additions and 44 deletions
Showing only changes of commit 48be020dc4 - Show all commits

View File

@@ -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).
}
}
}

View File

@@ -21,7 +21,7 @@ namespace CarManagerV3
nudPrice.Value = car.Price;
tbxAge.Text = car.AgeString;
pbxCarImage.Image = ImageManager.GetImage(car);
if (car.Id == 0)
if (car.Id == "0")
{
lblID.Text = "New Car";
}
@@ -125,7 +125,7 @@ namespace CarManagerV3
msgbox.Show();
await Task.Run(() =>
{
if(car.Id == 0) {
if(car.Id == "0") {
car = StateManager.CreateCar(car.Make, car.Model, car.Year, car.Color, car.Mileage, car.Price);
}
else {
@@ -145,7 +145,7 @@ namespace CarManagerV3
private void btnDelete_Click(object sender, EventArgs e)
{
if(car.Id == 0)
if(car.Id == "0")
{
//just close form if car is not saved yet
this.Close();

View File

@@ -68,6 +68,7 @@
<Compile Include="CarCard.Designer.cs">
<DependentUpon>CarCard.cs</DependentUpon>
</Compile>
<Compile Include="Class1.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>

12
CarManagerV3/Class1.cs Normal file
View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarManagerV3
{
internal class LegacyException : Exception
{
}
}

View File

@@ -25,13 +25,26 @@ namespace CarManagerV3
SafeManager.InitializeFile(filepath);
StateManager.setFilePath(filepath);
try
{
List<Car> _cars = SafeManager.ReadCars(filepath);
refreshCars(_cars);
refreshRecents();
cars = _cars;
refreshCars(_cars, false);
}
catch (LegacyException)
{
Console.Error.WriteLine("Legacy file format detected. Prompting user to select a new file.");
showOpenFileDialog();
}
}
public void showOpenFileDialog()
{
openToolStripMenuItem.PerformClick();
}
/// <summary>
/// Refreshes the cars displayed in the flow layout panel.
/// </summary>
@@ -135,7 +148,7 @@ namespace CarManagerV3
private void btnNewCar_Click(object sender, EventArgs e)
{
Car foocar = new Car(0, "New", "Car", 2020, "White", 0, 20000);
Car foocar = new Car("0", "New", "Car", 2020, "White", 0, 20000);
CarDetailsForm detailsForm = new CarDetailsForm(foocar);
detailsForm.FormClosed += (s2, e2) =>
{
@@ -226,6 +239,10 @@ namespace CarManagerV3
SafeManager.AddRecentPath(filepath);
refreshRecents();
}
catch (LegacyException)
{
showOpenFileDialog();
}
catch (Exception ex)
{
MessageBox.Show("Error loading file: " + ex.Message);
@@ -294,13 +311,6 @@ namespace CarManagerV3
// merge cars
foreach (Car car in importedCars)
{
// check if car with same ID exists
if (cars.Any(c => c.Id == car.Id))
{
// assign new ID
int newId = cars.Count > 0 ? cars.Max(c => c.Id) + 1 : 1;
car.Id = newId;
}
cars.Add(car);
}
DialogResult mergeAsNewFileResult = MessageBox.Show("Do you want to save the merged cars as a new file?", "Save As New File", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
@@ -330,6 +340,10 @@ namespace CarManagerV3
refreshCars(cars);
MessageBox.Show("File imported successfully.", "Import File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (LegacyException)
{
MessageBox.Show("The file you are trying to import is in a legacy format that is no longer supported. Please convert the file to the new format and try again.", "Import Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show("Error importing file: " + ex.Message);

View File

@@ -17,6 +17,7 @@ namespace CarManagerV3
/// </summary>
private static readonly string recentPathsFile = "recent_paths.txt";
/// <summary>
/// Initializes a file at a specified path if it does not already exist.
/// </summary>
@@ -52,6 +53,7 @@ namespace CarManagerV3
{
List<Car> cars = new List<Car>();
List<string> failedLines = new List<string>();
bool isLegacy = false;
try
{
using (StreamReader reader = new StreamReader(@path))
@@ -61,6 +63,19 @@ namespace CarManagerV3
{
// Process the line
if (line == "") continue;
if (Car.isLegacyCsvString(line))
{
if (!StateManager.askForMigration())
{
MessageBox.Show("The file you are trying to open is in an old format that is no longer supported. Please select a different file.", "Unsupported Format", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new LegacyException();
//Environment.Exit(0);
}
else
{
isLegacy = true;
}
}
Car tmp = Car.FromCsvString(line);
if (tmp == null)
{
@@ -71,7 +86,12 @@ namespace CarManagerV3
}
reader.Close();
}
} catch (Exception ex)
}
catch (LegacyException ex)
{
throw ex;
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error reading cars from file: {ex.Message}");
}
@@ -84,6 +104,11 @@ namespace CarManagerV3
}
MessageBox.Show($"Warning: {failedLines.Count} lines in the file could not be parsed and were skipped. Check the console for details.", "Parsing Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
cars = cars.OrderBy(c => c.Order).ToList();
if(isLegacy)
{
SafeManager.SaveCars(path, cars);
}
return cars;
}
@@ -104,7 +129,8 @@ namespace CarManagerV3
}
writer.Close();
}
} catch (Exception ex)
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error saving cars to file: {ex.Message}");
MessageBox.Show($"Error saving cars to file: {ex.Message}", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
@@ -149,7 +175,8 @@ namespace CarManagerV3
}
writer.Close();
}
} catch (Exception ex)
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error managing recent paths: {ex.Message}");
}
@@ -178,11 +205,14 @@ namespace CarManagerV3
reader.Close();
}
}
} catch (Exception ex)
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error reading recent paths: {ex.Message}");
}
return paths;
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace CarManagerV3
{
@@ -17,6 +18,10 @@ namespace CarManagerV3
// TODO: If no recent file paths are found, prompt user to select a file path instead of using a hardcoded default in the program folder.
static string filePath = "cars.csv";
static bool hasConfirmedMigration = false;
/// <summary>
/// Gets a car by its identifier.
/// </summary>
@@ -24,7 +29,7 @@ namespace CarManagerV3
/// <returns>
/// A <see cref="Car"/> object if found; otherwise, null.
/// </returns>
public static Car GetCarById(int id)
public static Car GetCarById(string id)
{
cars = SafeManager.ReadCars(filePath);
return cars.FirstOrDefault(c => c.Id == id);
@@ -96,8 +101,8 @@ namespace CarManagerV3
public static Car CreateCar(string make, string model, int year, string color, int mileage, decimal price)
{
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);
int newOrder = cars.Count > 0 ? cars.Max(c => c.Order) + 1 : 1;
Car newCar = new Car("", make, model, year, color, mileage, price, newOrder);
AddCar(newCar);
return newCar;
}
@@ -109,7 +114,21 @@ namespace CarManagerV3
/// <param name="path">The path.</param>
public static void setFilePath(string path)
{
// Reset migration confirmation when changing file path, as the new file may also require migration.
hasConfirmedMigration = false;
filePath = path;
}
public static bool askForMigration()
{
if (hasConfirmedMigration)
{
return true;
}
DialogResult result = MessageBox.Show("The file you are trying to open is in an older format. Do you want to attempt to migrate it to the new format? If you choose not to migrate, the file will not be opened.", "Migration Needed", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
hasConfirmedMigration = result == DialogResult.Yes;
return hasConfirmedMigration;
}
}
}