Design a class with the following description.
Class Name: Ebill
Data Members:
- String name – To store the name of the consumer.
- int units – To store number of units consumed.
- double bill – To store the amount to be paid.
Member Methods:
- EBill() – Default constructor to initialize the data members.
- void input() – To input consumer name and number of units consumed.
- void calculate() – To calculate the bill based on the following criteria.
- First 1 to 200 units 5 ₹/unit.
- Next 201 to 400 units 7 ₹/unit.
- More than 400 units 10 ₹/unit.
Note: Fixed meter charge ₹50 to be added.
- void display() – To print all details.
Write a main method to create an object of the class and call methods.
Java
import java.util.*;
public class EBill
{
String name;
int units;
double bill;
EBill()
{
name="";
units=0;
bill=0.0;
}
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Customer name: ");
name=sc.nextLine();
System.out.print("Units consumed: ");
units=sc.nextInt();
System.out.println();
}
public void calculate()
{
if(units>=0&&units<=200)
{
bill=(units*5)+50;
}
else if(units>200&&units<=400)
{
bill=((200-0)*5)+((units-200)*7)+50;
}
else if(units>400)
{
bill=((200-0)*5)+((400-200)*7)+((units-400)*10)+50;
}
else
{
System.out.println("Number of units can not be zero or negative");
}
}
void display()
{
System.out.println("Customer Name: "+name);
System.out.println("Number of units consumed: "+units);
System.out.println("Amount to be paid: "+bill);
}
public static void main(String args[])
{
EBill ob=new EBill();
ob.input();
ob.calculate();
ob.display();
}
}Java