I have an assignment to create a class Purse and print the coins in the purse, reverse the sequence of the coins, transfer the contents of one purse to another, and compare to see if two purses have the same contents. I have the following code but the transfer is not working correctly because it is leaving out the last coin and the same contents is not showing correctly which ones have the same contents. I cannot use any methods from the collections or arrays class.

import java.util.ArrayList;
import java.util.StringTokenizer;


public class Purse
{
	public Purse()
	{
		coins = new ArrayList<String>();
	}
	
	public void addCoin(String coinName)
	{
		coins.add(coinName);
	}
	
	public String toString()
	{
		return "Purse" + coins;

	}
	
	 public int countCoins()
	 {
		 return coins.size();
	 }

	public void reverse()
	{
		for(int i = 0; i < coins.size() / 2; i++)
		{    
			String coin1 = coins.get(i);
			String coin2 = coins.get(coins.size()-1-i);
			coins.set(i,coin2);
			coins.set(coins.size()-1-i,coin1);
		}
	}
	
	 public void transfer (Purse other)
	 {
		 for(int i = 0; i < other.coins.size();i++)
		 {
			 coins.add(other.coins.get(i));
			 other.coins.clear();
		 }
	 }
	 
	 public boolean sameContents(Object other)
	 {
		 boolean isSame = false;
		 for (int i = 0; i < coins.size(); i++)
		 {
			 if (!(coins.get(i).equals(coins.get(i))))
				 isSame = false;
			 else
				 isSame = true;
		 }
		 return isSame;
 }
	private ArrayList<String> coins;

}
public class PurseTester
{
	public static void main(String[] args)
	{
		Purse p = new Purse();
		p.addCoin("Quarter");
		p.addCoin("Dime");
		p.addCoin("Nickel");
		p.addCoin("Dime");
		
		System.out.println("Original purse: " + p.toString());
		System.out.println("Expected: Purse[Quarter,Dime,Nickel,Dime]");
		p.reverse();
		System.out.println("Reversed purse: " + p.toString());
		System.out.println("Expected: Purse[Dime,Nickel,Dime,Quarter]");;
		
		Purse a = new Purse();
		a.addCoin("Quarter");
		a.addCoin("Dime");
		a.addCoin("Nickel");
		a.addCoin("Dime");
		
		Purse b = new Purse();
		b.addCoin("Dime");
		b.addCoin("Nickel");
		
		a.transfer(b);
		
		System.out.println(a.toString());
		System.out.println("Expected: Purse[Quarter, Dime, Nickel, Dime, Dime, Nickel]");
		System.out.println(b.toString());
		System.out.println("Expected: Purse[]");
		
		Purse u = new Purse();
		u.addCoin("Quarter");
		u.addCoin("Dime");
		
		Purse v = new Purse();
		v.addCoin("Quarter");
		v.addCoin("Dime");
		
		System.out.println(u.sameContents(v));
		System.out.println("Expected: true");
		
		Purse w = new Purse();
		w.addCoin("Nickel");
		w.addCoin("Dime");
		w.addCoin("Nickel");
		
		Purse x = new Purse();
		x.addCoin("Nickel");
		x.addCoin("Dime");
		x.addCoin("Quarter");
		
		System.out.println(w.sameContents(x));
		System.out.println("Expected: false");
		
		Purse y = new Purse();
		y.addCoin("Nickel");
		y.addCoin("Dime");
		y.addCoin("Nickel");
		
		Purse z = new Purse();
		z.addCoin("Nickel");
		z.addCoin("Dime");
		
		System.out.println(x.sameContents(z));
		System.out.println("Expected: false");
		
		
		
		
	}
}

If you use "clear()" from coins which is an ArrayList, you will remove all elements! Take line 44 in your Purse class out of the loop.

A better idea is to implement a method that clear the purse for you (to hide the information). Implement a method inside your Purse class like...

class Purse {
  ...
  public void clearPurse() {
    coins.clear();
  }
  ...
}

And then call it in your transfer() method.

public void transfer (Purse other) {
  for(int i = 0; i < other.coins.size();i++) {
    coins.add(other.coins.get(i));
  }
  other.clearPurse();
}

Oh I see...I moved it and that fixed that problem. Do you know why my sameContents method isn't working correctly?

Hmm... You are passing in Object? Why not "Purse"? Or you are expecting others to extend your class?

Anyway, you could first check for the "size" of coins in both purse. If they are not equal, return false. If they are equal, then you can start checking.

The checking will be tricky. It seems that your class allow duplicated String. This will be difficult. One simple way (but may not be the most efficient) to do is to copy the whole coin of itself, and then remove each matched coin from the other's coins.

public boolean isSame(Purse other) {
  if (coins.size()!=other.coins.size()) { return false; }
  ArrayList<String> copied = new ArrayList<String>();
  for (int i=0; i<coins.size(); i++) { copied.add(coins.get(i)); }
  for (int i=0; i<other.coins.size(); i++) {
    if (!copied.remove(other.coins.get(i))) { return false; }
  }
  return true;
}

OK, here is what the method above is doing...

1)If the size of both purse aren't the same, no need to check further - false.
2)Copy over the class's coins
3)Go through each of the coin in the other's purse. Utilizing ArrayList's remove() method - returns "true" if an object if removed; otherwise return false.
4)If there is no same coin removed from the copied, no need to check further - false.
5)If all coins are removed, they are the same.

PS: There are other ways to check too. By the way, your way is not correct because you keep flipping the value of isSame. When you are checking for the same, keep the initial value as true first. If you see anything that is different, it is no longer true anymore. However, in this case, the order of each coin in the purse is not important, so simply check that way won't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.