Advertisement
calcpage

C11_Purse.java

Apr 26th, 2012
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. public class Purse implements Comparable
  3. {
  4.     private ArrayList<String> coins;
  5.  
  6.     private final int Q_VAL = 25;
  7.     private final int D_VAL = 10;
  8.     private final int N_VAL = 5;
  9.     private final int P_VAL = 1;
  10.  
  11.     public Purse()
  12.     {
  13.         coins = new ArrayList<String>();
  14.     }
  15.  
  16.     public void add(String coin)
  17.     {
  18.         coins.add(coin);
  19.     }
  20.  
  21.     public void reverse()
  22.     {
  23.         String temp;
  24.         int i = 0;
  25.         int j = coins.size()-1;
  26.         while(i<j)
  27.         {
  28.             temp = coins.set(i,coins.get(j));
  29.             coins.set(j,temp);
  30.             i++;
  31.             j--;
  32.         }
  33.     }
  34.  
  35.     public void transfer(Purse other)
  36.     {
  37.         for(String coin:other.coins)
  38.         {
  39.             this.add(coin);    
  40.         }
  41.         other.coins.clear();
  42.     }
  43.  
  44.     public boolean sameContents(Object other)
  45.     {
  46.         Purse temp = (Purse)other;
  47.         if(coins.size()==temp.coins.size())
  48.         {
  49.             for(int i=0; i<coins.size(); i++)
  50.             {
  51.                 if(!(coins.get(i).equals(temp.coins.get(i))))
  52.                 {
  53.                     return false;
  54.                 }
  55.             }
  56.         }
  57.         else
  58.         {
  59.             return false;
  60.         }
  61.         return true;
  62.     }
  63.  
  64.     public boolean equals(Object other)
  65.     {
  66.         Purse temp = (Purse)other;
  67.         return this.getValue()==temp.getValue();
  68.     }
  69.  
  70.     public int getValue()
  71.     {
  72.         int sum = 0;
  73.         for(String coin:coins)
  74.         {
  75.             if(coin.equals("Quarter")){sum+=Q_VAL;}
  76.             if(coin.equals("Dime")){sum+=D_VAL;}
  77.             if(coin.equals("Nickel")){sum+=N_VAL;}
  78.             if(coin.equals("Penny")){sum+=P_VAL;}
  79.         }
  80.         return sum;
  81.     }
  82.  
  83.     public String toString()
  84.     {
  85.         String output = "Purse[";
  86.         for(String coin:coins)
  87.         {
  88.             output += coin + ", ";
  89.         }
  90.         //for(int i=0; i<coins.size(); i++)
  91.         //{
  92.         //  output += coins.get(i) + ", ";
  93.         //}
  94.         if(coins.size()==0)
  95.         {
  96.             output += "]";
  97.         }
  98.         else
  99.         {
  100.             output = output.substring(0,output.length()-2)+"]";
  101.         }
  102.         return output;
  103.     }
  104.    
  105.     public int compareTo(Object other)
  106.     {
  107.         Purse temp = (Purse)other;
  108.         return getValue()-temp.getValue();
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement