Created
February 10, 2014 02:22
-
-
Save Massay/51a5bf094aac32b080e7 to your computer and use it in GitHub Desktop.
Carry out Basic Purse Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please Leave ur comments