-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathCurrency.java
More file actions
57 lines (42 loc) · 1.7 KB
/
Currency.java
File metadata and controls
57 lines (42 loc) · 1.7 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
public abstract class Currency {
private double exchangeRate;
private String currencySymbol;
public Currency(double exchangeRate, String currencySymbol){
this.exchangeRate = exchangeRate;
this.currencySymbol = currencySymbol;
}
@Deprecated
public Currency(double exchangeRate){
this(exchangeRate, null);
}
protected void changeRate(double newRate){
this.exchangeRate = newRate;
}
protected double getRate(){
return this.exchangeRate;
}
protected double convertToNewCurrency(double denomination, Currency newCurrency) {
return newCurrency.convertFromDollars(this.convertToDollars(denomination));
}
protected double convertFromDollars(double denominationInDollars) {
double workingDollars = multiplyBy100(denominationInDollars);
return divideBy100(Math.round(workingDollars * this.getRate()));
}
protected double convertToDollars(double denominationInForeignCurrency) {
double workingForeignCurrency = multiplyBy100(denominationInForeignCurrency);
return divideBy100(Math.round(workingForeignCurrency / this.getRate()));
}
protected double multiplyBy100(double valueToMultiply) {
double multipliedValue = Math.round(valueToMultiply * 100);
return multipliedValue;
}
protected double divideBy100(double valueToDivide) {
double dividedValue = valueToDivide / 100;
return dividedValue;
}
protected String displayWithSymbol (double amount){
StringBuilder builder = new StringBuilder();
builder.append(String.format("%4s", this.currencySymbol)).append(" " + amount + "\n");
return builder.toString();
}
}