Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. /**Objects represent nonnegative amounts of money*/
  2. public class Money
  3. {
  4.  /**A number of dollars*/
  5.  private long dollars;
  6.  /**A number of cents*/
  7.  private long cents;
  8.  /**Constructor creates a Money object using the amount of money in
  9.  dollars and cents represented with a decimal number
  10.  @param amount the amount of money in the conventional decimal
  11. format*/
  12.  public Money(double amount)
  13.  {
  14.  if (amount < 0)
  15.  {
  16.  System.out.println(
  17.  "Error: Negative amounts of money are not allowed.");
  18.  System.exit(0);
  19.  }
  20.  else
  21.  {
  22.  long allCents = Math.round(amount*100);
  23.  dollars = allCents/100;
  24.  cents = allCents%100;
  25.  }
  26.  }
  27.  
  28.  
  29.  public Money(Money otherAmount)
  30.  {
  31.      dollars = otherAmount.dollars;
  32.      cents = otherAmount.cents;
  33.      
  34.  }
  35.  
  36.  /**Adds the calling Money object to the parameter Money object.
  37.  @param otherAmount the amount of money to add
  38.  @return the sum of the calling Money object and the parameter
  39.  Money object*/
  40.  public Money add(Money otherAmount)
  41. {
  42.  Money sum = new Money(0);
  43.  sum.cents = this.cents + otherAmount.cents;
  44.  long carryDollars = sum.cents/100;
  45.  sum.cents = sum.cents%100;
  46.  sum.dollars = this.dollars
  47.  + otherAmount.dollars + carryDollars;
  48.  return sum;
  49.  }
  50.  
  51.  /**Subtracts the parameter Money object from the calling Money
  52.  object and returns the difference.
  53.  @param amount the amount of money to subtract
  54.  @return the difference between the calling Money object and the
  55.  parameter Money object*/
  56.  public Money subtract (Money amount)
  57.  {
  58.  Money difference = new Money(0);
  59.  if (this.cents < amount.cents)
  60.  {
  61.  this.dollars = this.dollars - 1;
  62.  this.cents = this.cents + 100;
  63.  }
  64.  difference.dollars = this.dollars - amount.dollars;
  65.  difference.cents = this.cents - amount.cents;
  66.  return difference;
  67.  }
  68.  
  69.  public int compareTo(Money amount)
  70.  {
  71.  int value;
  72. if(this.dollars < amount.dollars)
  73.  {
  74.  value = -1;
  75.  }
  76.  else if (this.dollars > amount.dollars)
  77.  {
  78.  value = 1;
  79.  }
  80.  else if (this.cents < amount.cents)
  81.  {
  82.  value = -1;
  83.  }
  84.  else if (this.cents > amount.cents)
  85.  {
  86.  value = 1;
  87.  }
  88.  else
  89.  {
  90.  value = 0;
  91.  }
  92.  return value;
  93.  }
  94.  
  95.  public boolean equals(Money amount)
  96.   {
  97.     if(this.cents == amount.cents)
  98.     {
  99.       if(this.dollars == amount.dollars)
  100.       {
  101.         return true;
  102.       }
  103.       else
  104.       {
  105.         return false;
  106.       }
  107.     }
  108.     else
  109.     {
  110.       return false;
  111.     }
  112.   }
  113.  
  114.  public String toString()
  115.  {
  116.        
  117.         if(this.cents > 10){
  118.              return "$" + this.dollars + "." + this.cents;
  119.             }
  120.         if(this.cents % 10 == 0)
  121.             return "$" + this.dollars + "." + this.cents + "0";
  122.         else{
  123.             return "$" + this.dollars + "." + "0" + this.cents;
  124.         }
  125.          
  126.      }
  127.      
  128.      
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement