Source of Money.java


  1: 
  2: import java.util.Scanner;
  3: 
  4: /**
  5:  Class representing 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 newAmount)
 29:     {
 30:         if (newAmount < 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(newAmount * 100);
 39:             dollars = allCents / 100;
 40:             cents = allCents % 100;
 41:         }
 42:     }
 43: 
 44:     public void set(Money moneyObject)
 45:     {
 46:         this.dollars = moneyObject.dollars;
 47:         this.cents = moneyObject.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:                 
 60:         //Delete '$' if any:
 61:         if (amountString.charAt(0) == '$')
 62:             amountString = amountString.substring(1);
 63:         amountString = amountString.trim( );
 64:                 
 65:         //Locate decimal point:
 66:         int pointLocation = amountString.indexOf(".");
 67:         if (pointLocation < 0) //If no decimal point
 68:         {
 69:             cents = 0;
 70:             dollars = Long.parseLong(amountString);
 71:         }
 72:         else //String has a decimal point.
 73:         {
 74:             dollarsString =
 75:                     amountString.substring(0, pointLocation);
 76:             centsString =
 77:                     amountString.substring(pointLocation + 1);
 78:                          //one digit in cents means tenths of a dollar                
 79:             if (centsString.length( ) <= 1)
 80:                 centsString = centsString + "0";
 81:                                 
 82:                         // convert to numeric        
 83:             dollars = Long.parseLong(dollarsString);
 84:             cents = Long.parseLong(centsString);
 85:             if ((dollars < 0) || (cents < 0) || (cents > 99))
 86:             {
 87:                 System.out.println(
 88:                    "Error: Illegal representation of money.");
 89:                 System.exit(0);
 90:             }
 91:         }
 92:      }
 93: 
 94:     public void readInput( )
 95:     {
 96:         System.out.println("Enter amount on a line by itself:");
 97:         Scanner keyboard = new Scanner(System.in);
 98:         String amount = keyboard.nextLine( );
 99:         set(amount.trim( ));
100:     }
101: 
102:      /**
103:       Does not go to the next line after displaying money.
104:      */
105:      public void writeOutput( )
106:      {
107:          System.out.print("$" + dollars);
108:          if (cents < 10)
109:              System.out.print(".0" + cents);
110:          else
111:              System.out.print("." + cents);
112:      }
113: 
114:      /**
115:       Returns n times the calling object.
116:      */
117:      public Money times(int n)
118:      {
119:          Money product = new Money( );
120:          product.cents = n * cents;
121:          long carryDollars = product.cents / 100;
122:          product.cents = product.cents % 100;
123:          product.dollars = n * dollars + carryDollars;
124:          return product;
125:      }
126: 
127:      /**
128:       Returns the sum of the calling object and the argument.
129:      */
130:      public Money add(Money otherAmount)
131:      {
132:          Money sum = new Money( );
133:          sum.cents = this.cents + otherAmount.cents;
134:          long carryDollars = sum.cents / 100;
135:          sum.cents = sum.cents % 100;
136:          sum.dollars = this.dollars
137:                       + otherAmount.dollars + carryDollars;
138:          return sum;
139:      }
140:  }