Untitled

mail@pastecode.io avatar
unknown
csharp
20 days ago
3.3 kB
6
Indexable
Never
```
// Car Entity
public class Car
{
    public Guid CarId { get; private set; }
    public string Model { get; private set; }
    public string Manufacturer { get; private set; }

    public Car(Guid carId, string model, string manufacturer)
    {
        CarId = carId;
        Model = model;
        Manufacturer = manufacturer;
    }

    public override string ToString()
    {
        return $"CarId: {CarId}, Model: {Model}, Manufacturer: {Manufacturer}";
    }
}

// Car Validator
public class CarValidator : AbstractValidator<Car>
{
    public CarValidator()
    {
        RuleFor(car => car.Model)
            .NotEmpty().WithMessage("Car model cannot be empty.")
            .Length(2, 50).WithMessage("Car model must be between 2 and 50 characters.");

        RuleFor(car => car.Manufacturer)
            .NotEmpty().WithMessage("Car manufacturer cannot be empty.")
            .Length(2, 50).WithMessage("Car manufacturer must be between 2 and 50 characters.");
    }
}

// Person Entity
public class Person
{
    public Guid PersonId { get; private set; }
    public string Name { get; private set; }
    public int Age { get; private set; }
    private List<Car> _cars;

    public IReadOnlyCollection<Car> Cars => _cars.AsReadOnly();

    public Person(Guid personId, string name, int age)
    {
        PersonId = personId;
        Name = name;
        Age = age;
        _cars = new List<Car>();
    }

    public void AddCar(Car car)
    {
        var validator = new CarValidator();
        var result = validator.Validate(car);

        if (!result.IsValid)
        {
            throw new ValidationException(result.Errors);
        }

        // Check Ferrari limit
        int ferrariCount = _cars.Count(c => c.Manufacturer.Equals("Ferrari", StringComparison.OrdinalIgnoreCase));
        if (car.Manufacturer.Equals("Ferrari", StringComparison.OrdinalIgnoreCase) && ferrariCount >= 2)
        {
            throw new InvalidOperationException("You cannot own more than two Ferraris.");
        }

        // Check Porsche limit
        int porscheCount = _cars.Count(c => c.Manufacturer.Equals("Porsche", StringComparison.OrdinalIgnoreCase));
        if (car.Manufacturer.Equals("Porsche", StringComparison.OrdinalIgnoreCase) && porscheCount >= 3)
        {
            throw new InvalidOperationException("You cannot own more than three Porsches.");
        }

        _cars.Add(car);
    }

    public void RemoveCar(Guid carId)
    {
        var car = _cars.FirstOrDefault(c => c.CarId == carId);
        if (car != null)
        {
            _cars.Remove(car);
        }
    }

    public override string ToString()
    {
        return $"PersonId: {PersonId}, Name: {Name}, Age: {Age}, Cars Owned: {Cars.Count}";
    }
}

// Person Validator
public class PersonValidator : AbstractValidator<Person>
{
    public PersonValidator()
    {
        RuleFor(person => person.Name)
            .NotEmpty().WithMessage("Name cannot be empty.")
            .Length(2, 50).WithMessage("Name must be between 2 and 50 characters.");

        RuleFor(person => person.Age)
            .InclusiveBetween(18, 100).WithMessage("Age must be between 18 and 100.");
    }
}
```
Leave a Comment