-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapter.java
More file actions
66 lines (49 loc) · 1.47 KB
/
Adapter.java
File metadata and controls
66 lines (49 loc) · 1.47 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
package structural;
import java.text.DecimalFormat;
interface Metrics {
public double getDistance();
public double getWeight();
}
class UKMetrics implements Metrics{
// In Kilometers
public double distance;
// In Kilograms
public double weight;
UKMetrics(double distance,double weight){
this.distance = distance;
this.weight = weight;
}
public double getDistance() {
return distance;
}
public double getWeight() {
return weight;
}
}
class UkToUsAdapter implements Metrics{
private static final DecimalFormat df = new DecimalFormat("0.00");
public double distance;
// In Kilograms
public double weight;
UkToUsAdapter(Metrics ukType){
this.distance = Double.parseDouble(df.format(ukType.getDistance() * 0.621371));
this.weight = Double.parseDouble(df.format(ukType.getWeight() * 2.20462));
}
@Override
public double getDistance() {
return distance;
}
@Override
public double getWeight() {
return weight;
}
}
public class Adapter {
public static void main(String[] args) {
Metrics uk = new UKMetrics(50, 80);
System.out.println("UK Type: Distance " + uk.getDistance() + " KM and weight " + uk.getWeight() + " Kg");
// Adapter to US type
Metrics us = new UkToUsAdapter(uk);
System.out.println("UK Type: Distance " + us.getDistance() + " Miles and weight " + us.getWeight() + " lbs");
}
}