-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChequeWriter.java
152 lines (126 loc) · 4.35 KB
/
ChequeWriter.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.wisetesch.global;
import java.math.BigDecimal;
class BoundaryException extends Exception
{
BoundaryException(String s)
{
super(s);
}
}
public class ChequeWriter {
private static final int HUNDRED =100;
private static final int THOUSAND = 1000;
private static final int TEN = 10;
private static final String[] oncePlace = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};
private static final String[] tenPlace = {
"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};
private static final String[] biggerValues = {
"",
" thousand",
" million",
" billion"
};
private String convertThousandsValueToWords(long curencyValue) {
String currencyInWords="";
if (curencyValue % HUNDRED < 20){
currencyInWords = oncePlace[(int)curencyValue % HUNDRED];
curencyValue /= HUNDRED;
}
else {
currencyInWords = oncePlace[(int)curencyValue % TEN];
curencyValue /= TEN;
currencyInWords = tenPlace[(int)curencyValue % TEN] + currencyInWords;
curencyValue /= TEN;
}
if (curencyValue == 0) {
return currencyInWords;
}
if(!currencyInWords.isEmpty())
return oncePlace[(int)curencyValue] + " hundred and" + currencyInWords;
else
return oncePlace[(int)curencyValue];
}
private String convert(BigDecimal bgNumber) throws BoundaryException {
StringBuilder dollarWords = new StringBuilder();
String centWords = "";
int decimalValue;
long dollarValue;
if (bgNumber.longValue() == 0) {
return "zero";
}
//Check for boundary condition
if ((bgNumber.longValue() > 2000000000) || bgNumber.longValue() < 0)
throw new BoundaryException(" VALUE GREATER THAN TWO BILLION : ChequeWriter");
//split dollar and decimal value seperatel to process
if (bgNumber.toString().contains(".")) {
String[] decimal = String.valueOf(bgNumber).split("\\.");
dollarValue = Integer.valueOf(decimal[0]);
decimalValue = Integer.valueOf(decimal[1]);
centWords = convertThousandsValueToWords(decimalValue);
} else {
//if there is no decimal value
dollarValue = bgNumber.longValue();
}
//store cent value to words
if (!centWords.isEmpty()) {
centWords = " AND " + centWords + " CENTS";
}
int bigValueholder = 0; // index to hold thousand, million and billion
do {
long tempNum = dollarValue % THOUSAND;
if (tempNum != 0) {
String strTemp = convertThousandsValueToWords(tempNum);
dollarWords.insert(0, strTemp + biggerValues[bigValueholder]);
}
bigValueholder++;
dollarValue /= THOUSAND;
} while (dollarValue > 0);
return dollarWords.toString().concat(" DOLLARS").concat(centWords).trim();
}
public static void main(String[] args) {
ChequeWriter cwObj = new ChequeWriter();
BigDecimal bg = new BigDecimal("200000000");
BigDecimal bg1 = new BigDecimal("45.55");
BigDecimal bg2 = new BigDecimal("23456767.09");
BigDecimal bg3 = new BigDecimal("2000000001");
try {
System.out.println(bg + " : " + cwObj.convert(bg));
System.out.println(bg1+ " : " + cwObj.convert(bg1));
System.out.println(bg2+ " : " +cwObj.convert(bg2));
//System.out.println(bg3+ " : " +cwObj.convert(bg3));
} catch (BoundaryException e) {
e.printStackTrace();
}
}
}