public class OwnedPiggyBank extends Object
Only the owner of the piggy bank is able to remove coins from the piggy bank.
Constructor and Description |
---|
OwnedPiggyBank(OwnedPiggyBank other)
Initializes this piggy bank by copying another piggy bank.
|
OwnedPiggyBank(Owner owner)
Initializes this piggy bank so that it has the specified owner
and no coins.
|
Modifier and Type | Method and Description |
---|---|
void |
add(List<Coin> coins)
Adds the specified coins to this piggy bank.
|
boolean |
contains(Coin coin)
Returns true if this piggy bank contains the specified coin, and false
otherwise.
|
List<Coin> |
deepCopy()
Returns a deep copy of the coins in this piggy bank.
|
Owner |
getOwner()
Returns the owner of this piggy bank.
|
int |
numberOf(Coin coin)
Counts the number of coins equal to the specified coin
in this piggy bank.
|
Coin |
remove(Owner user,
Coin coin)
Allows the owner of this piggy bank to remove a coin equal to the value
of the specified coin from this piggy bank.
|
List<Coin> |
removeCoins(Owner user,
int value)
Allows the owner of this piggy bank to remove
the smallest number of coins whose total value in cents is equal
to the specified value in cents from this piggy bank.
|
public OwnedPiggyBank(Owner owner)
owner
- the owner of this piggy bankpublic OwnedPiggyBank(OwnedPiggyBank other)
other
- the piggy bank to copypublic Owner getOwner()
public void add(List<Coin> coins)
coins
- a list of coins to add to this piggy bankpublic boolean contains(Coin coin)
coin
- a coinpublic Coin remove(Owner user, Coin coin)
If the specified user is not equal to the owner of this piggy bank, then the coin is not removed from this piggy bank, and null is returned.
user
- the person trying to remove the coincoin
- a coinpublic List<Coin> removeCoins(Owner user, int value)
Returns the empty list if the specified user is not equal to the owner of this piggy bank.
user
- the person trying to remove coins from this piggy bankvalue
- a value in centspublic List<Coin> deepCopy()
public int numberOf(Coin coin)
NOTE TO STUDENTS: You should create a private static helper method that recursively counts the number of coins in a specified list, array, or map depending on how you chose to implement the piggy bank. This method should then call the recursive method to get the required count.
coin
- a coin