Repository navigation
Expand file tree
/
Copy pathTemperatureConverter.java
More file actions
62 lines (59 loc) · 2.56 KB
/
Copy pathTemperatureConverter.java
File metadata and controls
62 lines (59 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
package com.tempconverter;
/**
* Main temperature converter class that handles conversions between different units.
* Demonstrates Encapsulation pillar of OOP by hiding the conversion logic.
*/
public class TemperatureConverter {
// Encapsulated fields
private Convertible converter;
/**
* Sets the converter based on from and to units.
* @param fromUnit the unit to convert from
* @param toUnit the unit to convert to
*/
public void setConverter(String fromUnit, String toUnit) {
if (fromUnit.equals("Celsius") && toUnit.equals("Fahrenheit")) {
converter = new CelsiusToFahrenheit();
} else if (fromUnit.equals("Fahrenheit") && toUnit.equals("Celsius")) {
converter = new FahrenheitToCelsius();
} else if (fromUnit.equals("Celsius") && toUnit.equals("Kelvin")) {
converter = new CelsiusToKelvin();
} else if (fromUnit.equals("Kelvin") && toUnit.equals("Celsius")) {
converter = new KelvinToCelsius();
} else if (fromUnit.equals("Fahrenheit") && toUnit.equals("Kelvin")) {
// For Fahrenheit to Kelvin, first to Celsius then to Kelvin
// But to keep simple, perhaps add direct classes, but for now, handle in convert method
converter = null; // Will handle specially
} else if (fromUnit.equals("Kelvin") && toUnit.equals("Fahrenheit")) {
converter = null;
} else {
converter = null;
}
}
/**
* Converts the temperature value using the set converter.
* @param value the value to convert
* @param fromUnit from unit
* @param toUnit to unit
* @return converted value
*/
public double convert(double value, String fromUnit, String toUnit) {
setConverter(fromUnit, toUnit);
if (converter != null) {
return converter.convert(value);
} else {
// Handle indirect conversions
if (fromUnit.equals("Fahrenheit") && toUnit.equals("Kelvin")) {
// F to C to K
double celsius = new FahrenheitToCelsius().convert(value);
return new CelsiusToKelvin().convert(celsius);
} else if (fromUnit.equals("Kelvin") && toUnit.equals("Fahrenheit")) {
// K to C to F
double celsius = new KelvinToCelsius().convert(value);
return new CelsiusToFahrenheit().convert(celsius);
} else {
return value; // Same unit
}
}
}
}