Initial commit

This commit is contained in:
2026-01-23 12:09:56 +01:00
parent d17e1183f9
commit e511610052
18 changed files with 407 additions and 45 deletions

View File

@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarManagerV2
namespace CarManagerV3
{
public class Car
{

View File

@@ -1,4 +1,4 @@
namespace CarManagerV2
namespace CarManagerV3
{
partial class CarCard
{

View File

@@ -8,7 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarManagerV2
namespace CarManagerV3
{
public partial class CarCard : UserControl
{

View File

@@ -1,4 +1,4 @@
namespace CarManagerV2
namespace CarManagerV3
{
partial class CarDetailsForm
{

View File

@@ -8,7 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarManagerV2
namespace CarManagerV3
{
public partial class CarDetailsForm : Form
{

View File

@@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarManagerV2
namespace CarManagerV3
{
internal class ImageManager
{

View File

@@ -1,4 +1,4 @@
namespace CarManagerV2
namespace CarManagerV3
{
partial class MainForm
{
@@ -34,33 +34,45 @@
this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
this.tbxSearch = new System.Windows.Forms.TextBox();
this.btnNewCar = new System.Windows.Forms.Button();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.importToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.recentFilesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.revealInFileExplorerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.tableLayoutPanel1.SuspendLayout();
this.tableLayoutPanel2.SuspendLayout();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 1;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Controls.Add(this.flpCars, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.flpCars, 0, 2);
this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.menuStrip1, 0, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowCount = 3;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.Size = new System.Drawing.Size(800, 450);
this.tableLayoutPanel1.Size = new System.Drawing.Size(802, 458);
this.tableLayoutPanel1.TabIndex = 0;
this.tableLayoutPanel1.Paint += new System.Windows.Forms.PaintEventHandler(this.tableLayoutPanel1_Paint);
//
// flpCars
//
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, 43);
this.flpCars.Location = new System.Drawing.Point(3, 67);
this.flpCars.Name = "flpCars";
this.flpCars.Size = new System.Drawing.Size(794, 404);
this.flpCars.Size = new System.Drawing.Size(796, 412);
this.flpCars.TabIndex = 1;
//
// tableLayoutPanel2
@@ -71,12 +83,12 @@
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, 3);
this.tableLayoutPanel2.Location = new System.Drawing.Point(3, 27);
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.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 34F));
this.tableLayoutPanel2.Size = new System.Drawing.Size(794, 34);
this.tableLayoutPanel2.Size = new System.Drawing.Size(796, 34);
this.tableLayoutPanel2.TabIndex = 2;
//
// tbxSearch
@@ -84,13 +96,13 @@
this.tbxSearch.Dock = System.Windows.Forms.DockStyle.Fill;
this.tbxSearch.Location = new System.Drawing.Point(3, 3);
this.tbxSearch.Name = "tbxSearch";
this.tbxSearch.Size = new System.Drawing.Size(391, 22);
this.tbxSearch.Size = new System.Drawing.Size(392, 22);
this.tbxSearch.TabIndex = 3;
this.tbxSearch.TextChanged += new System.EventHandler(this.tbxSearch_TextChanged);
//
// btnNewCar
//
this.btnNewCar.Location = new System.Drawing.Point(400, 3);
this.btnNewCar.Location = new System.Drawing.Point(401, 3);
this.btnNewCar.Name = "btnNewCar";
this.btnNewCar.Size = new System.Drawing.Size(75, 23);
this.btnNewCar.TabIndex = 4;
@@ -98,19 +110,88 @@
this.btnNewCar.UseVisualStyleBackColor = true;
this.btnNewCar.Click += new System.EventHandler(this.btnNewCar_Click);
//
// menuStrip1
//
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.TabIndex = 3;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.openToolStripMenuItem,
this.saveToolStripMenuItem,
this.saveAsToolStripMenuItem,
this.importToolStripMenuItem,
this.recentFilesToolStripMenuItem,
this.revealInFileExplorerToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
this.fileToolStripMenuItem.Text = "File";
//
// openToolStripMenuItem
//
this.openToolStripMenuItem.Name = "openToolStripMenuItem";
this.openToolStripMenuItem.Size = new System.Drawing.Size(187, 22);
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.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.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.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.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.Text = "Reveal in File Explorer";
this.revealInFileExplorerToolStripMenuItem.Click += new System.EventHandler(this.revealInFileExplorerToolStripMenuItem_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.ClientSize = new System.Drawing.Size(802, 458);
this.Controls.Add(this.tableLayoutPanel1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MainMenuStrip = this.menuStrip1;
this.MinimumSize = new System.Drawing.Size(818, 497);
this.Name = "MainForm";
this.Text = "Carmanager 2";
this.Text = "Carmanager 3";
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.tableLayoutPanel2.ResumeLayout(false);
this.tableLayoutPanel2.PerformLayout();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
}
@@ -122,5 +203,13 @@
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2;
private System.Windows.Forms.TextBox tbxSearch;
private System.Windows.Forms.Button btnNewCar;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveAsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem importToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem recentFilesToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem revealInFileExplorerToolStripMenuItem;
}
}

View File

@@ -8,25 +8,36 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarManagerV2
namespace CarManagerV3
{
public partial class MainForm : Form
{
List<Car> cars = new List<Car>();
string filepath = "cars.csv";
public MainForm()
{
InitializeComponent();
SafeManager.InitializeFile("cars.csv");
List<Car> _cars = SafeManager.ReadCars("cars.csv");
List<string> recentFiles = SafeManager.GetRecentPaths();
if(recentFiles.Count > 0)
{
filepath = recentFiles[0];
}
SafeManager.InitializeFile(filepath);
List<Car> _cars = SafeManager.ReadCars(filepath);
refreshCars(_cars);
refreshRecents();
}
private async void refreshCars(List<Car> _cars, bool updateGlobal = true)
{
this.Text = "Car Manager - " + System.IO.Path.GetFileName(filepath);
// Sort by Car.Order. If equal, sort by ID
_cars = _cars.Count > 0 ? _cars.OrderBy(c => c.Order).ThenBy(c => c.Id).ToList() : _cars;
@@ -83,8 +94,8 @@ namespace CarManagerV2
Console.WriteLine("Car details form closed.");
// refresh cars
Console.WriteLine("Refreshing cars...");
List<Car> __cars = await Task.Run(() => SafeManager.ReadCars("cars.csv"));
if(tbxSearch.Text.Length > 0)
List<Car> __cars = await Task.Run(() => SafeManager.ReadCars(filepath));
if (tbxSearch.Text.Length > 0)
{
Console.WriteLine("Search box has text, applying search filter.");
cars = __cars;
@@ -92,11 +103,11 @@ namespace CarManagerV2
return;
}
refreshCars(__cars);
};
detailsForm.ShowDialog();
};
if (isNew)
{
flpCars.Controls.Add(card);
@@ -126,7 +137,7 @@ namespace CarManagerV2
{
// refresh cars
Console.WriteLine("Refreshing cars...");
List<Car> cars_ = SafeManager.ReadCars("cars.csv");
List<Car> cars_ = SafeManager.ReadCars(filepath);
refreshCars(cars_, false);
};
detailsForm.ShowDialog();
@@ -155,7 +166,7 @@ namespace CarManagerV2
{
string query = tbxSearch.Text;
await Task.Delay(100); // debounce
if(query != tbxSearch.Text) return; // text changed during delay
if (query != tbxSearch.Text) return; // text changed during delay
//flpCars.Controls.Clear();
if (string.IsNullOrWhiteSpace(query))
{
@@ -166,5 +177,207 @@ namespace CarManagerV2
searchList(query);
}
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
dlgOpen.Title = "Open Car Data File";
// Default to users documents
dlgOpen.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DialogResult result = dlgOpen.ShowDialog();
if (result == DialogResult.OK)
{
try
{
List<Car> importedCars = SafeManager.ReadCars(dlgOpen.FileName);
if(importedCars.Count == 0)
{
throw new Exception("File doesn't contain valid Cars.");
}
filepath = dlgOpen.FileName;
cars = importedCars;
StateManager.setFilePath(filepath);
// Refresh display
refreshCars(cars);
MessageBox.Show("File loaded successfully.", "Load File", MessageBoxButtons.OK, MessageBoxIcon.Information);
SafeManager.AddRecentPath(filepath);
refreshRecents();
}
catch (Exception ex)
{
MessageBox.Show("Error loading file: " + ex.Message);
}
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
refreshCars(cars);
SafeManager.SaveCars(filepath, cars);
MessageBox.Show("File saved successfully.", "Save File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog dlgSave = new SaveFileDialog();
dlgSave.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
dlgSave.Title = "Save Car Data File As";
// Default to users documents
dlgSave.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DialogResult result = dlgSave.ShowDialog();
if (result == DialogResult.OK)
{
// does file already exist?
/*if (System.IO.File.Exists(dlgSave.FileName))
{
var overwriteResult = MessageBox.Show("File already exists. Overwrite?", "Overwrite File", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (overwriteResult != DialogResult.Yes)
{
return;
}
}*/
// Windows already handles this lmao
filepath = dlgSave.FileName;
this.Text = "Car Manager - " + System.IO.Path.GetFileName(filepath);
StateManager.setFilePath(filepath);
SafeManager.SaveCars(filepath, cars);
SafeManager.AddRecentPath(filepath);
}
refreshRecents();
}
private void importToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Importing will add cars from another file to this file. This action cannot be undone. Continue?", "Import Cars", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
dlgOpen.Title = "Import Car Data File";
// Default to users documents
dlgOpen.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DialogResult dlgResult = dlgOpen.ShowDialog();
if (dlgResult == DialogResult.OK)
{
try
{
Console.WriteLine("Starting merge...");
List<Car> importedCars = SafeManager.ReadCars(dlgOpen.FileName);
if(importedCars.Count == 0)
{
throw new Exception("File doesn't contain valid Cars.");
}
// 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);
if (mergeAsNewFileResult == DialogResult.Yes)
{
SaveFileDialog dlgSave = new SaveFileDialog();
dlgSave.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
dlgSave.Title = "Save Merged Car Data File As";
// Default to users documents
dlgSave.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DialogResult saveResult = dlgSave.ShowDialog();
if (saveResult == DialogResult.OK)
{
filepath = dlgSave.FileName;
StateManager.setFilePath(filepath);
SafeManager.SaveCars(filepath, cars);
SafeManager.AddRecentPath(filepath);
refreshRecents();
}
}
else
{
// save to current file
SafeManager.SaveCars(filepath, cars);
}
// Refresh display
refreshCars(cars);
MessageBox.Show("File imported successfully.", "Import File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error importing file: " + ex.Message);
}
}
else
{
Console.WriteLine("Import cancelled.");
}
}
}
private void recentFilesToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void refreshRecents()
{
recentFilesToolStripMenuItem.DropDownItems.Clear();
List<string> recentFiles = SafeManager.GetRecentPaths();
recentFilesToolStripMenuItem.Enabled = recentFiles.Count > 0;
recentFilesToolStripMenuItem.ToolTipText = recentFiles.Count > 0 ? "" : "No recent files.";
foreach (string path in recentFiles)
{
ToolStripMenuItem item = new ToolStripMenuItem(path);
item.Click += (s, e2) =>
{
try
{
List<Car> importedCars = SafeManager.ReadCars(path);
if(importedCars.Count == 0)
{
throw new Exception("File doesn't contain valid Cars.");
}
filepath = path;
cars = importedCars;
StateManager.setFilePath(filepath);
// Refresh display
refreshCars(cars);
MessageBox.Show("File loaded successfully.", "Load File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error loading file: " + ex.Message);
}
};
recentFilesToolStripMenuItem.DropDownItems.Add(item);
}
}
private void revealInFileExplorerToolStripMenuItem_Click(object sender, EventArgs e)
{
// Open File Explorer at the location of the current filepath
if (System.IO.File.Exists(filepath))
{
System.Diagnostics.Process.Start("explorer.exe", "/select,\"" + filepath + "\"");
}
else
{
MessageBox.Show("File does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

View File

@@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>

View File

@@ -1,4 +1,4 @@
namespace CarManagerV2
namespace CarManagerV3
{
partial class PleaseWait
{

View File

@@ -8,7 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarManagerV2
namespace CarManagerV3
{
public partial class PleaseWait : Form
{

View File

@@ -4,7 +4,7 @@ using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarManagerV2
namespace CarManagerV3
{
internal static class Program
{

View File

@@ -5,11 +5,11 @@ using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CarManagerV2")]
[assembly: AssemblyTitle("CarManagerV3")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CarManagerV2")]
[assembly: AssemblyProduct("CarManagerV3")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

View File

@@ -6,10 +6,12 @@ using System.Text;
using System.Threading.Tasks;
namespace CarManagerV2
namespace CarManagerV3
{
internal class SafeManager
{
private static readonly string recentPathsFile = "recent_paths.txt";
public static void InitializeFile(string path)
{
if (!File.Exists(@path))
@@ -48,5 +50,54 @@ namespace CarManagerV2
}
}
}
public static void AddRecentPath(string path)
{
// Read the file, if the path is not already in the file, add it to the top.
// If it is already in the file, move it to the top.
// Keep only the 5 most recent paths.
List<string> paths = new List<string>();
if (File.Exists(recentPathsFile))
{
using (StreamReader reader = new StreamReader(recentPathsFile))
{
string line;
while ((line = reader.ReadLine()) != null)
{
paths.Add(line);
}
}
}
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)
{
writer.WriteLine(p);
}
}
}
public static List<string> GetRecentPaths()
{
List<string> paths = new List<string>();
if (File.Exists(recentPathsFile))
{
using (StreamReader reader = new StreamReader(recentPathsFile))
{
string line;
while ((line = reader.ReadLine()) != null)
{
paths.Add(line);
}
}
}
return paths;
}
}
}

View File

@@ -4,16 +4,17 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarManagerV2
namespace CarManagerV3
{
internal class StateManager
{
static List<Car> cars = new List<Car>();
static string filePath = "cars.csv";
public static Car GetCarById(int id)
{
cars = SafeManager.ReadCars("cars.csv");
cars = SafeManager.ReadCars(filePath);
return cars.FirstOrDefault(c => c.Id == id);
}
@@ -21,18 +22,18 @@ namespace CarManagerV2
public static void AddCar(Car car)
{
cars = SafeManager.ReadCars("cars.csv");
cars = SafeManager.ReadCars(filePath);
cars.Add(car);
SafeManager.SaveCars("cars.csv", cars);
SafeManager.SaveCars(filePath, cars);
}
public static void RemoveCar(Car car)
{
cars = SafeManager.ReadCars("cars.csv");
cars = SafeManager.ReadCars(filePath);
Car existingCar = GetCarById(car.Id);
if (existingCar == null) return;
cars.Remove(existingCar);
SafeManager.SaveCars("cars.csv", cars);
SafeManager.SaveCars(filePath, cars);
}
public static void UpdateCar(Car car)
@@ -43,17 +44,22 @@ namespace CarManagerV2
int index = cars.IndexOf(existingCar);
cars[index] = car;
Console.WriteLine("Updated car: " + existingCar.Id);
SafeManager.SaveCars("cars.csv", cars);
SafeManager.SaveCars(filePath, cars);
}
}
public static Car CreateCar(string make, string model, int year, string color, int mileage, decimal price)
{
cars = SafeManager.ReadCars("cars.csv");
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);
AddCar(newCar);
return newCar;
}
public static void setFilePath(string path)
{
filePath = path;
}
}
}