feat: GUID
This commit is contained in:
@@ -7,7 +7,7 @@ namespace CarManagerV3
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Car
|
public class Car
|
||||||
{
|
{
|
||||||
private int id;
|
private string id;
|
||||||
private string make;
|
private string make;
|
||||||
private string model;
|
private string model;
|
||||||
private int year;
|
private int year;
|
||||||
@@ -16,14 +16,11 @@ namespace CarManagerV3
|
|||||||
private decimal price;
|
private decimal price;
|
||||||
private int order;
|
private int order;
|
||||||
|
|
||||||
public int Id { get => id;
|
public string Id { get => id; }
|
||||||
set {
|
|
||||||
if (value < 0) throw new ArgumentException("Id cannot be negative.");
|
|
||||||
id = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Make { get => make;
|
public string Make
|
||||||
|
{
|
||||||
|
get => make;
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Make cannot be empty.");
|
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
|
set
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Model cannot be empty.");
|
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
|
set
|
||||||
{
|
{
|
||||||
if (value < 1886 || value > DateTime.Now.Year + 1) throw new ArgumentException("Year must be between 1886 and next year.");
|
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
|
set
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Color cannot be empty.");
|
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
|
set
|
||||||
{
|
{
|
||||||
if (value < 0) throw new ArgumentException("Mileage cannot be negative.");
|
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
|
set
|
||||||
{
|
{
|
||||||
if (value < 0) throw new ArgumentException("Price cannot be negative.");
|
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="mileage">The current mileage on the car.</param>
|
||||||
/// <param name="price">The selling-price of the car.</param>
|
/// <param name="price">The selling-price of the car.</param>
|
||||||
/// <param name="order">The order.</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.
|
// Sets the properties using the setters to ensure validation is applied.
|
||||||
this.Id = id;
|
this.id = id;
|
||||||
this.Make = make;
|
this.Make = make;
|
||||||
this.Model = model;
|
this.Model = model;
|
||||||
this.Year = year;
|
this.Year = year;
|
||||||
@@ -137,7 +155,7 @@ namespace CarManagerV3
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string ToCsvString()
|
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.
|
//TODO: Add error handling for malformed CSV strings and detection for missing fields.
|
||||||
@@ -156,10 +174,23 @@ namespace CarManagerV3
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
string[] parts = csv.Split(';');
|
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]));
|
// is part 7 a valid int? if not set it to 0 and log a warning.
|
||||||
if (temp.Year < 1886 || temp.Year > DateTime.Now.Year + 1) throw new Exception($"Invalid year: {temp.Year}");
|
if (parts.Length == 7)
|
||||||
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}");
|
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;
|
return temp;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -187,7 +218,14 @@ namespace CarManagerV3
|
|||||||
/// <returns>An identical but seperate <see cref="Car"/></returns>
|
/// <returns>An identical but seperate <see cref="Car"/></returns>
|
||||||
public Car Clone()
|
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).
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace CarManagerV3
|
|||||||
nudPrice.Value = car.Price;
|
nudPrice.Value = car.Price;
|
||||||
tbxAge.Text = car.AgeString;
|
tbxAge.Text = car.AgeString;
|
||||||
pbxCarImage.Image = ImageManager.GetImage(car);
|
pbxCarImage.Image = ImageManager.GetImage(car);
|
||||||
if (car.Id == 0)
|
if (car.Id == "0")
|
||||||
{
|
{
|
||||||
lblID.Text = "New Car";
|
lblID.Text = "New Car";
|
||||||
}
|
}
|
||||||
@@ -125,7 +125,7 @@ namespace CarManagerV3
|
|||||||
msgbox.Show();
|
msgbox.Show();
|
||||||
await Task.Run(() =>
|
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);
|
car = StateManager.CreateCar(car.Make, car.Model, car.Year, car.Color, car.Mileage, car.Price);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -145,7 +145,7 @@ namespace CarManagerV3
|
|||||||
|
|
||||||
private void btnDelete_Click(object sender, EventArgs e)
|
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
|
//just close form if car is not saved yet
|
||||||
this.Close();
|
this.Close();
|
||||||
|
|||||||
@@ -68,6 +68,7 @@
|
|||||||
<Compile Include="CarCard.Designer.cs">
|
<Compile Include="CarCard.Designer.cs">
|
||||||
<DependentUpon>CarCard.cs</DependentUpon>
|
<DependentUpon>CarCard.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Class1.cs" />
|
||||||
<Compile Include="MainForm.cs">
|
<Compile Include="MainForm.cs">
|
||||||
<SubType>Form</SubType>
|
<SubType>Form</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
12
CarManagerV3/Class1.cs
Normal file
12
CarManagerV3/Class1.cs
Normal 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
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,13 +25,26 @@ namespace CarManagerV3
|
|||||||
|
|
||||||
SafeManager.InitializeFile(filepath);
|
SafeManager.InitializeFile(filepath);
|
||||||
StateManager.setFilePath(filepath);
|
StateManager.setFilePath(filepath);
|
||||||
List<Car> _cars = SafeManager.ReadCars(filepath);
|
try
|
||||||
|
{
|
||||||
refreshCars(_cars);
|
List<Car> _cars = SafeManager.ReadCars(filepath);
|
||||||
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>
|
/// <summary>
|
||||||
/// Refreshes the cars displayed in the flow layout panel.
|
/// Refreshes the cars displayed in the flow layout panel.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -135,7 +148,7 @@ namespace CarManagerV3
|
|||||||
|
|
||||||
private void btnNewCar_Click(object sender, EventArgs e)
|
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);
|
CarDetailsForm detailsForm = new CarDetailsForm(foocar);
|
||||||
detailsForm.FormClosed += (s2, e2) =>
|
detailsForm.FormClosed += (s2, e2) =>
|
||||||
{
|
{
|
||||||
@@ -226,6 +239,10 @@ namespace CarManagerV3
|
|||||||
SafeManager.AddRecentPath(filepath);
|
SafeManager.AddRecentPath(filepath);
|
||||||
refreshRecents();
|
refreshRecents();
|
||||||
}
|
}
|
||||||
|
catch (LegacyException)
|
||||||
|
{
|
||||||
|
showOpenFileDialog();
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Error loading file: " + ex.Message);
|
MessageBox.Show("Error loading file: " + ex.Message);
|
||||||
@@ -294,13 +311,6 @@ namespace CarManagerV3
|
|||||||
// merge cars
|
// merge cars
|
||||||
foreach (Car car in importedCars)
|
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);
|
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);
|
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);
|
refreshCars(cars);
|
||||||
MessageBox.Show("File imported successfully.", "Import File", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Error importing file: " + ex.Message);
|
MessageBox.Show("Error importing file: " + ex.Message);
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ namespace CarManagerV3
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly string recentPathsFile = "recent_paths.txt";
|
private static readonly string recentPathsFile = "recent_paths.txt";
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a file at a specified path if it does not already exist.
|
/// Initializes a file at a specified path if it does not already exist.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -52,6 +53,7 @@ namespace CarManagerV3
|
|||||||
{
|
{
|
||||||
List<Car> cars = new List<Car>();
|
List<Car> cars = new List<Car>();
|
||||||
List<string> failedLines = new List<string>();
|
List<string> failedLines = new List<string>();
|
||||||
|
bool isLegacy = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (StreamReader reader = new StreamReader(@path))
|
using (StreamReader reader = new StreamReader(@path))
|
||||||
@@ -61,6 +63,19 @@ namespace CarManagerV3
|
|||||||
{
|
{
|
||||||
// Process the line
|
// Process the line
|
||||||
if (line == "") continue;
|
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);
|
Car tmp = Car.FromCsvString(line);
|
||||||
if (tmp == null)
|
if (tmp == null)
|
||||||
{
|
{
|
||||||
@@ -71,7 +86,12 @@ namespace CarManagerV3
|
|||||||
}
|
}
|
||||||
reader.Close();
|
reader.Close();
|
||||||
}
|
}
|
||||||
} catch (Exception ex)
|
}
|
||||||
|
catch (LegacyException ex)
|
||||||
|
{
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"Error reading cars from file: {ex.Message}");
|
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);
|
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;
|
return cars;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +129,8 @@ namespace CarManagerV3
|
|||||||
}
|
}
|
||||||
writer.Close();
|
writer.Close();
|
||||||
}
|
}
|
||||||
} catch (Exception ex)
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"Error saving cars to file: {ex.Message}");
|
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);
|
MessageBox.Show($"Error saving cars to file: {ex.Message}", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
@@ -149,7 +175,8 @@ namespace CarManagerV3
|
|||||||
}
|
}
|
||||||
writer.Close();
|
writer.Close();
|
||||||
}
|
}
|
||||||
} catch (Exception ex)
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"Error managing recent paths: {ex.Message}");
|
Console.Error.WriteLine($"Error managing recent paths: {ex.Message}");
|
||||||
}
|
}
|
||||||
@@ -178,11 +205,14 @@ namespace CarManagerV3
|
|||||||
reader.Close();
|
reader.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception ex)
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"Error reading recent paths: {ex.Message}");
|
Console.Error.WriteLine($"Error reading recent paths: {ex.Message}");
|
||||||
}
|
}
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
namespace CarManagerV3
|
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.
|
// 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 string filePath = "cars.csv";
|
||||||
|
|
||||||
|
static bool hasConfirmedMigration = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a car by its identifier.
|
/// Gets a car by its identifier.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -24,7 +29,7 @@ namespace CarManagerV3
|
|||||||
/// <returns>
|
/// <returns>
|
||||||
/// A <see cref="Car"/> object if found; otherwise, null.
|
/// A <see cref="Car"/> object if found; otherwise, null.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public static Car GetCarById(int id)
|
public static Car GetCarById(string id)
|
||||||
{
|
{
|
||||||
cars = SafeManager.ReadCars(filePath);
|
cars = SafeManager.ReadCars(filePath);
|
||||||
return cars.FirstOrDefault(c => c.Id == id);
|
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)
|
public static Car CreateCar(string make, string model, int year, string color, int mileage, decimal price)
|
||||||
{
|
{
|
||||||
cars = SafeManager.ReadCars(filePath);
|
cars = SafeManager.ReadCars(filePath);
|
||||||
int newId = cars.Count > 0 ? cars.Max(c => c.Id) + 1 : 1;
|
int newOrder = cars.Count > 0 ? cars.Max(c => c.Order) + 1 : 1;
|
||||||
Car newCar = new Car(newId, make, model, year, color, mileage, price);
|
Car newCar = new Car("", make, model, year, color, mileage, price, newOrder);
|
||||||
AddCar(newCar);
|
AddCar(newCar);
|
||||||
return newCar;
|
return newCar;
|
||||||
}
|
}
|
||||||
@@ -109,7 +114,21 @@ namespace CarManagerV3
|
|||||||
/// <param name="path">The path.</param>
|
/// <param name="path">The path.</param>
|
||||||
public static void setFilePath(string path)
|
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;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user