Compare commits
2 Commits
e431a05124
...
2aeee0a009
| Author | SHA1 | Date | |
|---|---|---|---|
| 2aeee0a009 | |||
| 96d9334b56 |
@@ -85,9 +85,21 @@ namespace CarManagerV3
|
||||
/// A new <see cref="Car"/> instance.
|
||||
/// </returns>
|
||||
public static Car FromCsvString(string csv)
|
||||
{
|
||||
try
|
||||
{
|
||||
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]));
|
||||
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>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -18,10 +19,14 @@ namespace CarManagerV3
|
||||
public static void InitializeImageFolder()
|
||||
{
|
||||
string path = "images";
|
||||
|
||||
if (!System.IO.Directory.Exists(path))
|
||||
{
|
||||
System.IO.Directory.CreateDirectory(path);
|
||||
}
|
||||
|
||||
// Do not catch here. If we cannot create our images folder, the program wont work.
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -47,15 +52,18 @@ namespace CarManagerV3
|
||||
FetchImage(car);
|
||||
string path = GetImagePath(car);
|
||||
// does image exist?
|
||||
try
|
||||
{
|
||||
if (System.IO.File.Exists(path))
|
||||
{
|
||||
return Image.FromFile(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Image.FromFile("images/no_image_available.png");
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Error loading image: {ex.Message}");
|
||||
}
|
||||
return Image.FromFile("images/no_image_available.png");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace CarManagerV3
|
||||
}
|
||||
|
||||
SafeManager.InitializeFile(filepath);
|
||||
StateManager.setFilePath(filepath);
|
||||
List<Car> _cars = SafeManager.ReadCars(filepath);
|
||||
|
||||
refreshCars(_cars);
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
|
||||
namespace CarManagerV3
|
||||
@@ -23,6 +24,8 @@ namespace CarManagerV3
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
public static void InitializeFile(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(@path))
|
||||
{
|
||||
@@ -30,9 +33,15 @@ namespace CarManagerV3
|
||||
{
|
||||
// Create the file, empty
|
||||
writer.WriteLine();
|
||||
writer.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Error initializing file: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads cars from a specified file path.
|
||||
@@ -44,6 +53,9 @@ namespace CarManagerV3
|
||||
public static List<Car> ReadCars(string path)
|
||||
{
|
||||
List<Car> cars = new List<Car>();
|
||||
List<string> failedLines = new List<string>();
|
||||
try
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(@path))
|
||||
{
|
||||
string line;
|
||||
@@ -51,8 +63,28 @@ namespace CarManagerV3
|
||||
{
|
||||
// Process the line
|
||||
if (line == "") continue;
|
||||
cars.Add(Car.FromCsvString(line));
|
||||
Car tmp = Car.FromCsvString(line);
|
||||
if (tmp == null)
|
||||
{
|
||||
failedLines.Add(line);
|
||||
continue;
|
||||
}
|
||||
cars.Add(tmp);
|
||||
}
|
||||
reader.Close();
|
||||
}
|
||||
} catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Error reading cars from file: {ex.Message}");
|
||||
}
|
||||
if (failedLines.Count > 0)
|
||||
{
|
||||
Console.Error.WriteLine($"Warning: {failedLines.Count} lines could not be parsed and were skipped.");
|
||||
foreach (string failedLine in failedLines)
|
||||
{
|
||||
Console.Error.WriteLine($"Failed line: {failedLine}");
|
||||
}
|
||||
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);
|
||||
}
|
||||
return cars;
|
||||
}
|
||||
@@ -63,6 +95,8 @@ namespace CarManagerV3
|
||||
/// <param name="path">The path.</param>
|
||||
/// <param name="cars">A <see cref="List{Car}"/> containing all cars to save.</param>
|
||||
public static void SaveCars(string path, List<Car> cars)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(@path))
|
||||
{
|
||||
@@ -70,6 +104,12 @@ namespace CarManagerV3
|
||||
{
|
||||
writer.WriteLine(car.ToCsvString());
|
||||
}
|
||||
writer.Close();
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +123,8 @@ namespace CarManagerV3
|
||||
public static void AddRecentPath(string path)
|
||||
{
|
||||
List<string> paths = new List<string>();
|
||||
try
|
||||
{
|
||||
if (File.Exists(recentPathsFile))
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(recentPathsFile))
|
||||
@@ -92,6 +134,7 @@ namespace CarManagerV3
|
||||
{
|
||||
paths.Add(line);
|
||||
}
|
||||
reader.Close();
|
||||
}
|
||||
}
|
||||
paths.Remove(path);
|
||||
@@ -106,6 +149,11 @@ namespace CarManagerV3
|
||||
{
|
||||
writer.WriteLine(p);
|
||||
}
|
||||
writer.Close();
|
||||
}
|
||||
} catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Error managing recent paths: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +166,8 @@ namespace CarManagerV3
|
||||
public static List<string> GetRecentPaths()
|
||||
{
|
||||
List<string> paths = new List<string>();
|
||||
try
|
||||
{
|
||||
if (File.Exists(recentPathsFile))
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(recentPathsFile))
|
||||
@@ -127,8 +177,13 @@ namespace CarManagerV3
|
||||
{
|
||||
paths.Add(line);
|
||||
}
|
||||
reader.Close();
|
||||
}
|
||||
}
|
||||
} catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Error reading recent paths: {ex.Message}");
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user