type.lib
Class Money

java.lang.Object
  extended by type.lib.Fraction
      extended by type.lib.MixedNumber
          extended by type.lib.Money
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<Fraction>

public class Money
extends MixedNumber

This class encapsulates an amount of money. The amount has three attributes: an overall sign and a dollar / cent amounts (both are unsigned integers).

The class derives all its features from its superclass, MixedNumber, because any amount of money is a mixed number having the same sign, a whole part equal to the dollar amount, a proper numerator equal to the cent amount, and a proper denominator of 100.

The case of division by zero is handled the same as in the superclass. In particular, if a string representation of an amount with a zero-denominator is requested, the literal NaM (Not a Money) is returned.

Version:
7.0 - Summer 2010
Author:
H. Roumani, roumani@cse.yorku.ca
See Also:
Serialized Form

Field Summary
 
Fields inherited from class type.lib.Fraction
isQuoted, rng, separator
 
Constructor Summary
Money()
          Construct a default money amount of +1 dollar and zero cents.
Money(double m)
          Construct a money amount having the passed real number value.
Money(int s, long d, long c)
          Construct a money amount having the passed sign, dollar amount, and cent amount.
Money(Money money)
          Construct a copy of the passed Money object.
 
Method Summary
 long getCent()
          An accessor to the cent amount of this money amount.
 long getDollar()
          An accessor to the dollar amount of this money amount.
static Fraction getRandom()
          Create a random fraction.
 boolean resembles(Money other)
          Determine if this money object resembles the passed one.
 java.lang.String toString()
          Determine and return a string representation of this money amount.
 
Methods inherited from class type.lib.MixedNumber
getProperDenominator, getProperNumerator, getSign, getWhole, resembles
 
Methods inherited from class type.lib.Fraction
add, cloneMe, compareTo, divide, equals, getDenominator, getNumerator, getSeparator, hashCode, multiply, pow, resembles, setDenominator, setFraction, setFraction, setNumerator, setSeed, setSeparator, subtract, toProperString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Money

public Money()
Construct a default money amount of +1 dollar and zero cents.


Money

public Money(Money money)
Construct a copy of the passed Money object.

Parameters:
money - the Money object to copy.

Money

public Money(double m)
Construct a money amount having the passed real number value. The passed value is rounded to two decimals using IO.format(byte, java.lang.String) and the dollar and cent parts are then extracted.

Parameters:
m - the real value of the money amount to construct.

Money

public Money(int s,
             long d,
             long c)
Construct a money amount having the passed sign, dollar amount, and cent amount.

Parameters:
s - the sign of the money amount. Must be +1 or -1.
d - the dollar amount. Must be non-negative.
c - the cent amount. Must be non-negative.
Throws:
java.lang.RuntimeException - if any of the passed parameters is not valid as specified above.
Method Detail

getDollar

public long getDollar()
An accessor to the dollar amount of this money amount.

Returns:
the dollar amount of this money amount.

getCent

public long getCent()
An accessor to the cent amount of this money amount.

Returns:
the cent amount of this money amount.

toString

public java.lang.String toString()
Determine and return a string representation of this money amount.

Overrides:
toString in class MixedNumber
Returns:
the amount expressed as the following string: If the amount is negative, the string begins with "(minus) $", otherwise it begins with "$". This is followed by the dollar amount (with thousand separators) followed by " dollars and ", and then the cent amount followed by " cents.". Note however that if the amount involves division by zero, the literal NaM is returned.

resembles

public boolean resembles(Money other)
Determine if this money object resembles the passed one. Two money objects are said to resemble each other if their cent amounts can be expressed using the same number of quarters (25-cent coin). In other words, the quotient of dividing the cent amount by 25 should be the same for both.

Parameters:
other - a reference to the object to test resemblance with.
Returns:
true if other is not null and does resemble this object. The return is false otherwise.

getRandom

public static Fraction getRandom()
Create a random fraction. The return is a Money instance 50% of the times. In the remaining 50%, the return is supplied by the getRandom method of the MixedNumber superclass; i.e. the return is an instance of Money with a probability of 0.5, of MixedNumber with a probability of 0.25, and of Fraction with a probability of 0.25. The Money instances are uniformly distributed in [1000,3000).

This method was added for pedagogical reasons.

Returns:
a randomly chosen fraction.


Java by Abstraction: A Client-View Approach