My assignment is as follows.
Create a class called PursePile that can hold any number of Purse objects. The class should have the following methods:
public void add(Purse p) //adds a purse to the pile
public Purse findAPursesWith(String coinName) //searches the pile until a purse with the given coin is found
public Purse findWithMostCoins() //returns the purse in the pile that has the most coins in it. Does not consider the value of the coins
Create another class called PileTester that, when run, creates a PursePile, adds some purses, and tests all of the methods above.
Use the Purse class you created for the previous assignment. You can modify this class as needed, including adding methods to it.
Here is what I have:
*The bolded section is where Im getting an error.
Please help me figure out how to correct it.*
import java.util.ArrayList;
import java.util.Collections;
/**
A purse holds a collection of coins.
*/
public class Purse
{
/**
Constructs an empty purse.
*/
private ArrayList<String> coins;
private String coinName;
public Purse()
{
coins = new ArrayList<String>();
}
public void setCoinName(String coinName)
{
this.coinName = coinName;
}
public String getCoinName()
{
return coinName;
}
/**
Adds a coin to the purse.
@param coinName the coin to add
*/
public void addCoin(String coinName)
{
coins.add(coinName);
}
//Returns how many coins a purse has.
public int count()
{
return coins.size();
}
/**
Returns a string describing the object.
@return a string in the format "Purse[coinName1,coinName2,...]"
*/
public String toString()
{
String returnString = "Purse[";
if(coins.size() == 0)
{
return returnString + "]";
}
returnString += coins.get(0) + "";
for(int i = 1; i < coins.size(); i++)
{
returnString += "," + coins.get(i);
}
return returnString + "]";
}
/**
Determines if a purse has the same coins in the same or different
order as another purse.
@param other the other purse
@return true if the two purses have the same coins in the
same or different order, false otherwise
*/
public boolean sameCoins(Purse other){
if(this.coins.size() != other.coins.size())
{
return false;
}
//Must make a copy before sorting
ArrayList<String> thisOne = new ArrayList<String>(this.coins);
ArrayList<String> otherOne = new ArrayList<String>(other.coins);
//Sort the collection
Collections.sort(thisOne);
Collections.sort(otherOne);
for(int i = 0; i < this.coins.size(); i++)
{
if(!thisOne.get(i).equals(otherOne.get(i)))
{
return false;
}
}
return true;
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class PursePile {
private ArrayList<Purse> purses;
private ArrayList<String> coins;
public PursePile()
{
purses = new ArrayList<Purse>();
coins = new ArrayList<String>();
}
//adds a purse to the pile
public void add(Purse p)
{
purses.add(p);
}
//searches the pile until a purse with the given coin is found
public Purse findAPursesWith(String coinName)
{
for(Purse p: purses)
{
[B]if(p.getCoinName().equalsIgnoreCase(coinName))[/B]
return p;
}
return null;
}
//
// String x;
//
// for (int i = 0; i < coins.size(); i++)
// {
//
// x = coins.get(i);
// if(x.equals(coinName))
// {
// return x;
// }
//
// }
// return null;
//returns the purse in the pile that has the most coins in it. Does not consider the value of the coins
public Purse findWithMostCoins()
{
if(purses.size() == 0) return null;
Purse mostCoins = purses.get(0);
for(int i = 1; i < purses.size(); i++)
{
Purse p = purses.get(i);
if (p.count() > mostCoins.count())
mostCoins = p;
}
return mostCoins;
}
}
import java.util.Scanner;
public class PileTester {
/**
This class tests the PursePile class.
*/
public static void main(String[] args)
{
String input;
//Brings all of the purses together.
Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
a.addCoin("Nickel");
a.addCoin("Dime");
Purse b = new Purse();
b.addCoin("Nickel");
b.addCoin("Dime");
b.addCoin("Dime");
b.addCoin("Quarter");
Purse c = new Purse();
c.addCoin("Quarter");
c.addCoin("Penny");
c.addCoin("Nickel");
c.addCoin("Dime");
Purse d = new Purse();
d.addCoin("Nickel");
d.addCoin("Dime");
d.addCoin("Dime");
d.addCoin("Quarter");
//Creates a PursePile
PursePile pile = new PursePile();
pile.add(a);
pile.add(b);
pile.add(c);
pile.add(d);
//Creates a Scanner class for user input
Scanner keyboard = new Scanner(System.in);
System.out.println("Which coin would you like to find?: \nPlease enter either Quarter, Dime, Nickel, or Penny.");
input = keyboard.nextLine();
[B]System.out.println(pile.findAPursesWith(input));[/B]
System.out.println(pile.findWithMostCoins());
}
}