Write a Class FeeCalculator that’s programed against FeeStructure (or uses FeeStructure Interface) having the following members
Fields:Â feeStructure
Constructor (using dependency injection -> constructor injection)
Methods:
interface FeeStructure {
double calculateFee(int creditHour, String type);
double calculateFee(int creditHour, String type, int discount);
}
class FeeCalculator implements FeeStructure {
private FeeStructure feeStructure;
public FeeCalculator(FeeStructure feeStructure) {
setFeeStructure(feeStructure);
}
/**
* PKR 4,100 per creditHour when type is set to "theory" PKR 12,250 per
* creditHour when type is set to "lab"
*/
@Override
public double calculateFee(int creditHour, String type) {
if (type.compareToIgnoreCase("theory") == 0) {
return 4100 * creditHour;
}
if (type.compareToIgnoreCase("lab") == 0) {
return 12250 * creditHour;
}
return 0;
}
@Override
public double calculateFee(int creditHour, String type, int discount) {
double fee = calculateFee(creditHour, type);
return fee - ((fee * (double) discount) / 100.0);
}
/**
* @return the feeStructure
*/
public FeeStructure getFeeStructure() {
return feeStructure;
}
/**
* @param feeStructure the feeStructure to set
*/
public void setFeeStructure(FeeStructure feeStructure) {
this.feeStructure = feeStructure;
}
}
class App {
public static void main(String[] args) {
FeeStructure f = null;
FeeCalculator feeCalculator = new FeeCalculator(f);
System.out.println("FeeCalculator: " + feeCalculator.calculateFee(10, "theory"));
System.out.println("FeeCalculator discount: " + feeCalculator.calculateFee(25, "lab", 10));
}
}
Comments
Leave a comment