-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMoneyClass.java
67 lines (54 loc) · 1.56 KB
/
MoneyClass.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
public class Money {
private final int euros;
private final int cents;
public Money(int euros, int cents) {
this.euros = euros;
this.cents = cents;
}
public int euros() {
return euros;
}
public int cents() {
return cents;
}
public String toString() {
String zero = "";
if (cents < 10) {
zero = "0";
}
return euros + "." + zero + cents + "e";
}
public Money plus(Money added){
int centsAdded = this.cents + added.cents;
int eurosAdded = this.euros + added.euros;
if(centsAdded > 99){
centsAdded -= 100;
eurosAdded++;
}
Money result = new Money(eurosAdded, centsAdded);
return result;
}
public boolean less(Money compared){
if(this.euros < compared.euros){
return true;
}else if(this.euros == compared.euros && this.cents < compared.cents){
return true;
}else{
return false;
}
}
public Money minus(Money decremented){
int centsDifference = this.cents - decremented.cents;
int eurosDifference = this.euros - decremented.euros;
if(centsDifference < 0){
centsDifference = 100 - Math.abs(centsDifference);
eurosDifference--;
}
if(eurosDifference < 0){
centsDifference = 0;
eurosDifference = 0;
}
Money result = new Money(eurosDifference, centsDifference);
return result;
}
}