-
Notifications
You must be signed in to change notification settings - Fork 0
/
RomanNumeralsCalculator.java
114 lines (102 loc) · 3.69 KB
/
RomanNumeralsCalculator.java
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
package com.wisetesch.global;
import java.util.Scanner;
import java.util.regex.Pattern;
// user-defined expception
class AdditionException extends Exception
{
AdditionException(String s)
{
super(s);
}
}
public class RomanNumeralsCalculator {
enum RomanLetters {
M(1000),CM(900),D(500),CD(400),
C(100),XC(90),L(50),XL(40),X(10),
IX(9),V(5),IV(4),I(1);
private int value;
RomanLetters (int value){
this.value=value;
}
public int getValue(){
return this.value;
}
}
private static int convertTONumber(String[] romanStrArray){
int endValue=0;
for (String romanValue : romanStrArray ) {
RomanLetters[] enumArray = RomanLetters.values();
int tempCount=0;
while ((romanValue.length() > 0) && (tempCount < enumArray.length)) {
RomanLetters symbol = enumArray[tempCount];
if (romanValue.startsWith(symbol.name())) {
endValue += symbol.getValue();
romanValue = romanValue.substring(symbol.name().length());
} else tempCount++;
}
}
return endValue;
}
private static String convertToRoman(int number){
StringBuilder result= new StringBuilder();
RomanLetters[] enumArray = RomanLetters.values();
int tempCount=0;
while (tempCount<enumArray.length){
if(number>=enumArray[tempCount].getValue()){
result.append(enumArray[tempCount].name());
number-=enumArray[tempCount].getValue();
}
else {
tempCount++;
}
}
return result.toString();
}
public static void main(String[] args) {
String str1="",str2="";
Pattern pattern = Pattern.compile("^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$");
try {
while(true){
System.out.println("Enter the first Roman numeral or quit to exit");
Scanner sc = new Scanner(System.in);
str1 = sc.nextLine();
switch(str1){
case "quit": {
System.out.println("Bye!!");
System.exit(0);
}
case "": {
System.out.println("Empty Value..Try Again.");
System.exit(0);
}
}
System.out.println("Enter the second Roman numeral or quit to exit");
Scanner scc = new Scanner(System.in);
str2 = scc.nextLine();
switch(str2){
case "quit": {
System.out.println("Bye!!");
System.exit(0);
}
case "": {
System.out.println("Empty Value..Try Again.");
System.exit(0);
}
}
if((pattern.matcher(str1).matches()) && (pattern.matcher(str2).matches())) {
String[] strArray = {str1, str2};
int addedValue = convertTONumber(strArray);
if(addedValue>3000)
throw new AdditionException(" TOTAL VALUE EXCEEDS 3000");
System.out.println("Added value is : " + convertToRoman(addedValue));
}
else{
throw new Exception("NOT A VALID ROMAN NUMERAL (OR) VALUE EXCEEDS 3000");
}
}
}catch (Exception e)
{
System.out.println("Exception :" +e.getMessage());
}
}
}