forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverter.java
223 lines (112 loc) · 3.61 KB
/
Converter.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
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
package currencyConverter;
import java.util.ArrayList;
import java.util.HashMap;
public class Currency {
private String name;
private String shortName;
private HashMap<String, Double> exchangeValues = new HashMap<String, Double>();
// “Currency” Constructor
public Currency(String nameValue, String shortNameValue) {
this.name = nameValue;
this.shortName = shortNameValue;
}
// Getter for name
public String getName() {
return this.name;
}
// Setter for name
public void setName(String name) {
this.name = name;
}
// Getter for shortName
public String getShortName() {
return this.shortName;
}
// Setter for shortName
public void setShortName(String shortName) {
this.shortName = shortName;
}
// Getter for exchangeValues
public HashMap<String, Double> getExchangeValues() {
return this.exchangeValues;
}
// Setter for exchangeValues
public void setExchangeValues(String key, Double value) {
this.exchangeValues.put(key, value);
}
// Set default values for a currency
public void defaultValues() {
String currency = this.name;
switch (currency) {
case “US Dollar”:
this.exchangeValues.put(“USD”, 1.00);
this.exchangeValues.put(“EUR”, 0.93);
this.exchangeValues.put(“GBP”, 0.66);
this.exchangeValues.put(“CHF”, 1.01);
this.exchangeValues.put(“CNY”, 6.36);
this.exchangeValues.put(“JPY”, 123.54);
break;
case “Euro”:
this.exchangeValues.put(“USD”, 1.073);
this.exchangeValues.put(“EUR”, 1.00);
this.exchangeValues.put(“GBP”, 0.71);
this.exchangeValues.put(“CHF”, 1.08);
this.exchangeValues.put(“CNY”, 6.83);
this.exchangeValues.put(“JPY”, 132.57);
break;
case “British Pound”:
this.exchangeValues.put(“USD”, 1.51);
this.exchangeValues.put(“EUR”, 1.41);
this.exchangeValues.put(“GBP”, 1.00);
this.exchangeValues.put(“CHF”, 1.52);
this.exchangeValues.put(“CNY”, 9.60);
this.exchangeValues.put(“JPY”, 186.41);
break;
case “Swiss Franc”:
this.exchangeValues.put(“USD”, 0.99);
this.exchangeValues.put(“EUR”, 0.93);
this.exchangeValues.put(“GBP”, 0.66);
this.exchangeValues.put(“CHF”, 1.00);
this.exchangeValues.put(“CNY”, 6.33);
this.exchangeValues.put(“JPY”, 122.84);
break;
case “Chinese Yuan Renminbi”:
this.exchangeValues.put(“USD”, 0.16);
this.exchangeValues.put(“EUR”, 0.15);
this.exchangeValues.put(“GBP”, 0.11);
this.exchangeValues.put(“CHF”, 0.16);
this.exchangeValues.put(“CNY”, 1.00);
this.exchangeValues.put(“JPY”, 19.41);
break;
case “Japanese Yen”:
this.exchangeValues.put(“USD”, 0.008);
this.exchangeValues.put(“EUR”, 0.007);
this.exchangeValues.put(“GBP”, 0.005);
this.exchangeValues.put(“CHF”, 0.008);
this.exchangeValues.put(“CNY”, 0.051);
this.exchangeValues.put(“JPY”, 1.000);
break;
}
}
// Initialize currencies
public static ArrayList<Currency> init() {
ArrayList<Currency> currencies = new ArrayList<Currency>();
currencies.add( new Currency(“US Dollar”, “USD”) );
currencies.add( new Currency(“Euro”, “EUR”) );
currencies.add( new Currency(“British Pound”, “GBP”) );
currencies.add( new Currency(“Swiss Franc”, “CHF”) );
currencies.add( new Currency(“Chinese Yuan Renminbi”, “CNY”) );
currencies.add( new Currency(“Japanese Yen”, “JPY”) );
for (Integer i =0; i < currencies.size(); i++) {
currencies.get(i).defaultValues();
}
return currencies;
}
// Convert a currency to another
public static Double convert(Double amount, Double exchangeValue) {
Double price;
price = amount * exchangeValue;
price = Math.round(price * 100d) / 100d;
return price;
}
}