-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomCalculator.java
More file actions
118 lines (110 loc) · 2.56 KB
/
CustomCalculator.java
File metadata and controls
118 lines (110 loc) · 2.56 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.util.Scanner;
import java.util.InputMismatchException;
class InvalidInputException extends Exception{
public String toString(){
return "InvalidInputException: "+getMessage();
}
public String getMessage(){
return "The user input is Invalid";
}
}
class CannotDivideBy0Exception extends Exception{
public String toString(){
return "CannotDivideBy0Exception: "+getMessage();
}
public String getMessage(){
return "A number is not divisible by zero(0)";
}
}
class MaxInputExceededException extends Exception{
public String toString(){
return "MaxInputExceededException: "+getMessage();
}
public String getMessage(){
return "You have exceeded the Maximum input Allowed";
}
}
class MaxMultiplierReachedException extends Exception{
public String toString(){
return "MaxMultiplierReachedException: "+getMessage();
}
public String getMessage(){
return "You have excedeed the Maximum multiplier";
}
}
class CustomCalculator{
static void performOperations() throws InvalidInputException,CannotDivideBy0Exception,MaxInputExceededException,MaxMultiplierReachedException{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Values and operand :");
int a;
char c;
int b;
try{
a=sc.nextInt();
c=sc.next().charAt(0);
b=sc.nextInt();
}
catch(InputMismatchException e){
throw new InvalidInputException();
}
if(a<=7000 && b<=7000){
}
else{
throw new MaxInputExceededException();
}
if(c=='+'||c=='-'||c=='*'||c=='/'){
if(c=='+'){
if(-2147483648<a && a<2147483647 && -2147483648<a && a<2147483647){
System.out.println(a+b);
}
else{
throw new InvalidInputException();
}
}
else if(c=='-'){
if(-2147483648<a && a<2147483647 && -2147483648<a && a<2147483647){
System.out.println(a-b);
}
else{
throw new InvalidInputException();
}
}
else if(c=='*'){
if(a*b<100000){
System.out.println(a*b);
}
else{
throw new MaxMultiplierReachedException();
}
}
else if(c=='/'){
if(b!=0){
System.out.println(a/b);
}
else{
throw new CannotDivideBy0Exception();
}
}
}
else{
throw new InvalidInputException();
}
}
public static void main(String args[]){
try{
performOperations();
}
catch(InvalidInputException e){
e.printStackTrace();
}
catch(CannotDivideBy0Exception e){
e.printStackTrace();
}
catch(MaxInputExceededException e){
e.printStackTrace();
}
catch(MaxMultiplierReachedException e){
e.printStackTrace();
}
}
}