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

JAVA_exp2_ (2)

The document outlines an experiment for developing an Electricity Bill Calculator using Java, where customer records are read and processed to calculate bills based on units consumed and rate per unit. It details the structure of the CustomerBill class, including methods for calculating and printing the bill, as well as input and output formats. Sample test cases are provided to illustrate the expected functionality and formatting of the output.

Uploaded by

Rohit Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

JAVA_exp2_ (2)

The document outlines an experiment for developing an Electricity Bill Calculator using Java, where customer records are read and processed to calculate bills based on units consumed and rate per unit. It details the structure of the CustomerBill class, including methods for calculating and printing the bill, as well as input and output formats. Sample test cases are provided to illustrate the expected functionality and formatting of the output.

Uploaded by

Rohit Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1.2
Student Name: Rohit Kumar UID: 23BCS12640
Branch: BE-CSE Section/Group: 605-A
Semester: 4th Date of Performance: 20-02-25
Subject Name: OOPS using JAVA Subject Code: 23CSP-202

1. Aim: David is developing an Electricity Bill Calculator for his apartment complex,
where electricity usage details are recorded as String values and must be converted using
Wrapper Classes (Integer for units consumed and Double for rate per unit). The total bill is
calculated as: Formula Total Bill = Units Consumed × Rate per Unit If the total bill exceeds
₹5000, a 5% discount is applied. Develop a CustomerBill class with private attributes, a
constructor to handle data conversion, and methods to calculate and print the bill. In the
main method, read N customer records, store them using an array of objects, compute the
bill for each customer, and display the final amount formatted to two decimal places. Note
The Customer ID is a non-empty string of up to 10 characters. Input format : The first line
contains an integer N, representing the number of customers. The next N lines contain the
following space-separated values: Customer ID (String) Units Consumed (String, converted
to Integer) Rate per Unit (String, converted to Double) Output format : The output prints
the customer ID followed by the Total Bill formatted to two decimal places for each
customer. Refer to the sample output for the formatting specifications.

Code constraints : The given test cases fall under the following specifications: 1 ≤ N ≤
100 1 ≤ Units Consumed ≤ 10000 0.5 ≤ Rate per Unit ≤ 50.0 Sample test cases : Input 1 :
3 C101 300 7.5 C102 800 6.8 C103 450 5.2 Output 1 : C101: Total Bill = 2250.00 C102:
Total Bill = 5168.00 C103: Total Bill = 2340.00 Input 2 : 2 C201 120 8.0 C202 500 9.5
Output 2 : C201: Total Bill = 960.00 C202: Total Bill = 4750.00.

2. Objective: The objective is to develop an Electricity Bill Calculator for an apartment complex that:
● Reads N customer records, where each record consists of:
o A Customer ID (String)
o Units Consumed (String, converted to Integer)
o Rate per Unit (String, converted to Double)
● Uses Wrapper Classes to handle type conversion.
● Computes the Total Bill using the formula: Total Bill=Units Consumed×Rate per Unit\text{Total Bill} =
\text{Units Consumed} \times \text{Rate per Unit}Total Bill=Units Consumed×Rate per Unit
● Applies a 5% discount if the total bill exceeds ₹5000.
● Stores customer records in an array of objects.
● Displays the final amount formatted to two decimal places
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Java Code:
import java.util.*;

class CustomerBill {
private String customerId;
private int unitsConsumed;
private double ratePerUnit;

public CustomerBill(String customerId, String units, String rate) {


this.customerId = customerId;
this.unitsConsumed = Integer.parseInt(units);
this.ratePerUnit = Double.parseDouble(rate);
}

public double calculateBill() {


double totalBill = unitsConsumed * ratePerUnit;
if (totalBill > 5000) {
totalBill *= 0.95;
}
return totalBill;
}

public void printBill() {


System.out.printf("%s: Total Bill = %.2f%n", customerId, calculateBill());
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = Integer.parseInt(sc.nextLine());
CustomerBill[] customers = new CustomerBill[N];

for (int i = 0; i < N; i++) {


String[] input = sc.nextLine().split(" ");
customers[i] = new CustomerBill(input[0], input[1], input[2]);
}

for (CustomerBill customer : customers) {


customer.printBill();
}
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
1. Output:

You might also like