-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadNumbers.java
More file actions
226 lines (198 loc) · 9.9 KB
/
ReadNumbers.java
File metadata and controls
226 lines (198 loc) · 9.9 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Author: Pascal Bruno (pascal@pascalbruno.com)
Date: 03/09/13
Description: This class accepts a string of integers and
converts the integer to it's alphabetic version
*/
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
class ReadNumbers
{
private Queue<Integer> digitQueue = new LinkedList<Integer>();
private Queue<String> alphaQueue = new LinkedList<String>();
private String numberToRead;
private String output = "";
private int len;
private int numOne = -1;
private int numTwo = -1;
private int numThree = -1;
private int queueSize;
private String[] alphaArray;
String[] basicNames = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
String[] teenNames = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
String[] tyNames = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninty"};
String[] largeNames = {"thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion",
"octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
"quindecillion"};
public ReadNumbers(String userInput)
{
numberToRead = userInput;
len = numberToRead.length();
initialize();
}
public void initialize()
{
for (int i = 0; i < len; i++)
{
String digit = numberToRead.substring(0,1);
numberToRead = numberToRead.substring(1);
int toAdd = Integer.parseInt(digit);
digitQueue.add(toAdd);
}
convertNumberToAlphabetic();
}
public void convertNumberToAlphabetic()
{
while (!digitQueue.isEmpty())
{
int digitQueueSize = digitQueue.size();
int toDeQueue = digitQueueSize % 3;
int largeNumber = digitQueueSize / 3;
if (toDeQueue == 1)
{
numOne = digitQueue.poll();
if (numOne >= 1 && digitQueue.isEmpty())
alphaQueue.add(" " + basicNames[numOne - 1]);
else if (numOne == 0 && digitQueue.isEmpty())
alphaQueue.add(" Zero");
else
alphaQueue.add(" " + basicNames[numOne - 1]);
if (largeNumber >= 1)
{
if (!digitQueue.isEmpty())
{
alphaQueue.add(" " + largeNames[(largeNumber - 1)]);
}
}
}
if (toDeQueue == 2)
{
numOne = digitQueue.poll();
numTwo = digitQueue.poll();
if (largeNumber >= 1 && digitQueue.size() == 0)
alphaQueue.add(" " + largeNames[(largeNumber - 1)]);
if (numOne == 1)
{
alphaQueue.add(" " + teenNames[numTwo]);
}
else
{
if (numOne >= 1)
{
alphaQueue.add(" " + tyNames[(numOne - 2)]);
if (numTwo >= 1)
{
alphaQueue.add(" " + basicNames[(numTwo - 1)]);
}
}
}
if (largeNumber >= 1)
{
if (!digitQueue.isEmpty())
{
alphaQueue.add(" " + largeNames[(largeNumber - 1)]);
}
}
}
if (toDeQueue == 0)
{
numOne = digitQueue.poll();
numTwo = digitQueue.poll();
numThree = digitQueue.poll();
if (numOne >= 1)
{
alphaQueue.add(" " + basicNames[numOne - 1] + " hundred");
if (numTwo > 1)
{
alphaQueue.add(" " + tyNames[(numTwo) - 2]);
if (numThree >= 1)
{
alphaQueue.add(" " + basicNames[(numThree - 1)]);
}
}
if (numTwo == 1)
{
alphaQueue.add(" " + teenNames[(numThree)]);
}
if (numTwo == 0)
{
if (numThree >= 1)
{
alphaQueue.add(" " + basicNames[(numThree - 1)]);
}
}
}
else
{
if (numTwo > 1)
{
alphaQueue.add(" " + tyNames[(numTwo - 2)]);
if (numThree >= 1)
{
alphaQueue.add(" " + basicNames[(numThree - 1)]);
}
}
if (numTwo == 1)
{
alphaQueue.add(" " + teenNames[(numThree)]);
}
if (numTwo == 0)
{
if (numThree >= 1)
{
alphaQueue.add(" " + basicNames[(numThree - 1)]);
}
}
}
if (largeNumber >= 1)
{
if (!digitQueue.isEmpty())
{
if (numOne != 0 || numTwo != 0 || numThree != 0)
alphaQueue.add(" " + largeNames[(largeNumber - 2)]);
}
}
}
}
queueToString();
}
public void queueToString()
{
queueSize = alphaQueue.size();
alphaArray = new String[queueSize]; // Array that will store the output for getArray()
int i = 0;
// Building the output string and the array for the getter methods
while (!alphaQueue.isEmpty())
{
String element = alphaQueue.poll();
output += element;
alphaArray[i] = element;
alphaArray[i] = alphaArray[i].substring(1);
i++;
}
fixFirstCharacter();
}
// Removes the space in the beginning and put the first character in upper case
public void fixFirstCharacter()
{
String letterToFix = output.substring(1, 2);
letterToFix = letterToFix.toUpperCase();
output = letterToFix + output.substring(2);
}
// Displays the output on the screen
public void readNum()
{
System.out.println("--> " + output + "\n");
}
// Returns the alphabetic version
public String getOutputString()
{
return output;
}
// Returns the ouput in an array, that way it can be used to play wave files
public String[] getOutputArray()
{
return alphaArray;
}
}