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

Electricity bill

Uploaded by

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

Electricity bill

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package electricitybill;

import java.util.ArrayList;

import java.util.Scanner;

class Customer {

private int customerId;

private String name;

private int unitsConsumed;

private double billAmount;

public Customer(int customerId, String name, int unitsConsumed) {

this.customerId = customerId;

this.name = name;

this.unitsConsumed = unitsConsumed;

this.billAmount = calculateBill();

private double calculateBill() {

double rate;

if (unitsConsumed <= 100) {

rate = 1.50;

} else if (unitsConsumed <= 300) {

rate = 2.00;

} else {

rate = 3.00;
}

return unitsConsumed * rate;

@Override

public String toString() {

return "Customer ID: " + customerId + ", Name: " + name +

", Units Consumed: " + unitsConsumed + ", Bill Amount: ₹" + billAmount;

public class ElectricityBill {

private static ArrayList<Customer> customers = new ArrayList<>();

private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

int choice;

do {

System.out.println("\nElectricity Bill Generation System");

System.out.println("1. Add Customer");

System.out.println("2. Display Bills");

System.out.println("3. Exit");

System.out.print("Enter your choice: ");

choice = scanner.nextInt();

scanner.nextLine(); // Consume newline

switch (choice) {

case 1 : addCustomer();break;

case 2 : displayBills();break;
case 3 : System.out.println("Exiting the system. Goodbye!");break;

default : System.out.println("Invalid choice! Please try again.");

} while (choice != 3);

private static void addCustomer() {

System.out.print("Enter Customer ID: ");

int customerId = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Enter Customer Name: ");

String name = scanner.nextLine();

System.out.print("Enter Units Consumed: ");

int units = scanner.nextInt();

Customer customer = new Customer(customerId, name, units);

customers.add(customer);

System.out.println("Customer added successfully!");

private static void displayBills() {

if (customers.isEmpty()) {

System.out.println("No customer records found.");

} else {

System.out.println("\nCustomer Bills:");

for (Customer customer : customers) {

System.out.println(customer);

}
}

You might also like