7 Commits

Author SHA1 Message Date
f6b70fa387 feat: Better car creation 2026-02-24 08:57:47 +01:00
4404ac3c7b chore: clean unused imports 2026-02-23 17:49:40 +01:00
c70407b40b chore: cleanup 2026-02-23 17:47:54 +01:00
d733e49698 feat: display car age 2026-02-23 17:47:01 +01:00
160352383a feat: set & get validation 2026-02-23 17:35:49 +01:00
2aeee0a009 fix: extra error messages 2026-02-19 08:41:43 +01:00
96d9334b56 fix: safeManager error handeling & Load fix 2026-02-19 08:25:07 +01:00
14 changed files with 433 additions and 122 deletions

15
.idea/.idea.CarManagerV3/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,15 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/contentModel.xml
/.idea.CarManagerV3.iml
/modules.xml
/projectSettingsUpdater.xml
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

6
.idea/.idea.CarManagerV3/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarManagerV3
{
@@ -11,14 +7,84 @@ namespace CarManagerV3
/// </summary>
public class Car
{
public int Id;
public string Make;
public string Model;
public int Year;
public string Color;
public int Mileage;
public decimal Price;
public int Order;
private int id;
private string make;
private string model;
private int year;
private string color;
private int mileage;
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 Make { get => make;
set
{
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Make cannot be empty.");
make = value;
}
}
public string Model { get => model;
set
{
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Model cannot be empty.");
model = value;
}
}
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.");
year = value;
}
}
public string Color { get => color;
set
{
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Color cannot be empty.");
color = value;
}
}
public int Mileage { get => mileage;
set
{
if (value < 0) throw new ArgumentException("Mileage cannot be negative.");
mileage = value;
}
}
public decimal Price { get => price;
set
{
if (value < 0) throw new ArgumentException("Price cannot be negative.");
price = value;
}
}
public int Order { get => order; set => order = value; }
public int Age { get => DateTime.Now.Year - year; }
public string AgeString
{
get
{
int age = this.Age;
if (age == 0) return "New";
else if (age == 1) return "1 year";
else if (age < 0) return "From the future!";
else return $"{age} years";
}
}
//TODO: Make Id read-only CUID. Allows for better integrity, especially when merging.
//TODO: Add buying price and automatic calculation of profit/loss with selling price suggestion.
@@ -38,6 +104,7 @@ namespace CarManagerV3
/// <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)
{
// Sets the properties using the setters to ensure validation is applied.
this.Id = id;
this.Make = make;
this.Model = model;
@@ -86,8 +153,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>

View File

@@ -43,16 +43,20 @@
this.btnSave = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.lblID = new System.Windows.Forms.Label();
this.nudYear = new System.Windows.Forms.NumericUpDown();
this.tbxColor = new System.Windows.Forms.TextBox();
this.nudMileage = new System.Windows.Forms.NumericUpDown();
this.nudPrice = new System.Windows.Forms.NumericUpDown();
this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
this.nudYear = new System.Windows.Forms.NumericUpDown();
this.lblAge = new System.Windows.Forms.Label();
this.tbxAge = new System.Windows.Forms.TextBox();
this.tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pbxCarImage)).BeginInit();
this.flowLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.nudYear)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudMileage)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudPrice)).BeginInit();
this.tableLayoutPanel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.nudYear)).BeginInit();
this.SuspendLayout();
//
// tableLayoutPanel1
@@ -62,6 +66,8 @@
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.pbxCarImage, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.label1, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.tbxMake, 1, 1);
@@ -72,10 +78,10 @@
this.tableLayoutPanel1.Controls.Add(this.label5, 0, 5);
this.tableLayoutPanel1.Controls.Add(this.label6, 0, 6);
this.tableLayoutPanel1.Controls.Add(this.flowLayoutPanel1, 0, 7);
this.tableLayoutPanel1.Controls.Add(this.nudYear, 1, 3);
this.tableLayoutPanel1.Controls.Add(this.tbxColor, 1, 4);
this.tableLayoutPanel1.Controls.Add(this.nudMileage, 1, 5);
this.tableLayoutPanel1.Controls.Add(this.nudPrice, 1, 6);
this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 1, 3);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
@@ -106,11 +112,13 @@
// label1
//
this.label1.AutoSize = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Location = new System.Drawing.Point(3, 200);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(44, 16);
this.label1.Size = new System.Drawing.Size(59, 30);
this.label1.TabIndex = 1;
this.label1.Text = "Make:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// tbxMake
//
@@ -120,15 +128,18 @@
this.tbxMake.Size = new System.Drawing.Size(473, 22);
this.tbxMake.TabIndex = 1;
this.tbxMake.TextChanged += new System.EventHandler(this.tbxMake_TextChanged);
this.tbxMake.Leave += new System.EventHandler(this.tbxMake_Leave);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Dock = System.Windows.Forms.DockStyle.Fill;
this.label2.Location = new System.Drawing.Point(3, 230);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 16);
this.label2.Size = new System.Drawing.Size(59, 30);
this.label2.TabIndex = 3;
this.label2.Text = "Model:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// tbxModel
//
@@ -138,42 +149,51 @@
this.tbxModel.Size = new System.Drawing.Size(473, 22);
this.tbxModel.TabIndex = 2;
this.tbxModel.TextChanged += new System.EventHandler(this.tbxModel_TextChanged);
this.tbxModel.Leave += new System.EventHandler(this.tbxModel_Leave);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Dock = System.Windows.Forms.DockStyle.Fill;
this.label3.Location = new System.Drawing.Point(3, 260);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(39, 16);
this.label3.Size = new System.Drawing.Size(59, 30);
this.label3.TabIndex = 5;
this.label3.Text = "Year:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Dock = System.Windows.Forms.DockStyle.Fill;
this.label4.Location = new System.Drawing.Point(3, 290);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(42, 16);
this.label4.Size = new System.Drawing.Size(59, 30);
this.label4.TabIndex = 6;
this.label4.Text = "Color:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Dock = System.Windows.Forms.DockStyle.Fill;
this.label5.Location = new System.Drawing.Point(3, 320);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(59, 16);
this.label5.Size = new System.Drawing.Size(59, 30);
this.label5.TabIndex = 7;
this.label5.Text = "Mileage:";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label6
//
this.label6.AutoSize = true;
this.label6.Dock = System.Windows.Forms.DockStyle.Fill;
this.label6.Location = new System.Drawing.Point(3, 350);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(41, 16);
this.label6.Size = new System.Drawing.Size(59, 30);
this.label6.TabIndex = 8;
this.label6.Text = "Price:";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// flowLayoutPanel1
//
@@ -218,19 +238,6 @@
this.lblID.TabIndex = 2;
this.lblID.Text = "ID";
//
// nudYear
//
this.nudYear.Location = new System.Drawing.Point(68, 263);
this.nudYear.Maximum = new decimal(new int[] {
3000,
0,
0,
0});
this.nudYear.Name = "nudYear";
this.nudYear.Size = new System.Drawing.Size(120, 22);
this.nudYear.TabIndex = 3;
this.nudYear.ValueChanged += new System.EventHandler(this.nudYear_ValueChanged);
//
// tbxColor
//
this.tbxColor.Dock = System.Windows.Forms.DockStyle.Fill;
@@ -239,6 +246,7 @@
this.tbxColor.Size = new System.Drawing.Size(473, 22);
this.tbxColor.TabIndex = 4;
this.tbxColor.TextChanged += new System.EventHandler(this.tbxColor_TextChanged);
this.tbxColor.Leave += new System.EventHandler(this.tbxColor_Leave);
//
// nudMileage
//
@@ -269,6 +277,57 @@
this.nudPrice.ThousandsSeparator = true;
this.nudPrice.ValueChanged += new System.EventHandler(this.nudPrice_ValueChanged);
//
// tableLayoutPanel2
//
this.tableLayoutPanel2.ColumnCount = 3;
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
this.tableLayoutPanel2.Controls.Add(this.nudYear, 0, 0);
this.tableLayoutPanel2.Controls.Add(this.lblAge, 1, 0);
this.tableLayoutPanel2.Controls.Add(this.tbxAge, 2, 0);
this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel2.Location = new System.Drawing.Point(68, 263);
this.tableLayoutPanel2.Name = "tableLayoutPanel2";
this.tableLayoutPanel2.RowCount = 1;
this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel2.Size = new System.Drawing.Size(473, 24);
this.tableLayoutPanel2.TabIndex = 10;
//
// nudYear
//
this.nudYear.Location = new System.Drawing.Point(3, 3);
this.nudYear.Maximum = new decimal(new int[] {
3000,
0,
0,
0});
this.nudYear.Name = "nudYear";
this.nudYear.Size = new System.Drawing.Size(120, 22);
this.nudYear.TabIndex = 3;
this.nudYear.ValueChanged += new System.EventHandler(this.nudYear_ValueChanged);
//
// lblAge
//
this.lblAge.AutoSize = true;
this.lblAge.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblAge.Location = new System.Drawing.Point(160, 0);
this.lblAge.Name = "lblAge";
this.lblAge.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.lblAge.Size = new System.Drawing.Size(151, 24);
this.lblAge.TabIndex = 4;
this.lblAge.Text = "Age";
this.lblAge.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// tbxAge
//
this.tbxAge.Enabled = false;
this.tbxAge.Location = new System.Drawing.Point(317, 3);
this.tbxAge.Name = "tbxAge";
this.tbxAge.ReadOnly = true;
this.tbxAge.Size = new System.Drawing.Size(100, 22);
this.tbxAge.TabIndex = 5;
//
// CarDetailsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
@@ -284,9 +343,11 @@
((System.ComponentModel.ISupportInitialize)(this.pbxCarImage)).EndInit();
this.flowLayoutPanel1.ResumeLayout(false);
this.flowLayoutPanel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.nudYear)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudMileage)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudPrice)).EndInit();
this.tableLayoutPanel2.ResumeLayout(false);
this.tableLayoutPanel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.nudYear)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@@ -312,6 +373,9 @@
private System.Windows.Forms.NumericUpDown nudPrice;
private System.Windows.Forms.PictureBox pbxCarImage;
private System.Windows.Forms.Label lblID;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2;
private System.Windows.Forms.Label lblAge;
private System.Windows.Forms.TextBox tbxAge;
}
}

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
@@ -13,7 +7,7 @@ namespace CarManagerV3
public partial class CarDetailsForm : Form
{
Car car;
public Car car;
public CarDetailsForm(Car car)
{
@@ -25,38 +19,100 @@ namespace CarManagerV3
tbxColor.Text = car.Color;
nudMileage.Value = car.Mileage;
nudPrice.Value = car.Price;
tbxAge.Text = car.AgeString;
pbxCarImage.Image = ImageManager.GetImage(car);
lblID.Text = $"ID: {car.Id}";
if (car.Id == 0)
{
lblID.Text = "New Car";
}
else
{
lblID.Text = $"ID: {car.Id}";
}
}
/// <summary>
/// Updates a car property safely by executing the update action and reverting it if an exception occurs.
/// </summary>
/// <param name="updateAction">The update action.</param>
/// <param name="revertAction">The action to perform when the update fails.</param>
private void SafeUpdate(Action updateAction, Action revertAction)
{
try
{
updateAction();
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error);
revertAction();
}
}
//private string ValueOrFormer(string newValue, string oldValue)
//{
// return string.IsNullOrWhiteSpace(newValue) ? oldValue : newValue;
//}
private T ValueOrFormer<T>(T newValue, T oldValue)
{
if (newValue is string str)
{
return string.IsNullOrWhiteSpace(str) ? oldValue : newValue;
}
return newValue;
}
private void tbxMake_TextChanged(object sender, EventArgs e)
{
car.Make = tbxMake.Text;
SafeUpdate(
() => car.Make = ValueOrFormer(tbxMake.Text, car.Make),
() => tbxMake.Text = car.Make
);
}
private void tbxModel_TextChanged(object sender, EventArgs e)
{
car.Model = tbxModel.Text;
SafeUpdate(
() => car.Model = ValueOrFormer(tbxModel.Text, car.Model),
() => tbxModel.Text = car.Model
);
}
private void nudYear_ValueChanged(object sender, EventArgs e)
{
car.Year = (int)nudYear.Value;
SafeUpdate(
() =>
{
car.Year = (int)ValueOrFormer(nudYear.Value, car.Year);
tbxAge.Text = car.AgeString;
},
() => nudYear.Value = car.Year
);
}
private void tbxColor_TextChanged(object sender, EventArgs e)
{
car.Color = tbxColor.Text;
SafeUpdate(
() => car.Color = ValueOrFormer(tbxColor.Text, car.Color),
() => tbxColor.Text = car.Color
);
}
private void nudMileage_ValueChanged(object sender, EventArgs e)
{
car.Mileage = (int)nudMileage.Value;
SafeUpdate(
() => car.Mileage = (int)nudMileage.Value,
() => nudMileage.Value = car.Mileage
);
}
private void nudPrice_ValueChanged(object sender, EventArgs e)
{
car.Price = nudPrice.Value;
SafeUpdate(
() => car.Price = nudPrice.Value,
() => nudPrice.Value = car.Price
);
}
private async void btnSave_Click(object sender, EventArgs e)
@@ -69,7 +125,12 @@ namespace CarManagerV3
msgbox.Show();
await Task.Run(() =>
{
StateManager.UpdateCar(car);
if(car.Id == 0) {
car = StateManager.CreateCar(car.Make, car.Model, car.Year, car.Color, car.Mileage, car.Price);
}
else {
StateManager.UpdateCar(car);
}
Console.WriteLine("Saved car: " + car.Id);
});
Console.WriteLine("Car saved. " + car.Id);
@@ -84,6 +145,12 @@ namespace CarManagerV3
private void btnDelete_Click(object sender, EventArgs e)
{
if(car.Id == 0)
{
//just close form if car is not saved yet
this.Close();
return;
}
//are you sure?
var result = MessageBox.Show("Are you sure you want to delete this car?", "Delete Car", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
@@ -113,5 +180,22 @@ namespace CarManagerV3
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void tbxMake_Leave(object sender, EventArgs e)
{
tbxMake.Text = ValueOrFormer(tbxMake.Text, car.Make);
}
private void tbxModel_Leave(object sender, EventArgs e)
{
tbxModel.Text = ValueOrFormer(tbxModel.Text, car.Model);
}
private void tbxColor_Leave(object sender, EventArgs e)
{
tbxColor.Text = ValueOrFormer(tbxColor.Text, car.Color);
}
}
}

