0% found this document useful (0 votes)
9 views2 pages

Electricity Bill Calculator in Java

The document is a Java program that calculates electricity bills based on units consumed during off-peak and peak hours. It applies a surcharge if the total cost exceeds Ksh 20,000 and offers two payment options: installments or full payment. The program also includes a method to calculate the cost based on tiered pricing for different ranges of unit consumption.

Uploaded by

2304760
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Electricity Bill Calculator in Java

The document is a Java program that calculates electricity bills based on units consumed during off-peak and peak hours. It applies a surcharge if the total cost exceeds Ksh 20,000 and offers two payment options: installments or full payment. The program also includes a method to calculate the cost based on tiered pricing for different ranges of unit consumption.

Uploaded by

2304760
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

Scanner;

public class ElectricityBillCalculator {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

[Link]("Enter the units consumed during off-peak hours


(6:00 PM - 6:00 AM):");
int offPeakUnits = [Link]();
[Link]("Enter the units consumed during peak hours (6:00
AM - 6:00 PM):");
int peakUnits = [Link]();

double offPeakCost = calculateCost(offPeakUnits, 15, 20, 25);


double peakCost = calculateCost(peakUnits, 20, 25, 30);
double totalCost = offPeakCost + peakCost;

if (totalCost > 20000) {


totalCost += totalCost * 0.15; // Applying 15% surcharge
}

[Link]("Total Bill: Ksh " + totalCost);

[Link]("Choose payment option: 1 - Installments, 2 -


Full Payment");
int paymentOption = [Link]();

if (paymentOption == 1) {
double installmentAmount = totalCost / 3;
[Link]("You can pay in 3 equal monthly installments of
Ksh " + installmentAmount);
} else if (paymentOption == 2) {
[Link]("Please make a full payment of Ksh " +
totalCost);
} else {
[Link]("Invalid payment option");
}
}
public static double calculateCost(int units, int cost1, int cost2,
int cost3) {
double cost = 0;
if (units <= 100) {
cost = units * cost1;
} else if (units <= 300) {
cost = 100 * cost1 + (units - 100) * cost2;
} else {
cost = 100 * cost1 + 200 * cost2 + (units - 300) * cost3;
}
return cost;
}
}

Common questions

Powered by AI

The calculateCost method applies the highest rate per unit (cost3) when the unit consumption exceeds 300 units. In the code, this is determined by segmenting the unit consumption into brackets: units 1-100 use cost1, units 101-300 use cost2, and any units above 300 use cost3, thus invoking the highest rate for the units beyond 300.

The program provides two payment options: installments and full payment. If the user chooses installments (option 1), the program calculates and informs the user about 3 equal monthly installments. If the full payment option (option 2) is chosen, the program prompts the user to make a full payment of the total cost. Any other input is considered invalid, and the user is informed of this.

The ElectricityBillCalculator uses separate methods to determine rates for off-peak and peak hours through the `calculateCost` function. By using different sets of rates depending on the time of consumption, it can dynamically adjust the cost calculations to reflect consumption in different periods with varying unit costs.

The installment payment option benefits consumers by spreading the total cost into 3 equal monthly payments instead of a lump sum, reducing the immediate financial burden. The program calculates each installment by dividing the total cost by 3, providing consumers flexibility in managing their finances over three months.

The application of a surcharge is determined by checking if the calculated total cost exceeds Ksh 20,000. If this condition is met, a 15% surcharge is applied to the total. The impact is directly proportional to the total bill amount, as the surcharge significantly increases the final payable amount, which may affect financial planning for customers with high electricity usage.

If a user enters an invalid payment option (anything other than 1 or 2), the program will output 'Invalid payment option.' Although this alerts the user to the error, it doesn't prompt for correction. This could lead to frustration and confusion, highlighting the need for enhanced input validation and potential retries or options explanations.

The program uses two different calls to the `calculateCost` method: one call for off-peak hours and another for peak hours. For off-peak hours, it uses different rate factors (15, 20, and 25) while for peak hours, it uses rate factors (20, 25, and 30). This differentiation allows the program to correctly apply different pricing structures depending on whether the consumption occurred during off-peak or peak times.

The algorithm efficiently segments unit consumption into fixed cost brackets, applying different rates based on consumption levels. Strengths include adaptability to varying consumption rates and simplicity in calculating differentiated costs. However, limitations include rigidity in pricing tiers and potential unanticipated user inputs not handling, which can affect broader flexibility.

Without a cap, the surcharge grows exponentially with increased consumption past the Ksh 20,000 threshold, potentially leading to excessively high bills that might not accurately reflect incremental consumption costs. This could result in financial strain for consumers and misalign with intended surcharging policies aimed at moderating behavior instead of punitive financial impositions.

If the total cost of the electricity bill exceeds Ksh 20,000, a 15% surcharge is added to the total bill. The surcharge is calculated by multiplying the total cost by 0.15 and adding the result back to the initial total cost.

You might also like