-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.java
More file actions
101 lines (97 loc) · 2.47 KB
/
Copy pathDemo.java
File metadata and controls
101 lines (97 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.*;
class ElectricityBill {
private long consumerNo;
private double numUnitsConsumed;
private String consumerName,consumerAddress;
private int consumerAge;
private static int count;
ElectricityBill(long cn,double u,String n,String a,int age)
{
consumerNo=cn;
numUnitsConsumed=u;
consumerName=n;
consumerAddress=a;
consumerAge=age;
count++;
}
void display()
{
System.out.println("Consumer Number:- "+consumerNo);
System.out.println("Consumer Name:- "+consumerName);
System.out.println("Consumer Age:- "+consumerAge);
System.out.println("Consumer Address:- "+consumerAddress);
System.out.println("Total Bill:- Rs"+calculate());
}
double calculate()
{
if(numUnitsConsumed<=100)
return 500.0;
else if(numUnitsConsumed<=200)
return (1.0*numUnitsConsumed+500.0);
else if(numUnitsConsumed<=300)
return (1.2*numUnitsConsumed+500.0);
else
return (1.5*numUnitsConsumed+500.0);
}
void displayCount()
{
System.out.println("\n Total Number of Consumer bills till now="+count);
}
}
public class Demo extends ElectricityBill
{
Demo(long cn,double u,String n,String a,int age)
{
super(cn,u,n,a,age);
}
public void display()
{
super.display();
displayCount();
}
public static void main(String[] args) throws IOException
{
String name,address;
long cno;
int a;
double u;
int ch=1;
BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
do {
System.out.println("Enter your name");
name=sc.readLine();
System.out.println("Enter your Consumer Number");
cno=Long.parseLong(sc.readLine());
System.out.println("Enter your age");
a=Integer.parseInt(sc.readLine());
while(a<0)
{
try {
if(a<0)
throw new NegativeAgeException();
}
catch(NegativeAgeException e)
{
System.out.println(e.toString());
}
System.out.println("Enter your age");
a=Integer.parseInt(sc.readLine());
}
System.out.println("Enter your address");
address=sc.readLine();
System.out.println("Enter the units consumed");
u=Double.parseDouble(sc.readLine());
Demo ob=new Demo(cno,u,name,address,a);
ob.display();
System.out.println("Do you want to calculate another bill? Enter 1 if yes");
ch=Integer.parseInt(sc.readLine());
}while(ch==1);
}
}
class NegativeAgeException extends Exception
{
public String toString()
{
return ("Error: Age cannot be negative!");
}
}