View File

@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarManagerV3
{
@@ -18,10 +14,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 +47,26 @@ namespace CarManagerV3
FetchImage(car);
string path = GetImagePath(car);
// does image exist?
if (System.IO.File.Exists(path))
try
{
return Image.FromFile(path);
if (System.IO.File.Exists(path))
{
return Image.FromFile(path);
}
}
else
catch (Exception ex)
{
Console.Error.WriteLine($"Error loading image: {ex.Message}");
}
try
{
return Image.FromFile("images/no_image_available.png");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error loading fallback image: {ex.Message}");
return null;
}
}
/// <summary>

View File

@@ -70,7 +70,7 @@
this.flpCars.AutoScroll = true;
this.flpCars.AutoScrollMargin = new System.Drawing.Size(0, 200);
this.flpCars.Dock = System.Windows.Forms.DockStyle.Fill;
this.flpCars.Location = new System.Drawing.Point(3, 67);
this.flpCars.Location = new System.Drawing.Point(3, 71);
this.flpCars.Name = "flpCars";
this.flpCars.Size = new System.Drawing.Size(796, 412);
this.flpCars.TabIndex = 1;
@@ -83,7 +83,7 @@
this.tableLayoutPanel2.Controls.Add(this.tbxSearch, 0, 0);
this.tableLayoutPanel2.Controls.Add(this.btnNewCar, 1, 0);
this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel2.Location = new System.Drawing.Point(3, 27);
this.tableLayoutPanel2.Location = new System.Drawing.Point(3, 31);
this.tableLayoutPanel2.Name = "tableLayoutPanel2";
this.tableLayoutPanel2.RowCount = 1;
this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
@@ -112,11 +112,12 @@
//
// menuStrip1
//
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(802, 24);
this.menuStrip1.Size = new System.Drawing.Size(802, 28);
this.menuStrip1.TabIndex = 3;
this.menuStrip1.Text = "menuStrip1";
//
@@ -130,48 +131,48 @@
this.recentFilesToolStripMenuItem,
this.revealInFileExplorerToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
this.fileToolStripMenuItem.Size = new System.Drawing.Size(46, 24);
this.fileToolStripMenuItem.Text = "File";
//
// openToolStripMenuItem
//
this.openToolStripMenuItem.Name = "openToolStripMenuItem";
this.openToolStripMenuItem.Size = new System.Drawing.Size(187, 22);
this.openToolStripMenuItem.Size = new System.Drawing.Size(238, 26);
this.openToolStripMenuItem.Text = "Open";
this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(187, 22);
this.saveToolStripMenuItem.Size = new System.Drawing.Size(238, 26);
this.saveToolStripMenuItem.Text = "Save";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
//
// saveAsToolStripMenuItem
//
this.saveAsToolStripMenuItem.Name = "saveAsToolStripMenuItem";
this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(187, 22);
this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(238, 26);
this.saveAsToolStripMenuItem.Text = "Save as";
this.saveAsToolStripMenuItem.Click += new System.EventHandler(this.saveAsToolStripMenuItem_Click);
//
// importToolStripMenuItem
//
this.importToolStripMenuItem.Name = "importToolStripMenuItem";
this.importToolStripMenuItem.Size = new System.Drawing.Size(187, 22);
this.importToolStripMenuItem.Size = new System.Drawing.Size(238, 26);
this.importToolStripMenuItem.Text = "Import";
this.importToolStripMenuItem.Click += new System.EventHandler(this.importToolStripMenuItem_Click);
//
// recentFilesToolStripMenuItem
//
this.recentFilesToolStripMenuItem.Name = "recentFilesToolStripMenuItem";
this.recentFilesToolStripMenuItem.Size = new System.Drawing.Size(187, 22);
this.recentFilesToolStripMenuItem.Size = new System.Drawing.Size(238, 26);
this.recentFilesToolStripMenuItem.Text = "Recent Files";
this.recentFilesToolStripMenuItem.Click += new System.EventHandler(this.recentFilesToolStripMenuItem_Click);
//
// revealInFileExplorerToolStripMenuItem
//
this.revealInFileExplorerToolStripMenuItem.Name = "revealInFileExplorerToolStripMenuItem";
this.revealInFileExplorerToolStripMenuItem.Size = new System.Drawing.Size(187, 22);
this.revealInFileExplorerToolStripMenuItem.Size = new System.Drawing.Size(238, 26);
this.revealInFileExplorerToolStripMenuItem.Text = "Reveal in File Explorer";
this.revealInFileExplorerToolStripMenuItem.Click += new System.EventHandler(this.revealInFileExplorerToolStripMenuItem_Click);
//

