feat: set & get validation

This commit is contained in:
2026-02-23 17:35:49 +01:00
parent 2aeee0a009
commit 160352383a
5 changed files with 165 additions and 25 deletions

View File

@@ -11,14 +11,73 @@ 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.");
Console.WriteLine($"Updating year to {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; }
//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 +97,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;