-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitConverter.java
More file actions
325 lines (295 loc) · 13.3 KB
/
UnitConverter.java
File metadata and controls
325 lines (295 loc) · 13.3 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import java.util.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.net.URI;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.function.DoubleUnaryOperator;
public class UnitConverter {
// Exact standard constants
private static final double MILES_TO_KM = 1.609344;
private static final double FOOT_TO_M = 0.3048;
private static final double INCH_TO_CM = 2.54;
private static final double LB_TO_KG = 0.4539237;
private static final double STONE_TO_LB = 14.0;
private static final double LITER_TO_US_GALLON = 1.0 / 3.785411784;
private static final double LITER_TO_IMP_GALLON = 1.0 / 4.54609;
private static final double LITER_TO_US_PINT = 1.0 / 0.473176473;
private static final double LITER_TO_IMP_PINT = 1.0 / 0.56826125;
private static final double MPH_TO_KMH = 1.609344;
private static final double YARD_TO_M = 0.9144;
private static final double OZ_TO_G = 28.349523125;
private static final NumberFormat USD_F = NumberFormat.getCurrencyInstance(java.util.Locale.US);
private static final NumberFormat GBP_F = NumberFormat.getCurrencyInstance(java.util.Locale.UK);
enum Category { LENGTH, WEIGHT, TEMPERATURE, VOLUME, CURRENCY, SPEED }
static class Option {
final String label; final Runnable action;
Option(String label, Runnable action) { this.label = label; this.action = action; }
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
println("🇬🇧 Java Unit Converter (UK + US friendly)\n");
Map<Integer, Option> main = new LinkedHashMap<>();
main.put(1, new Option("Length (mi/km, ft/m, in/cm, yd/m)", () -> lengthMenu(sc)));
main.put(2, new Option("Weight (lb/kg, stone/kg, oz/g)", () -> weightMenu(sc)));
main.put(3, new Option("Temperature (°C/°F)", () -> temperatureMenu(sc)));
main.put(4, new Option("Volume (L, US/Imp gal & pint)", () -> volumeMenu(sc)));
main.put(5, new Option("Currency (USD ↔ GBP)", () -> currencyMenu(sc)));
main.put(6, new Option("Speed (mph ↔ km/h)", () -> speedMenu(sc)));
main.put(0, new Option("Exit", () -> { println("Thank you! Happy travels! ✈️"); System.exit(0); }));
while (true) {
printMenu("Main Menu", main);
int choice = readInt(sc, "Choose an option: ");
runChoice(main, choice);
println("");
}
}
private static Double fetchUsdToGbpRate() {
try {
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(5))
.build();
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create("https://api.frankfurter.dev/v1/latest?base=USD&symbols=GBP"))
.timeout(Duration.ofSeconds(8))
.GET()
.build();
HttpResponse<String> res = client.send(req, HttpResponse.BodyHandlers.ofString());
if (res.statusCode() != 200) return null;
// dependency-free parse
String body = res.body();
int i = body.indexOf("\"GBP\"");
if (i < 0) return null;
int colon = body.indexOf(':', i);
if (colon < 0) return null;
// grab number until next non-number char
int p = colon + 1;
StringBuilder num = new StringBuilder();
while (p < body.length()) {
char c = body.charAt(p++);
if ((c >= '0' && c <= '9') || c == '.' ) num.append(c);
else if (num.length() > 0) break;
}
return num.length() > 0 ? Double.parseDouble(num.toString()) : null;
} catch (IOException | InterruptedException e) {
return null; // handles network/timeouts gracefully
}
}
// Menus
private static void lengthMenu(Scanner sc) {
Map<Integer, Option> m = new LinkedHashMap<>();
m.put(1, new Option("Miles → Kilometers", () ->
convertLoop(sc, "miles", "kilometers", v -> v * MILES_TO_KM)
));
m.put(2, new Option("Kilometers → Miles", () ->
convertLoop(sc, "kilometers", "miles", v -> v / MILES_TO_KM)
));
m.put(3, new Option("Feet → Meters", () ->
convertLoop(sc, "feet", "meters", v -> v * FOOT_TO_M)
));
m.put(4, new Option("Meters → Feet", () ->
convertLoop(sc, "meters", "feet", v -> v / FOOT_TO_M)
));
m.put(5, new Option("Inches → Centimeters", () ->
convertLoop(sc, "inches", "centimeters", v -> v * INCH_TO_CM)
));
m.put(6, new Option("Centimeters → Inches", () ->
convertLoop(sc, "centimeters", "inches", v -> v / INCH_TO_CM)
));
m.put(7, new Option("Yards → Meters", () ->
convertLoop(sc, "yards", "meters", v -> v * YARD_TO_M)
));
m.put(8, new Option("Meters → Yards", () ->
convertLoop(sc, "meters", "yards", v -> v / YARD_TO_M)
));
m.put(0, new Option("Back", () -> {}));
subLoop(sc, "Length", m);
}
private static void weightMenu(Scanner sc) {
Map<Integer, Option> m = new LinkedHashMap<>();
m.put(1, new Option("Pounds → Kilograms", () ->
convertLoop(sc, "pounds", "kilograms", v -> v * LB_TO_KG)
));
m.put(2, new Option("Kilograms → Pounds", () ->
convertLoop(sc, "kilograms", "pounds", v -> v / LB_TO_KG)
));
m.put(3, new Option("Stone → Kilograms", () ->
convertLoop(sc, "stone", "kilograms", v -> (v * STONE_TO_LB) * LB_TO_KG)
));
m.put(4, new Option("Kilograms → Stone", () ->
convertLoop(sc, "kilograms", "stone", v -> (v / LB_TO_KG) / STONE_TO_LB)
));
m.put(5, new Option("Ounces(oz) → Grams(g)", () ->
convertLoop(sc, "ounces", "grams", v -> v * OZ_TO_G)
));
m.put(6, new Option("Grams(g) → Ounces(oz)", () ->
convertLoop(sc, "grams", "ounces", v -> v / OZ_TO_G)
));
m.put(0, new Option("Back", () -> {}));
subLoop(sc, "Weight", m);
}
private static void temperatureMenu(Scanner sc) {
Map<Integer, Option> m = new LinkedHashMap<>();
m.put(1, new Option("Celsius(°C) → Fahrenheit(°F)", () -> {
while (true) {
String raw = readLine(sc, "Enter °C (or 'b' to go back): ");
if (raw.equalsIgnoreCase("b")) return;
try {
double c = Double.parseDouble(raw);
double f = c * 9.0/5.0 + 32.0;
println(fmt(c) + " °C = " + fmt(f) + " °F");
} catch (NumberFormatException e) {
println("Please enter a valid number or 'b' to go back.");
}
}
}));
m.put(2, new Option("Fahrenheit(°F) → Celsius(°C)", () -> {
while (true) {
String raw = readLine(sc, "Enter °F (or 'b' to go back): ");
if (raw.equalsIgnoreCase("b")) return;
try {
double f = Double.parseDouble(raw);
double c = (f - 32.0) * 5.0/9.0;
println(fmt(f) + " °F = " + fmt(c) + " °C");
} catch (NumberFormatException e) {
println("Please enter a valid number or 'b' to go back.");
}
}
}));
m.put(0, new Option("Back", () -> {}));
subLoop(sc, "Temperature", m);
}
private static void volumeMenu(Scanner sc) {
Map<Integer, Option> m = new LinkedHashMap<>();
m.put(1, new Option("Liters → US Gallons", () ->
convertLoop(sc, "liters", "US gallons", v -> v * LITER_TO_US_GALLON)
));
m.put(2, new Option("US Gallons → Liters", () ->
convertLoop(sc, "US gallons", "liters", v -> v / LITER_TO_US_GALLON)
));
m.put(3, new Option("Liters → Imperial Gallons", () ->
convertLoop(sc, "liters", "Imperial gallons", v -> v * LITER_TO_IMP_GALLON)
));
m.put(4, new Option("Imperial Gallons → Liters", () ->
convertLoop(sc, "Imperial gallons", "liters", v -> v / LITER_TO_IMP_GALLON)
));
m.put(5, new Option("Liters → US Pints", () ->
convertLoop(sc, "liters", "US pints", v -> v * LITER_TO_US_PINT)
));
m.put(6, new Option("US Pints → Liters", () ->
convertLoop(sc, "US pints", "liters", v -> v / LITER_TO_US_PINT)
));
m.put(7, new Option("Liters → Imperial Pints", () ->
convertLoop(sc, "liters", "Imperial pints", v -> v * LITER_TO_IMP_PINT)
));
m.put(8, new Option("Imperial Pints → Liters", () ->
convertLoop(sc, "Imperial pints", "liters", v -> v / LITER_TO_IMP_PINT)
));
m.put(0, new Option("Back", () -> {}));
subLoop(sc, "Volume", m);
}
private static void currencyMenu(Scanner sc) {
println("\n- Currency -");
println("[1] Auto rate (Frankfurter/ECB)");
println("[2] Enter rate manually");
println("[0] Back");
int how = readInt(sc, "Choose: ");
if (how == 0) return;
Double usdToGbp = null;
if (how ==1) {
usdToGbp = fetchUsdToGbpRate();
if (usdToGbp == null) {
println("Couldn't fetch live rate. Falling back to manual.");
} else {
println("Live rate: 1 USD = " + fmt(usdToGbp) + " GBP");
}
}
final double rate = usdToGbp;
Map<Integer, Option> m = new LinkedHashMap<>();
m.put(1, new Option("USD($) → GBP(£)", () ->
convertLoop(sc, "USD", "GBP", v -> v * rate)
));
m.put(2, new Option("GBP(£) → USD($)", () ->
convertLoop(sc, "GBP", "USD", v -> v / rate)
));
m.put(0, new Option("Back", () -> {}));
subLoop(sc, "Currency", m);
}
private static void speedMenu(Scanner sc) {
Map<Integer, Option> m = new LinkedHashMap<>();
m.put(1, new Option("Miles/hour(mph) → Kilometers/hour(km/h)", () ->
convertLoop(sc, "mph", "km/h", v -> v * MPH_TO_KMH)
));
m.put(2, new Option("Kilometers/hour(km/h) → Miles/hour(mph)", () ->
convertLoop(sc, "km/h", "mph", v -> v / MPH_TO_KMH)
));
m.put(0, new Option("Back", () -> {}));
subLoop(sc, "Speed", m);
}
// Helpers
private static void subLoop(Scanner sc, String title, Map<Integer, Option> menu) {
while (true) {
printMenu(title, menu);
int choice = readInt(sc, "Choose an option: ");
if (choice == 0) return;
runChoice(menu, choice);
println("");
}
}
private static void convert(Scanner sc, String from, String to, double factor) {
double v = readDouble(sc, "Enter " + from + ": ");
double out = v * factor;
println(fmt(v) + " " + from + " = " + fmt(out) + " " + to);
}
private static void printMenu(String title, Map<Integer, Option> items) {
println("\n- " + title + " -");
for (Map.Entry<Integer, Option> e : items.entrySet()) {
System.out.printf("[%d] %s%n", e.getKey(), e.getValue().label);
}
}
private static void runChoice(Map<Integer, Option> menu, int choice) {
Option opt = menu.get(choice);
if (opt != null) opt.action.run();
else println("Invalid choice. Please try again.");
}
private static int readInt(Scanner sc, String prompt) {
while (true) {
System.out.print(prompt);
String s = sc.nextLine().trim();
try { return Integer.parseInt(s); }
catch (NumberFormatException e) { println("Please enter a whole number."); }
}
}
private static double readDouble(Scanner sc, String prompt) {
while (true) {
System.out.print(prompt);
String s = sc.nextLine().trim();
try { return Double.parseDouble(s); }
catch (NumberFormatException e) { println("Please enter a valid number."); }
}
}
private static String readLine(Scanner sc, String prompt) {
System.out.print(prompt);
return sc.nextLine().trim();
}
// friendly formatting w/o scientific notation for typical ranges
private static String fmt(double d) {
return String.format(Locale.US, "%,.6f", d).replaceAll("(\\.\\d*?)0+$", "$1").replaceAll("\\.$", "");
}
private static void println(String s) { System.out.println(s); }
private static void convertLoop(Scanner sc, String from, String to, DoubleUnaryOperator op) {
while (true) {
String raw = readLine(sc, "Enter " + from + " (or 'b' to go back): ");
if (raw.equalsIgnoreCase("b")) return;
try {
double v = Double.parseDouble(raw);
double out = op.applyAsDouble(v);
println(fmt(v) + " " + from + " → " + fmt(out) + " " + to);
} catch (NumberFormatException e) {
println("Please enter a valid number or 'b' to go back.");
}
}
}
}