Your challenge is to write a program to simulate a parking garage fee collection
ID: 3625639 • Letter: Y
Question
Your challenge is to write a program to simulate a parking garage fee collection system using varying fee calculation algorithms. Every parking garage business has a cash register that calculates the fee for each car that parks there. But the actual formula used for this calculation will vary with the garage business. We need to be able to plug in different fee calculation components into the cash register without the need to modify the cash register source code.
Here are the fee calculation components you must support. You should be able to switch from one to the other without breaking code, and you should assume other components like this will be needed in the future.
BestValue Fee Calculator
This fee is based on a $2.00 minimum fee to park for up to three hours. After that there is an additional $0.50 per hour charge for each hour or part of an hour parked. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours.
Thrifty Fee Calculator
This fee is based on a $1.50 minimum fee to park for up to two hours. After that there is an additional $0.75 per hour for each hour or part of an hour parked. There is no maximum charge. Assume that no car parks for longer than 24 hours.
Input and Output
The INPUT will be the hours parked for a single car. You must enter this input using the Scanner object or the JOptionPane object (your choice). Once the input is provided, the cash register should automatically calculate the fee to be paid by that car using the provided calculator component. Remember, you should be able to switch calculators at any time without changing the cash register code. The OUTPUT will be the hours parked and fee for that car, plus the running totals for the garage while the program is running (total hours parked and total fees collected). You may display this output at the console or in a JOptionPane (your choice).
Don’t over-engineer your solution. You goal is to demonstrate the principles and practices learned to date – especially Encapsulation and the DIP.
Explanation / Answer
CashRegister.java
import java.text.NumberFormat;
import java.util.Scanner;
public class CashRegister {
FeeCalculator fc;
public CashRegister(FeeCalculator fc) {
this.fc = fc;
}
public void printCharge() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of hours parked: ");
double hours = in.nextDouble();
in.nextLine();
NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println("Charge: "+fmt.format(fc.calculateFee(hours)));
}
public static void main(String[] args) {
System.out.println("Garage A, using BestValueFeeCalculator.");
CashRegister a = new CashRegister(new BestValueFeeCalculator());
a.printCharge();
a.printCharge();
a.printCharge();
System.out.println("Garage B, using ThriftyFeeCalculator.");
CashRegister b = new CashRegister(new ThriftyFeeCalculator());
b.printCharge();
b.printCharge();
b.printCharge();
}
}
--------------
ThrifyFeeCalculator.java
public class ThriftyFeeCalculator implements FeeCalculator {
public double calculateFee(double hours) {
return Math.max(1.5, .75*Math.ceil(hours));
}
}
--------------------
BestValueFeeCalculator.java
public class BestValueFeeCalculator implements FeeCalculator {
public double calculateFee(double hours) {
return Math.min(2.0+.5*Math.max(0,Math.ceil(hours)-3),10.0);
}
}
-------------------
FeeCalculator.java
public interface FeeCalculator {
public double calculateFee(double hours);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.