Skip to content

Instantly share code, notes, and snippets.

@Massay
Created February 10, 2014 02:22
Show Gist options
  • Save Massay/51a5bf094aac32b080e7 to your computer and use it in GitHub Desktop.
Save Massay/51a5bf094aac32b080e7 to your computer and use it in GitHub Desktop.
Carry out Basic Purse Functions
import java.util.*;
public class Purse {
ArrayList<String> list;
public Purse()
{
list=new ArrayList<>();
}
public void addCoin(String coinName)
{
list.add(coinName);
}
public String toString()
{
return list.toString();
}
public ArrayList<String> reverse(ArrayList<String> list)
{
ArrayList<String> myList=new ArrayList<>();
int maxValue=list.size()-1;
for(int i=0;i<list.size();i++)
{
myList.add(i,list.get(maxValue));
maxValue--;
}
return myList;
}
public void transfer(Purse other)
{
System.out.println("Fransfering....");
for(int i=0;i<list.size();i++)
{
other.addCoin(list.get(i));
}
list.clear();
if(list.isEmpty())
{
System.out.println("List is now EMPTY");
}
}
public boolean sameContents(Purse other) {
boolean theSame=false;
for(int i=0;i<list.size();i++)
{
if(list.get(i)==other.list.get(i))
{
theSame=true;
}
else
{
theSame=false;
break;
}
}
if(theSame==true)
{
System.out.println("They have the same content");
return true;
}
else
{
System.out.println("They do not have the same content");
return false;
}
}
public boolean sameCoins(Purse other)
{
int counter=0;
for(int i=0;i<list.size();i++)
{
for(int j=0;j<other.list.size();j++)
{
if(list.get(i)==other.list.get(j))
{
other.list.remove(j);
counter++;
continue;
}
}
}
if(counter!=list.size())
{
System.out.println("Coins not the same");
return false;
}
else
{
System.out.println("Coins are the same");
return true;
}
}
}
@Massay
Copy link
Author

Massay commented Feb 10, 2014

Please Leave ur comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment