KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > util > Currency


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: Currency.java,v 1.1 2004/04/21 19:31:58 slobodan Exp $
23  */

24
25 package com.lutris.util;
26
27 import java.math.BigDecimal JavaDoc;
28
29 /**
30  * Object to store and manipulate money.
31  */

32 public class Currency extends BigDecimal JavaDoc {
33     /**
34      * Construct a Currency object of value zero.
35      */

36     public Currency() {
37         super(0);
38     }
39
40     /**
41      * Construct a Currency object from a BigDecimal or Currency object.
42      * A null value result in an object containing zero.
43      */

44     public Currency(BigDecimal JavaDoc value) {
45         super((value == null) ? 0.0 : value.doubleValue());
46     }
47  
48     /**
49      * Construct a Currency object from a double.
50      */

51     public Currency(double value) {
52         super(value);
53     }
54  
55     /**
56      * Construct a Currency object from a float.
57      */

58     public Currency(float value) {
59         super((double)value);
60     }
61  
62     /**
63      * Construct a Currency object from a String.
64      */

65     public Currency(String JavaDoc value) {
66         this(Double.valueOf(value).doubleValue());
67     }
68
69     /**
70      * Check if equal to a float value.
71      */

72     public boolean equals(float value) {
73         return compareTo(new Currency(value)) == 0;
74     }
75
76     /**
77      * Check if equal to a double value.
78      */

79     public boolean equals(double value) {
80         return compareTo(new Currency(value)) == 0;
81     }
82
83     /**
84      * Returns a Currency whose value is (this + val).
85      */

86     public Currency add(Currency val) {
87         return new Currency(super.add(val));
88     }
89
90     /**
91      * Returns a Currency whose value is (this - val).
92      */

93     public Currency subtract(Currency val) {
94         return new Currency(super.subtract(val));
95     }
96
97     /**
98      * Returns a Currency whose value is (this * val)
99      */

100     public Currency multiply(Currency val){
101         return new Currency(super.multiply(val));
102     }
103             
104     /**
105      * Returns a Currency whose value is (this * val)
106      */

107     public Currency multiply(int val){
108         return multiply(new Currency(val));
109     }
110             
111     /**
112      * Returns a Currency whose value is (this / val).
113      */

114     public Currency divide(Currency val)
115         throws ArithmeticException JavaDoc, IllegalArgumentException JavaDoc {
116         return new Currency(super.divide(val, 2, ROUND_HALF_UP));
117     }
118
119     /**
120      * Returns a Currency whose value is (this / val).
121      */

122     public Currency divide(int val)
123         throws ArithmeticException JavaDoc, IllegalArgumentException JavaDoc {
124         return divide(new Currency((double)val));
125     }
126
127     /**
128      * Returns a Currency whose value is the absolute value of this
129      * number.
130      */

131     public Currency absCurrency() {
132         return (signum() < 0 ? negateCurrency() : this);
133     }
134
135     /**
136      * Returns a Currency whose value is -1 * this.
137      */

138     public Currency negateCurrency() {
139         return new Currency(negate());
140     }
141
142     /**
143      * Convert to string with two decimals.
144      */

145     public String JavaDoc toString() {
146         if (scale() == 2) {
147             return super.toString();
148         } else {
149             return setScale(2, ROUND_HALF_UP).toString();
150         }
151     }
152 }
153
Popular Tags