fix: extra error messages
This commit is contained in:
@@ -86,8 +86,20 @@ namespace CarManagerV3
|
||||
/// </returns>
|
||||
public static Car FromCsvString(string csv)
|
||||
{
|
||||
string[] parts = csv.Split(';');
|
||||
return new Car(int.Parse(parts[0]), parts[1], parts[2], int.Parse(parts[3]), parts[4], int.Parse(parts[5]), decimal.Parse(parts[6]));
|
||||
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}");
|
||||
return temp;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Error parsing CSV Car string: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user