forked from Denisolt/CSCI-160
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoney.java
More file actions
118 lines (111 loc) · 3.57 KB
/
Money.java
File metadata and controls
118 lines (111 loc) · 3.57 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
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
//Denisolt Shakhbulatov
public class Money
{
private long dollars;
private long cents;
public Money(double amount)
{
if (amount < 0)
{
System.out.println("Error: Negative amounts of money are not allowed.");
System.exit(0);
}
else
{
long allCents = Math.round(amount*100);
dollars = allCents/100;
cents = allCents%100;
}
}
public Money(Money obj1)
{
dollars = obj1.dollars;
cents = obj1.cents;
}
/**Adds the calling Money object to the parameter Money object.
@param otherAmount the amount of money to add
@return the sum of the calling Money object and the parameter Money object*/
public Money add(Money otherAmount)
{
Money sum = new Money(0);
sum.cents = this.cents + otherAmount.cents;
long carryDollars = sum.cents/100;
sum.cents = sum.cents%100;
sum.dollars = this.dollars
+ otherAmount.dollars + carryDollars;
return sum;
}
/**Subtracts the parameter Money object from the calling Money
object and returns the difference.
@param amount the amount of money to subtract
@return the difference between the calling Money object and the
parameter Money object*/
public Money subtract (Money amount)
{
Money difference = new Money(0);
if (this.cents < amount.cents)
{
this.dollars = this.dollars - 1;
this.cents = this.cents + 100;
}
difference.dollars = this.dollars - amount.dollars;
difference.cents = this.cents - amount.cents;
return difference;
}
/**Compares instance variable of the calling object with the
parameter object. It returns -1 if the dollars and the cents
of the calling object are less than the dollars and the cents
of the parameter object, 0 if the dollars and the cents of the
calling object are equal to the dollars and cents of the
parameter object, and 1 if the dollars and the cents of the
calling object are more than the dollars and the cents of the
parameter object.
@param amount the amount of money to compare against
@return -1 if the dollars and the cents of the calling object are
less than the dollars and the cents of the parameter object, 0 if
the dollars and the cents of the calling object are equal to the
dollars and cents of the parameter object, and 1 if the dollars
and the cents of the calling object are more than the dollars and
the cents of the parameter object.*/
public int compareTo(Money amount)
{
int value;
if(this.dollars < amount.dollars)
{
value = -1;
}
else if (this.dollars > amount.dollars)
{
value = 1;
}
else if (this.cents < amount.cents)
{
value = -1;
}
else if (this.cents > amount.cents)
{
value = 1;
}
else
{
value = 0;
}
return value;
}
public boolean equals(Money otherObject)
{
if((dollars==otherObject.dollars)&&(cents==otherObject.cents))
return true;
else
return false;
}
public String toString()
{
String str = "";
if (cents>10)
str = "$" + dollars + "." + cents;
else
str = "$" + dollars + ".0" + cents;
return str;
}
}