Instructions
Requirements and Specifications
Source Code
import java.util.Scanner;
public class Main {
private static final double KWH_COST = 0.050445;
private static final double LATE_FEE_RATE = 0.15;
private static final int NUM_ASTERISKS = 45;
private static Scanner in = new Scanner(System.in);
// Read an account number from user input
private static String readAccountNumber() {
System.out.print("Please enter your account number: ");
return in.nextLine();
}
// Read a value to check if bill being paid is late
private static boolean readBillIfLate() {
System.out.print("Is your bill late (enter true or false)? ");
return in.nextLine().equalsIgnoreCase("true");
}
// Return the total kwh used for all properties
private static int readTotalKwhUsed() {
System.out.print("Please enter the number of properties that you own: ");
int numProperties = Integer.parseInt(in.nextLine());
// Ask for the kwh usage for each property
int totalKwh = 0;
for (int i = 0; i < numProperties; i++) {
System.out.print("Please enter the number of kWh used at property #" + (i + 1) + ": ");
totalKwh += Integer.parseInt(in.nextLine());
}
return totalKwh;
}
// calculate the total amount due based on the kwh used
private static double calculateAmountDue(int totalKwhUsed) {
return totalKwhUsed * KWH_COST;
}
// Print the billing information
private static void printBillingInformation(String accountNumber,
int totalKwhUsed, boolean isLate) {
double amountDue = calculateAmountDue(totalKwhUsed);
System.out.println();
System.out.println("Account Number: " + accountNumber);
System.out.println("Total Number of kWh Used = " + totalKwhUsed);
System.out.println("Amount Due = $" + String.format("%.2f", amountDue));
if (isLate) {
// Apply 15% fee for late
double lateFee = LATE_FEE_RATE * amountDue;
System.out.println("Late Fee = $" + String.format("%.2f", lateFee));
System.out.println("Total Due = $" + String.format("%.2f", amountDue + lateFee));
}
printDividerLine();
printDividerLine();
}
// Prints out 45 asterisks
private static void printDividerLine() {
for(int i = 0; i < NUM_ASTERISKS; i++) {
System.out.print("*");
}
System.out.println();
}
// Entry point of the program
public static void main(String[] args) {
// Print an intro
System.out.println("Welcome to the Electricity Billing Calculator");
System.out.println();
// Ask for the account number
String accountNumber = readAccountNumber();
// Ask if bill is late
boolean isLate = readBillIfLate();
// Read the total kwh used
int totalKwhUsed = readTotalKwhUsed();
// Print the billing information
printBillingInformation(accountNumber, totalKwhUsed, isLate);
}
}