POO-ITBA / 2024_01

Consultas 1C 2024
0 stars 0 forks source link

Parcial 2021 1C - Ejercicio 3 #29

Closed lmoliveto closed 4 months ago

lmoliveto commented 4 months ago

Hola! Estaria mal hacer un getter de building en la clase abstracta para despues acceder a getResidentialRoomExpense() y getCommercialMult() desde las hijas en lugar de hacer getters en la clase abstracta de esos mismos datos?


public abstract class FunctionalUnit{
    private final String name;
    private final int squareMeters;
    private final Building building;

    public FunctionalUnit(String name, int squareMeters, Building building){
        this.name = name;
        this.squareMeters = squareMeters;
        this.building = building;
    }

    protected double calculateExpense(){
        return squareMeters * building.getM2Expense();
    }

    protected Building getBuilding(){
        return building;
    }

    @Override
    public String toString(){
        return "U.F %s ( %d m2 )".formatted(name, squareMeters);
    }
}

public class ResidentialFunctionalUnit extends FunctionalUnit{
    private final int rooms;

    public ResidentialFunctionalUnit(String name, int squareMeters, int rooms, Building building){
        super(name, squareMeters, building);
        this.rooms = rooms;
    }

    @Override
    protected double calculateExpense(){
        return super.calculateExpense() + rooms * getBuilding().getResidentialRoomExpense();
    }

    @Override
    public String toString(){
        return "Residencial: %s : $%.2f".formatted(super.toString(), calculateExpense());
    }
}

public class CommercialFunctionalUnit extends FunctionalUnit{

    public CommercialFunctionalUnit(String name, int squareMeters, Building building){
        super(name, squareMeters, building);
    }

    @Override
    protected double calculateExpense(){
        return super.calculateExpense() * getBuilding().getCommercialMult();
    }

    @Override
    public String toString(){
        return "Comercial: %s : $%.2f".formatted(super.toString(), calculateExpense());
    }
}
fmeola commented 4 months ago

Hola @lmoliveto No, está perfecto así como lo implementaste. Y está muy bien que sea protected.