Sobes.tech
Senior

What is the main problem with the chosen solution structure? The Property class does not support polymorphism. Class fields may be uninitialized. Method of transferring total cost calculation to the 'Order' class. Does not use transactional mechanisms. No exceptions are thrown for an empty list.

sobes.tech AI

Answer from AI

The main issue is that the Property class does not support polymorphism. In the current implementation, all Property objects have the same structure and behavior, which limits extensibility and flexibility.

If in the future different types of real estate with different methods of price calculation or additional properties appear, the current structure will not allow for convenient implementation.

To improve this, Property should be made an abstract class or interface, and specific types of real estate should be implemented as subclasses. Then, the getPrice() method can be overridden for different types.

Example:

abstract class Property {
    public abstract double getPrice();
}

class Apartment extends Property {
    private double basePrice;
    private double maintenanceFee;

    public Apartment(double basePrice, double maintenanceFee) {
        this.basePrice = basePrice;
        this.maintenanceFee = maintenanceFee;
    }

    @Override
    public double getPrice() {
        return basePrice + maintenanceFee;
    }
}

class House extends Property {
    private double price;

    public House(double price) {
        this.price = price;
    }

    @Override
    public double getPrice() {
        return price;
    }
}

This will allow expanding the model without changing existing code and using polymorphism for cost calculation.