View Javadoc

1   package com.diasparsoftware.java.util;
2   
3   import java.io.Serializable;
4   import java.text.*;
5   import java.util.*;
6   
7   public class Money implements Cloneable, Serializable, Comparable {
8       public static Money ZERO = Money.dollars(0);
9   
10      public static Money cents(int cents) {
11          return new Money(cents);
12      }
13  
14      public static Money dollars(int dollars) {
15          return new Money(dollars, 0);
16      }
17  
18      public static Money dollars(int dollars, int cents) {
19          return new Money(dollars, cents);
20      }
21  
22      public static Money parse(String moneyAsString) {
23          try {
24              return new Money(parseToCents(moneyAsString));
25          }
26          catch (ParseException e) {
27              e.printStackTrace();
28          }
29          return null;
30      }
31  
32      private static int parseToCents(String moneyAsString)
33          throws ParseException {
34  
35          double amountInDollars =
36              NumberFormat
37                  .getCurrencyInstance()
38                  .parse(moneyAsString)
39                  .doubleValue();
40          return (int) (amountInDollars * 100);
41      }
42  
43      private int cents;
44  
45      public Money() {
46          this.cents = 0;
47      }
48  
49      public Money(int cents) {
50          this.cents = cents;
51      }
52  
53      public Money(int dollars, int cents) {
54          this(100 * dollars + cents);
55      }
56  
57      public Money(String moneyAsString) throws ParseException {
58          this(parseToCents(moneyAsString));
59      }
60  
61      public Money add(Money augend) {
62          return new Money(this.cents + augend.cents);
63      }
64  
65      public Object clone() {
66          return new Money(cents);
67      }
68  
69      public boolean equals(Object other) {
70          if (other != null && getClass() == other.getClass()) {
71              Money that = (Money) other;
72              return this.cents == that.cents;
73          }
74          return false;
75      }
76  
77      public int hashCode() {
78          return cents;
79      }
80  
81      public int inCents() {
82          return cents;
83      }
84  
85      private double inDollars() {
86          return ((double) inCents()) / 100.0d;
87      }
88  
89      public boolean isValid() {
90          return true;
91      }
92  
93      public Money multipliedBy(int times) {
94          return Money.cents(this.inCents() * times);
95      }
96  
97      public Money multipliedBy(float times) {
98          return Money.cents(Math.round(this.inCents() * times));
99      }
100 
101     public Money negate() {
102         return null;
103     }
104 
105     public Money roundToNearestDollar() {
106         return null;
107     }
108 
109     public List split(int nWays) {
110         List result = new ArrayList();
111         int baseSplitInCents = inCents() / nWays;
112         int centsLeftOver = inCents() - baseSplitInCents * nWays;
113 
114         for (int i = 0; i < nWays; i++) {
115             int eachSplitInCents;
116             if (i < centsLeftOver) {
117                 eachSplitInCents = baseSplitInCents + 1;
118             }
119             else {
120                 eachSplitInCents = baseSplitInCents;
121             }
122 
123             result.add(new Money(eachSplitInCents));
124         }
125 
126         return result;
127     }
128 
129     public String toString() {
130         return NumberFormat.getCurrencyInstance().format(inDollars());
131     }
132 
133     public boolean valueInCentsIs(int expected) {
134         return this.cents == expected;
135     }
136 
137     public int compareTo(Object other) {
138         Money that = (Money) other;
139         return this.inCents() - that.inCents();
140     }
141 }