I am refactoring a car rental system code. The rental class keeps record of each rental duration, mileage, and car type. It has a method to compute final cost based on these attributes.
public class Rental {
private Vehicle vehicle;
private int kilometersRented;
private int daysRented;
private boolean lateFee;
private double setupFee;
public double ComputeCost() {
...
}
}
There are 3 (possibly more) car types with different daily prices. Checking each car type in the ComputeCost method is obviously not the best solution. However, polymorphism doesn't seem the appropriate solution either, since different car types don't implement extra functionality; they just have different rates. Moving the ComputeCost method to each of these subclasses doesn't also seem appropriate, since the method depends more on rental attributes like mileage and duration than on car type.
Aucun commentaire:
Enregistrer un commentaire