View File

@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
@@ -27,6 +24,7 @@ namespace CarManagerV3
}
SafeManager.InitializeFile(filepath);
StateManager.setFilePath(filepath);
List<Car> _cars = SafeManager.ReadCars(filepath);
refreshCars(_cars);
@@ -137,7 +135,7 @@ namespace CarManagerV3
private void btnNewCar_Click(object sender, EventArgs e)
{
Car foocar = StateManager.CreateCar("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) =>
{

View File

@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarManagerV3

View File

@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarManagerV3

View File

@@ -2,8 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarManagerV3
@@ -24,14 +23,22 @@ namespace CarManagerV3
/// <param name="path">The path.</param>
public static void InitializeFile(string path)
{
if (!File.Exists(@path))
try
{
using (StreamWriter writer = new StreamWriter(@path))
if (!File.Exists(@path))
{
// Create the file, empty
writer.WriteLine();
using (StreamWriter writer = new StreamWriter(@path))
{
// Create the file, empty
writer.WriteLine();
writer.Close();
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error initializing file: {ex.Message}");
}
}
/// <summary>
@@ -44,15 +51,38 @@ namespace CarManagerV3
public static List<Car> ReadCars(string path)
{
List<Car> cars = new List<Car>();
using (StreamReader reader = new StreamReader(@path))
List<string> failedLines = new List<string>();
try
{
string line;
while ((line = reader.ReadLine()) != null)
using (StreamReader reader = new StreamReader(@path))
{
// Process the line
if (line == "") continue;
cars.Add(Car.FromCsvString(line));
string line;
while ((line = reader.ReadLine()) != null)
{
// Process the line
if (line == "") continue;
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;
}
@@ -64,12 +94,20 @@ namespace CarManagerV3
/// <param name="cars">A <see cref="List{Car}"/> containing all cars to save.</param>
public static void SaveCars(string path, List<Car> cars)
{
using (StreamWriter writer = new StreamWriter(@path))
try
{
foreach (Car car in cars)
using (StreamWriter writer = new StreamWriter(@path))
{
writer.WriteLine(car.ToCsvString());
foreach (Car car in cars)
{
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,29 +121,37 @@ namespace CarManagerV3
public static void AddRecentPath(string path)
{
List<string> paths = new List<string>();
if (File.Exists(recentPathsFile))
try
{
using (StreamReader reader = new StreamReader(recentPathsFile))
if (File.Exists(recentPathsFile))
{
string line;
while ((line = reader.ReadLine()) != null)
using (StreamReader reader = new StreamReader(recentPathsFile))
{
paths.Add(line);
string line;
while ((line = reader.ReadLine()) != null)
{
paths.Add(line);
}
reader.Close();
}
}
}
paths.Remove(path);
paths.Insert(0, path);
if (paths.Count > 5)
{
paths = paths.Take(5).ToList();
}
using (StreamWriter writer = new StreamWriter(recentPathsFile))
{
foreach (string p in paths)
paths.Remove(path);
paths.Insert(0, path);
if (paths.Count > 5)
{
writer.WriteLine(p);
paths = paths.Take(5).ToList();
}
using (StreamWriter writer = new StreamWriter(recentPathsFile))
{
foreach (string p in paths)
{
writer.WriteLine(p);
}
writer.Close();
}
} catch (Exception ex)
{
Console.Error.WriteLine($"Error managing recent paths: {ex.Message}");
}
}
@@ -118,16 +164,23 @@ namespace CarManagerV3
public static List<string> GetRecentPaths()
{
List<string> paths = new List<string>();
if (File.Exists(recentPathsFile))
try
{
using (StreamReader reader = new StreamReader(recentPathsFile))
if (File.Exists(recentPathsFile))
{
string line;
while ((line = reader.ReadLine()) != null)
using (StreamReader reader = new StreamReader(recentPathsFile))
{
paths.Add(line);
string line;
while ((line = reader.ReadLine()) != null)
{
paths.Add(line);
}
reader.Close();
}
}
} catch (Exception ex)
{
Console.Error.WriteLine($"Error reading recent paths: {ex.Message}");
}
return paths;
}

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarManagerV3
{