Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Purse implements Comparable
- {
- private ArrayList<String> coins;
- private final int Q_VAL = 25;
- private final int D_VAL = 10;
- private final int N_VAL = 5;
- private final int P_VAL = 1;
- public Purse()
- {
- coins = new ArrayList<String>();
- }
- public void add(String coin)
- {
- coins.add(coin);
- }
- public void reverse()
- {
- String temp;
- int i = 0;
- int j = coins.size()-1;
- while(i<j)
- {
- temp = coins.set(i,coins.get(j));
- coins.set(j,temp);
- i++;
- j--;
- }
- }
- public void transfer(Purse other)
- {
- for(String coin:other.coins)
- {
- this.add(coin);
- }
- other.coins.clear();
- }
- public boolean sameContents(Object other)
- {
- Purse temp = (Purse)other;
- if(coins.size()==temp.coins.size())
- {
- for(int i=0; i<coins.size(); i++)
- {
- if(!(coins.get(i).equals(temp.coins.get(i))))
- {
- return false;
- }
- }
- }
- else
- {
- return false;
- }
- return true;
- }
- public boolean equals(Object other)
- {
- Purse temp = (Purse)other;
- return this.getValue()==temp.getValue();
- }
- public int getValue()
- {
- int sum = 0;
- for(String coin:coins)
- {
- if(coin.equals("Quarter")){sum+=Q_VAL;}
- if(coin.equals("Dime")){sum+=D_VAL;}
- if(coin.equals("Nickel")){sum+=N_VAL;}
- if(coin.equals("Penny")){sum+=P_VAL;}
- }
- return sum;
- }
- public String toString()
- {
- String output = "Purse[";
- for(String coin:coins)
- {
- output += coin + ", ";
- }
- //for(int i=0; i<coins.size(); i++)
- //{
- // output += coins.get(i) + ", ";
- //}
- if(coins.size()==0)
- {
- output += "]";
- }
- else
- {
- output = output.substring(0,output.length()-2)+"]";
- }
- return output;
- }
- public int compareTo(Object other)
- {
- Purse temp = (Purse)other;
- return getValue()-temp.getValue();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement