コード例 #1
0
 /** add(Money value) binary operator returns sum of object plus value */
 public Money add(Money value) {
   Money result = new Money();
   result.cents = cents + value.cents;
   result.dollars = dollars + value.dollars;
   // Only need to check if cents is greater than 100 since the
   // largest value that can result from an addition is 198
   if (result.cents >= 100) {
     ++result.dollars;
     result.cents -= 100;
   }
   return result;
 }
コード例 #2
0
 /**
  * subtract(Money value) binary operator returns the difference between object and value returns
  * an error if value is larger than the object calling the method
  */
 public Money subtract(Money value) {
   Money result = new Money();
   // Find the total cents values for the subtrahend (value) and the minuend (this), and compare
   // them to make sure
   // that the subtrahend is smaller than the minuend.
   long myCentsAmount = (this.dollars * 100) + this.cents;
   long valueCentsAmount = (value.dollars * 100) + value.cents;
   long differenceBetween = myCentsAmount - valueCentsAmount;
   if (differenceBetween < 0) { // Subtraction will result in a negative money value
     System.out.println(
         "! ERROR: Subtraction can't result in a negative Money value! "
             + "Setting dollars and cents to default values of 0.");
   } else { // Proper values for subtraction
     result.dollars = differenceBetween / 100;
     result.cents = differenceBetween % 100;
   }
   return result;
 }
コード例 #3
0
ファイル: BankAccount.java プロジェクト: pegurnee/2013-03-211
 public void closeAccount() {
   balance.dollars = 0;
   balance.cents = 0;
 }