Compare commits
7 Commits
e511610052
...
1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| e431a05124 | |||
| b20e0a94dd | |||
| afa98b2681 | |||
| 6d50c28c02 | |||
| 8be81812c1 | |||
| de603b5cef | |||
| 640d9acbf6 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -361,4 +361,6 @@ MigrationBackup/
|
||||
|
||||
# Fody - auto-generated XML schema
|
||||
FodyWeavers.xsd
|
||||
IAF42_Kaulmann_CarmanagerV2.zip
|
||||
|
||||
# Submissions folder (contains submission ZIPs)
|
||||
submissions/
|
||||
@@ -1,68 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CarManagerV3
|
||||
{
|
||||
public class Car
|
||||
{
|
||||
int id;
|
||||
string make;
|
||||
string model;
|
||||
int year;
|
||||
string color;
|
||||
int mileage;
|
||||
decimal price;
|
||||
int order;
|
||||
|
||||
public Car(int id, string make, string model, int year, string color, int mileage, decimal price, int order = 0)
|
||||
{
|
||||
this.id = id;
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
this.year = year;
|
||||
this.color = color;
|
||||
this.mileage = mileage;
|
||||
this.price = price;
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public int Id { get { return id; } set { id = value; } }
|
||||
public string Make { get { return make; } set { make = value; } }
|
||||
public string Model { get { return model; } set { model = value; } }
|
||||
public int Year { get { return year; } set { year = value; } }
|
||||
public string Color { get { return color; } set { color = value; } }
|
||||
public int Mileage { get { return mileage; } set { mileage = value; } }
|
||||
public decimal Price { get { return price; } set { price = value; } }
|
||||
public int Order { get { return order; } set { order = value; } }
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{make} {model} ({year})";
|
||||
}
|
||||
|
||||
public string ToCsvString()
|
||||
{
|
||||
return $"{id};{make};{model};{year};{color};{mileage};{price}";
|
||||
}
|
||||
|
||||
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]));
|
||||
}
|
||||
|
||||
public bool IsChanged(Car other)
|
||||
{
|
||||
return make != other.make || model != other.model || year != other.year || color != other.color || mileage != other.mileage || price != other.price || other.color != color;
|
||||
}
|
||||
|
||||
public Car Clone()
|
||||
{
|
||||
return new Car(id, make, model, year, color, mileage, price);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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(filePath);
|
||||
return cars.FirstOrDefault(c => c.Id == id);
|
||||
}
|
||||
|
||||
public static List<Car> Cars { get { return cars; } set { cars = value; } }
|
||||
|
||||
public static void AddCar(Car car)
|
||||
{
|
||||
cars = SafeManager.ReadCars(filePath);
|
||||
cars.Add(car);
|
||||
SafeManager.SaveCars(filePath, cars);
|
||||
}
|
||||
|
||||
public static void RemoveCar(Car car)
|
||||
{
|
||||
cars = SafeManager.ReadCars(filePath);
|
||||
Car existingCar = GetCarById(car.Id);
|
||||
if (existingCar == null) return;
|
||||
cars.Remove(existingCar);
|
||||
SafeManager.SaveCars(filePath, cars);
|
||||
}
|
||||
|
||||
public static void UpdateCar(Car car)
|
||||
{
|
||||
Car existingCar = GetCarById(car.Id);
|
||||
if (existingCar != null)
|
||||
{
|
||||
int index = cars.IndexOf(existingCar);
|
||||
cars[index] = car;
|
||||
Console.WriteLine("Updated car: " + existingCar.Id);
|
||||
SafeManager.SaveCars(filePath, cars);
|
||||
}
|
||||
}
|
||||
|
||||
public static Car CreateCar(string make, string model, int year, string color, int mileage, decimal price)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.14.36414.22
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarManagerV3", "CarManagerV2\CarManagerV3.csproj", "{93CA258B-A645-41A8-A24F-59036ABC173F}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarManagerV3", "CarManagerV3\CarManagerV3.csproj", "{93CA258B-A645-41A8-A24F-59036ABC173F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
||||
114
CarManagerV3/Car.cs
Normal file
114
CarManagerV3/Car.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CarManagerV3
|
||||
{
|
||||
/// <summary>
|
||||
/// Class <c>Car</c> represents a car with various attributes such as make, model, year, color, mileage, and price.
|
||||
/// </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;
|
||||
|
||||
//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.
|
||||
//TODO: Add Buying Date.
|
||||
//TODO: Add sold boolean and Sold Date.
|
||||
//TODO: Add "hidden" attribute for cars that are not for sale anymore but should be kept in the database for records.
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Car"/> class.
|
||||
/// </summary>
|
||||
/// <param name="id">The unique identifier as an Integer.</param>
|
||||
/// <param name="make">The make / manufacturer of the car.</param>
|
||||
/// <param name="model">The model.</param>
|
||||
/// <param name="year">The year the car was built.</param>
|
||||
/// <param name="color">The color of 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="order">The order.</param>
|
||||
public Car(int id, string make, string model, int year, string color, int mileage, decimal price, int order = 0)
|
||||
{
|
||||
this.Id = id;
|
||||
this.Make = make;
|
||||
this.Model = model;
|
||||
this.Year = year;
|
||||
this.Color = color;
|
||||
this.Mileage = mileage;
|
||||
this.Price = price;
|
||||
this.Order = order;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Converts to string in a custom readable format.
|
||||
/// <example>
|
||||
/// For Example:
|
||||
/// Skoda Fabia (2015)
|
||||
/// </example>
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A <see cref="System.String" /> that represents this instance.
|
||||
/// </returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{this.Make} {this.Model} ({this.Year})";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts to default CSV savable string.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string ToCsvString()
|
||||
{
|
||||
return $"{this.Id};{this.Make};{this.Model};{this.Year};{this.Color};{this.Mileage};{this.Price}";
|
||||
}
|
||||
|
||||
//TODO: Add error handling for malformed CSV strings and detection for missing fields.
|
||||
//TODO: Add support for different CSV formats (e.g., different delimiters, quoted fields, etc.).
|
||||
//TODO: Add support for nil or optional fields.
|
||||
//TODO: Add detectin for invalid data / nonsensical values (e.g., negative mileage or year in the future). / Validate it actually is a car.
|
||||
/// <summary>
|
||||
/// Creates a <see cref="Car"/> instance from a CSV string in the default format.
|
||||
/// </summary>
|
||||
/// <param name="csv">The CSV Line.</param>
|
||||
/// <returns>
|
||||
/// A new <see cref="Car"/> instance.
|
||||
/// </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]));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this <see cref="Car"/> is any different from the provided <see cref="Car" />.
|
||||
/// </summary>
|
||||
/// <param name="other">The <see cref="Car"/> to check against</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if the specified other is changed; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public bool IsChanged(Car other)
|
||||
{
|
||||
return this.Make != other.Make || this.Model != other.Model || this.Year != other.Year || this.Color != other.Color || this.Mileage != other.Mileage || this.Price != other.Price || this.Color != other.Color;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clones this instance.
|
||||
/// </summary>
|
||||
/// <returns>An identical but seperate <see cref="Car"/></returns>
|
||||
public Car Clone()
|
||||
{
|
||||
return new Car(this.Id, this.Make, this.Model, this.Year, this.Color, this.Mileage, this.Price);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,27 @@
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{93CA258B-A645-41A8-A24F-59036ABC173F}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<RootNamespace>CarManagerV2</RootNamespace>
|
||||
<AssemblyName>CarManagerV2</AssemblyName>
|
||||
<RootNamespace>CarManagerV3</RootNamespace>
|
||||
<AssemblyName>CarManagerV3</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<Deterministic>true</Deterministic>
|
||||
<PublishUrl>publish\</PublishUrl>
|
||||
<Install>true</Install>
|
||||
<InstallFrom>Disk</InstallFrom>
|
||||
<UpdateEnabled>false</UpdateEnabled>
|
||||
<UpdateMode>Foreground</UpdateMode>
|
||||
<UpdateInterval>7</UpdateInterval>
|
||||
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
|
||||
<UpdatePeriodically>false</UpdatePeriodically>
|
||||
<UpdateRequired>false</UpdateRequired>
|
||||
<MapFileExtensions>true</MapFileExtensions>
|
||||
<ApplicationRevision>0</ApplicationRevision>
|
||||
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
|
||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||
<UseApplicationTrust>false</UseApplicationTrust>
|
||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
@@ -96,6 +111,7 @@
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
<DesignTime>True</DesignTime>
|
||||
</Compile>
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
@@ -110,5 +126,17 @@
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<BootstrapperPackage Include=".NETFramework,Version=v4.7.2">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>Microsoft .NET Framework 4.7.2 %28x86 and x64%29</ProductName>
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>.NET Framework 3.5 SP1</ProductName>
|
||||
<Install>false</Install>
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -7,9 +7,14 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace CarManagerV3
|
||||
{
|
||||
/// <summary>
|
||||
/// The <c>ImageManager</c> class is responsible for managing car images, including fetching and storing them locally.
|
||||
/// </summary>
|
||||
internal class ImageManager
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the image folder by creating it if it does not exist.
|
||||
/// </summary>
|
||||
public static void InitializeImageFolder()
|
||||
{
|
||||
string path = "images";
|
||||
@@ -19,6 +24,11 @@ namespace CarManagerV3
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates the image path for a given car based on its attributes.
|
||||
/// </summary>
|
||||
/// <param name="car">The car.</param>
|
||||
/// <returns>The image path for this Car.</returns>
|
||||
public static string GetImagePath(Car car)
|
||||
{
|
||||
string basePath = "images/";
|
||||
@@ -26,6 +36,11 @@ namespace CarManagerV3
|
||||
return basePath + fileName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the image for a given car, fetching it if necessary.
|
||||
/// </summary>
|
||||
/// <param name="car">The car.</param>
|
||||
/// <returns>The <see cref="Image"/> object for the car.</returns>
|
||||
public static Image GetImage(Car car)
|
||||
{
|
||||
InitializeImageFolder();
|
||||
@@ -43,11 +58,16 @@ namespace CarManagerV3
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches an image for a Car from https://cdn.imagin.studio/getimage if it does not already exist locally and saves it to images/<c>Make_Model_Year_Color.webp</>.
|
||||
/// If the image cannot be fetched, a placeholder image is used instead.
|
||||
/// Uses example customer id "hrjavascript-mastery", because they wouldn't give me one. Stole this from a tutorial page. :)
|
||||
/// </summary>
|
||||
/// <param name="car">The car.</param>
|
||||
public static void FetchImage(Car car)
|
||||
{
|
||||
// Fetch the image from https://cdn.imagin.studio/getimage and save it to images/Make_Model_Year.webp
|
||||
// use params like this: ?customer=hrjavascript-mastery&zoomType=fullscreen&make={make}&modelFamily={model}&modelYear={year}&angle=front&paintDescription={color}&fileType=png
|
||||
|
||||
// check if the image already exists
|
||||
string path = GetImagePath(car);
|
||||
if (System.IO.File.Exists(path))
|
||||
@@ -55,7 +75,7 @@ namespace CarManagerV3
|
||||
return;
|
||||
}
|
||||
string url = $"https://cdn.imagin.studio/getimage?customer=hrjavascript-mastery&zoomType=fullscreen&make={car.Make}&modelFamily={car.Model}&modelYear={car.Year}&angle=front&paintDescription={car.Color}&fileType=png";
|
||||
//add Referer header
|
||||
//add Referer header to avoid 403 error
|
||||
using (var client = new System.Net.WebClient())
|
||||
{
|
||||
client.Headers.Add("Referer", "http://localhost");
|
||||
@@ -65,7 +85,7 @@ namespace CarManagerV3
|
||||
}
|
||||
catch
|
||||
{
|
||||
// if error, use no_image_available.png
|
||||
// if error, use fallback image no_image_available.png
|
||||
System.IO.File.Copy("images/no_image_available.png", path);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ namespace CarManagerV3
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
// Open the most recent file if it exists. Otherwise, use default filepath.
|
||||
List<string> recentFiles = SafeManager.GetRecentPaths();
|
||||
if(recentFiles.Count > 0)
|
||||
{
|
||||
@@ -33,6 +34,11 @@ namespace CarManagerV3
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes the cars displayed in the flow layout panel.
|
||||
/// </summary>
|
||||
/// <param name="_cars">The cars.</param>
|
||||
/// <param name="updateGlobal">if set to <c>true</c> [update global].</param>
|
||||
private async void refreshCars(List<Car> _cars, bool updateGlobal = true)
|
||||
{
|
||||
|
||||
@@ -143,6 +149,13 @@ namespace CarManagerV3
|
||||
detailsForm.ShowDialog();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Filters the cars by a search query.
|
||||
/// </summary>
|
||||
/// <param name="query">The search query.</param>
|
||||
/// <returns>
|
||||
/// A list of <see cref="Car"/> objects that match the query.
|
||||
/// </returns>
|
||||
List<Car> filterCarsByQuery(string query)
|
||||
{
|
||||
List<Car> results = new List<Car>();
|
||||
@@ -156,6 +169,10 @@ namespace CarManagerV3
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches the list of cars by a query and refreshes the display.
|
||||
/// </summary>
|
||||
/// <param name="query">The query.</param>
|
||||
void searchList(string query)
|
||||
{
|
||||
List<Car> results = filterCarsByQuery(query);
|
||||
@@ -327,11 +344,15 @@ namespace CarManagerV3
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Unbind and remove this.
|
||||
private void recentFilesToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes the recently opened files menu.
|
||||
/// </summary>
|
||||
private void refreshRecents()
|
||||
{
|
||||
recentFilesToolStripMenuItem.DropDownItems.Clear();
|
||||
@@ -8,8 +8,8 @@
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace CarManagerV2.Properties
|
||||
{
|
||||
namespace CarManagerV3.Properties {
|
||||
using System;
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -19,32 +19,27 @@ namespace CarManagerV2.Properties
|
||||
// class via a tool like ResGen or Visual Studio.
|
||||
// To add or remove a member, edit your .ResX file then rerun ResGen
|
||||
// with the /str option, or rebuild your VS project.
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
internal class Resources
|
||||
{
|
||||
internal class Resources {
|
||||
|
||||
private static global::System.Resources.ResourceManager resourceMan;
|
||||
|
||||
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||
|
||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
internal Resources()
|
||||
{
|
||||
internal Resources() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the cached ResourceManager instance used by this class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Resources.ResourceManager ResourceManager
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((resourceMan == null))
|
||||
{
|
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CarManagerV2.Properties.Resources", typeof(Resources).Assembly);
|
||||
internal static global::System.Resources.ResourceManager ResourceManager {
|
||||
get {
|
||||
if (object.ReferenceEquals(resourceMan, null)) {
|
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CarManagerV3.Properties.Resources", typeof(Resources).Assembly);
|
||||
resourceMan = temp;
|
||||
}
|
||||
return resourceMan;
|
||||
@@ -56,14 +51,11 @@ namespace CarManagerV2.Properties
|
||||
/// resource lookups using this strongly typed resource class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Globalization.CultureInfo Culture
|
||||
{
|
||||
get
|
||||
{
|
||||
internal static global::System.Globalization.CultureInfo Culture {
|
||||
get {
|
||||
return resourceCulture;
|
||||
}
|
||||
set
|
||||
{
|
||||
set {
|
||||
resourceCulture = value;
|
||||
}
|
||||
}
|
||||
@@ -8,21 +8,17 @@
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace CarManagerV2.Properties
|
||||
{
|
||||
namespace CarManagerV3.Properties {
|
||||
|
||||
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
|
||||
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
|
||||
{
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.14.0.0")]
|
||||
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
|
||||
|
||||
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
|
||||
|
||||
public static Settings Default
|
||||
{
|
||||
get
|
||||
{
|
||||
public static Settings Default {
|
||||
get {
|
||||
return defaultInstance;
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,20 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace CarManagerV3
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles safe reading and writing of car data to files.
|
||||
/// </summary>
|
||||
internal class SafeManager
|
||||
{
|
||||
/// <summary>
|
||||
/// The path of the txt file that contains recently opened file paths.
|
||||
/// </summary>
|
||||
private static readonly string recentPathsFile = "recent_paths.txt";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a file at a specified path if it does not already exist.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
public static void InitializeFile(string path)
|
||||
{
|
||||
if (!File.Exists(@path))
|
||||
@@ -24,6 +34,13 @@ namespace CarManagerV3
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads cars from a specified file path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="List{Car}"/> containing the cars read from the file.
|
||||
/// </returns>
|
||||
public static List<Car> ReadCars(string path)
|
||||
{
|
||||
List<Car> cars = new List<Car>();
|
||||
@@ -40,6 +57,11 @@ namespace CarManagerV3
|
||||
return cars;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the cars to a specified file path.
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(@path))
|
||||
@@ -51,11 +73,15 @@ namespace CarManagerV3
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a file path to the recent paths list.
|
||||
/// If a path is already in the list, it is moved to the top.
|
||||
/// Otherwise, it is added to the top.
|
||||
/// Keeps only the 5 most recent paths.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
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))
|
||||
{
|
||||
@@ -83,6 +109,12 @@ namespace CarManagerV3
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the recently opened file paths.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A <see cref="List{String}"/> containing the recent file paths.
|
||||
/// </returns>
|
||||
public static List<string> GetRecentPaths()
|
||||
{
|
||||
List<string> paths = new List<string>();
|
||||
117
CarManagerV3/StateManager.cs
Normal file
117
CarManagerV3/StateManager.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CarManagerV3
|
||||
{
|
||||
// Could be made non-static / non-singleton if multiple collections are needed in the future. Not likely though.
|
||||
/// <summary>
|
||||
/// The <c>StateManager</c> class is responsible for managing the state of the car collection, including adding, removing, updating, and retrieving cars.
|
||||
/// Fully static / singleton at the moment, as only one collection is needed.
|
||||
/// </summary>
|
||||
internal class StateManager
|
||||
{
|
||||
// Initialize global static list of cars
|
||||
static List<Car> cars = new List<Car>();
|
||||
// Initialize default file path for car data.
|
||||
// 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";
|
||||
|
||||
/// <summary>
|
||||
/// Gets a car by its identifier.
|
||||
/// </summary>
|
||||
/// <param name="id">The identifier.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="Car"/> object if found; otherwise, null.
|
||||
/// </returns>
|
||||
public static Car GetCarById(int id)
|
||||
{
|
||||
cars = SafeManager.ReadCars(filePath);
|
||||
return cars.FirstOrDefault(c => c.Id == id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Public getter and setter for the cars list. Used to have better control over the list.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The cars.
|
||||
/// </value>
|
||||
public static List<Car> Cars { get { return cars; } set { cars = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// Adds a car to the collection.
|
||||
/// </summary>
|
||||
/// <param name="car">The car to add.</param>
|
||||
public static void AddCar(Car car)
|
||||
{
|
||||
cars = SafeManager.ReadCars(filePath);
|
||||
cars.Add(car);
|
||||
SafeManager.SaveCars(filePath, cars);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a car from the collection.
|
||||
/// </summary>
|
||||
/// <param name="car">The car to remove.</param>
|
||||
public static void RemoveCar(Car car)
|
||||
{
|
||||
cars = SafeManager.ReadCars(filePath);
|
||||
Car existingCar = GetCarById(car.Id);
|
||||
if (existingCar == null) return;
|
||||
cars.Remove(existingCar);
|
||||
SafeManager.SaveCars(filePath, cars);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a car in the collection. Identifies itself by its Id.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If the car's Id has changed during editing, this will not work correctly. Keep Id immutable!
|
||||
/// </remarks>
|
||||
/// <param name="car">The car to update.</param>
|
||||
public static void UpdateCar(Car car)
|
||||
{
|
||||
Car existingCar = GetCarById(car.Id);
|
||||
if (existingCar != null)
|
||||
{
|
||||
int index = cars.IndexOf(existingCar);
|
||||
cars[index] = car;
|
||||
Console.WriteLine("Updated car: " + existingCar.Id);
|
||||
SafeManager.SaveCars(filePath, cars);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new car and adds it to the collection.
|
||||
/// </summary>
|
||||
/// <param name="make">The make.</param>
|
||||
/// <param name="model">The model.</param>
|
||||
/// <param name="year">The year.</param>
|
||||
/// <param name="color">The color.</param>
|
||||
/// <param name="mileage">The mileage.</param>
|
||||
/// <param name="price">The price.</param>
|
||||
/// <returns>
|
||||
/// The newly created <see cref="Car"/> object.
|
||||
/// </returns>
|
||||
public static Car CreateCar(string make, string model, int year, string color, int mileage, decimal price)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the file path used for saving and loading car data.
|
||||
/// Called when user selects a new file path.
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
public static void setFilePath(string path)
|
||||
{
|
||||
filePath = path;
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user