Class MyCurrency

java.lang.Object
  |
  +--MyCurrency

public class MyCurrency
extends java.lang.Object

This class is intended to be used for currency items so as to avoid the loss-of-precision problems so common to floats and doubles. (This class was created to demonstrate the use of Javadoc.)


Constructor Summary
MyCurrency()
          MyCurrency default constructor initializes the currency amount to $0.00
MyCurrency(int d, int c)
          MyCurrency overloaded constructor allows the user to specify dollars and cents amounts.
 
Method Summary
 MyCurrency add(int i)
          This method adds an integer to the currency amount.
 MyCurrency add(MyCurrency c)
          This method adds the amount of one MyCurrency object to the amount of the current MyCurrency object.
 int getCents()
          The getCents method returns the cents portion only.
 int getDollars()
          The getDollars method returns the dollars portion only.
 MyCurrency setAmount(int d, int c)
          The setCurrency method allows the user to change the dollar and cents amounts.
 double toDouble()
          The toDouble method returns the currency amount as a double.
 java.lang.String toString()
          The toString method returns the currency amount as a String, with dollars sign and decimal point.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MyCurrency

public MyCurrency()
MyCurrency default constructor initializes the currency amount to $0.00


MyCurrency

public MyCurrency(int d,
                  int c)
MyCurrency overloaded constructor allows the user to specify dollars and cents amounts.

Parameters:
d - - dollars
c - - cents
Method Detail

setAmount

public MyCurrency setAmount(int d,
                            int c)
The setCurrency method allows the user to change the dollar and cents amounts. If the given cents is over 99, dollars and cents will be adjusted accordingly, thereby ensuring a consistent state.

Parameters:
d - - dollars
c - - cents
Returns:
a reference to the current MyCurrency object

getDollars

public int getDollars()
The getDollars method returns the dollars portion only.

Returns:
an integer containing the dollars.

getCents

public int getCents()
The getCents method returns the cents portion only.

Returns:
an integer containing the cents.

toDouble

public double toDouble()
The toDouble method returns the currency amount as a double. Remember, there are precision problems associated with floats and doubles!

Returns:
a double containing the whole amount.

toString

public java.lang.String toString()
The toString method returns the currency amount as a String, with dollars sign and decimal point.

Overrides:
toString in class java.lang.Object
Returns:
a String with dollar sign and decimal point.

add

public MyCurrency add(int i)
This method adds an integer to the currency amount. The integer is assumed to be cents. If the resulting cents is over 99, dollars and cents will be adjusted accordingly, thereby ensuring a consistent state.

Parameters:
i - integer to be added (cents)
Returns:
a reference to the current MyCurrency object

add

public MyCurrency add(MyCurrency c)
This method adds the amount of one MyCurrency object to the amount of the current MyCurrency object. If the resulting cents is over 99, dollars and cents will be adjusted accordingly, thereby ensuring a consistent state.

Parameters:
c - MyCurrency amount to be added
Returns:
a reference to the current MyCurrency object