Source of Money.java


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