Source of Money.java


  1: 
  2: import java.util.*;
  3: 
  4: /**
  5:  Objects represent nonnegative amounts of money,
  6:  such as $100, $41.99, $0.05.
  7: */
  8: public class Money
  9: {
 10:     private long dollars;
 11:     private long cents;
 12: 
 13:     public void set(long newDollars)
 14:     {
 15:         if (newDollars < 0)
 16:         {
 17:             System.out.println(
 18:                  "Error: Negative amounts of money are not allowed.");
 19:             System.exit(0);
 20:         }
 21:         else
 22:         {
 23:             dollars = newDollars;
 24:             cents = 0;
 25:         }
 26:     }
 27: 
 28:     public void set(double amount)
 29:     {
 30:         if (amount < 0)
 31:         {
 32:             System.out.println(
 33:                  "Error: Negative amounts of money are not allowed.");
 34:             System.exit(0);
 35:         }
 36:         else
 37:         {
 38:             long allCents = Math.round(amount*100);
 39:             dollars = allCents/100;
 40:             cents = allCents%100;
 41:         }
 42:     }
 43: 
 44:     public void set(Money otherObject)
 45:     {
 46:         this.dollars = otherObject.dollars;
 47:         this.cents = otherObject.cents;
 48:     }
 49: 
 50:     /**
 51:      Precondition: The argument is an ordinary representation
 52:      of an amount of money, with or without a dollar sign.
 53:      Fractions of a cent are not allowed.
 54:     */
 55:     public void set(String amountString)
 56:     {
 57:         String dollarsString;
 58:         String centsString;
 59:         //Delete '$' if any:
 60:         if (amountString.charAt(0) == '$')
 61:             amountString = amountString.substring(1);
 62:         amountString = amountString.trim( );
 63:         //Locate decimal point:
 64:         int pointLocation = amountString.indexOf(".");
 65:         if (pointLocation < 0) //If no decimal point
 66:         {
 67:             cents = 0;
 68:             dollars = Long.parseLong(amountString);
 69:         }
 70:         else //String has a decimal point.
 71:         {
 72:             dollarsString =
 73:                     amountString.substring(0, pointLocation);
 74:             centsString =
 75:                     amountString.substring(pointLocation + 1);
 76:             if (centsString.length( ) <= 1)
 77:             //if one digit meaning tenths of a dollar
 78:                 centsString = centsString + "0";
 79:             dollars = Long.parseLong(dollarsString);
 80:             cents = Long.parseLong(centsString);
 81:             if ((dollars < 0) || (cents < 0) || (cents > 99))
 82:             {
 83:                 System.out.println(
 84:                    "Error: Illegal representation of money.");
 85:                 System.exit(0);
 86:             }
 87:         }
 88:      }
 89: 
 90:     public void readInput( )
 91:     {
 92:         System.out.println("Enter amount on a line by itself:");
 93:         Scanner keyboard = new Scanner(System.in);
 94:         String amount = keyboard.nextLine( );
 95:         set(amount.trim( ));
 96:     }
 97: 
 98:      /**
 99:       Does not go to the next line after outputting money.
100:      */
101:      public void writeOutput( )
102:      {
103:          System.out.print("$" + dollars);
104:          if (cents < 10)
105:              System.out.print(".0" + cents);
106:          else
107:              System.out.print("." + cents);
108:      }
109: 
110:      /**
111:       Returns n times the calling object.
112:      */
113:      public Money times(int n)
114:      {
115:          Money product = new Money( );
116:          product.cents = n*cents;
117:          long carryDollars = product.cents/100;
118:          product.cents = product.cents%100;
119:          product.dollars = n*dollars + carryDollars;
120:          return product;
121:      }
122: 
123:      /**
124:       Returns the sum of the calling object and the argument.
125:      */
126:      public Money add(Money otherAmount)
127:      {
128:          Money sum = new Money( );
129:          sum.cents = this.cents + otherAmount.cents;
130:          long carryDollars = sum.cents/100;
131:          sum.cents = sum.cents%100;
132:          sum.dollars = this.dollars
133:                       + otherAmount.dollars + carryDollars;
134:          return sum;
135:      }
136:  }