0% found this document useful (0 votes)
2 views

Java Assignment1

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Assignment1

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

public class ElectricityBillCalculator {


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

System.out.println("Enter the units consumed during off-peak hours


(6:00 PM - 6:00 AM):");
int offPeakUnits = input.nextInt();
System.out.println("Enter the units consumed during peak hours (6:00
AM - 6:00 PM):");
int peakUnits = input.nextInt();

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
}

System.out.println("Total Bill: Ksh " + totalCost);

System.out.println("Choose payment option: 1 - Installments, 2 -


Full Payment");
int paymentOption = input.nextInt();

if (paymentOption == 1) {
double installmentAmount = totalCost / 3;
System.out.println("You can pay in 3 equal monthly installments of
Ksh " + installmentAmount);
} else if (paymentOption == 2) {
System.out.println("Please make a full payment of Ksh " +
totalCost);
} else {
System.out.println("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;
}
}

You might also like