-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoney.java
84 lines (62 loc) · 2.03 KB
/
Money.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
import static java.lang.Math.abs;
public class Money {
private final int euros;
private final int cents;
public Money(int euros, int cents) {
if (cents > 99) {
euros += cents / 100;
cents %= 100;
}
this.euros = euros;
this.cents = cents;
}
public int euros() {
return euros;
}
public int cents() {
return cents;
}
@Override
public String toString() {
String zero = "";
if (cents < 10) {
zero = "0";
}
return euros + "." + zero + cents + "e";
}
public Money plus(Money added) {
int tempEuros = this.euros + added.euros;
int tempCents = this.cents + added.cents;
Money addedMoney = new Money(tempEuros, tempCents);
return addedMoney;
}
public boolean less(Money compared) {
if (this.euros < compared.euros) {
return true;
} else if (this.euros == compared.euros && this.cents < compared.cents) {
return true;
}
return false;
}
public Money minus(Money decrimented) {
int tempEuros;
int tempCents;
// Money returnedMoney;
if (this.less(decrimented)) {
//returnedMoney = new Money(0, 0);
tempEuros = 0;
tempCents = 0;
} else if (this.euros >= decrimented.euros && this.cents < decrimented.cents) {
tempEuros = this.euros - decrimented.euros - 1;
tempCents = (this.cents + 100) - decrimented.cents;
// tempCents = abs(this.cents - decrimented.cents);
//returnedMoney = new Money(tempEuros, tempCents);
} else {
tempEuros = this.euros - decrimented.euros;
tempCents = this.cents - decrimented.cents;
//returnedMoney = new Money(tempEuros, tempCents);
}
Money otherMoney = new Money(tempEuros, tempCents);
return otherMoney;
}
}