D
dbstyless
The programming assignment is to implement a class Purse. A purse
contains a collection of coins. Each coin object must contain its
name. You should not put a limit on the number of coins that a purse
can hold. This program should provide the user with the ability to
display the contents of the purse, add coins, count coins, calculate
the amount of money in purse and for extra credit, spend coins. You
will need 2 Java Object Classes: one to define the Coin objects, and
one for the Purse object.
There is a sample PurseTester class and its output. You are required
to use this class to test your code but you may make modifications to
the method calls to match your method names.
public class PurseTester {
public static void main(String[] args)
{
Purse myPurse = new Purse();
System.out.println("Created my purse!");
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
System.out.println("Trying to add an invalid coin called
Dollar...");
myPurse.addCoin(new Coin("Dollar"));
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
System.out.println("Adding coins to purse");
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.NICKEL));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.QUARTER));
myPurse.addCoin(new Coin(Coin.QUARTER));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) +
" pennies, " +
myPurse.countCoin(Coin.NICKEL)
+ " nickels, " +
myPurse.countCoin(Coin.DIME) +
" dimes, and " +
myPurse.countCoin(Coin.QUARTER)
+ "quarters.");
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
/* extra credit from here on down */
System.out.println("Attempting to spend dime that you don't have.");
if (myPurse.spendCoin(Coin.DIME))
System.out.println(Coin.DIME + " was spent!");
else
System.out.println("No " + Coin.DIME + " was found in
purse!");
System.out.println("\nAdding a dime.");
myPurse.addCoin(new Coin(Coin.DIME));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) +
" pennies, " +
myPurse.countCoin(Coin.NICKEL)
+ " nickels, " +
myPurse.countCoin(Coin.DIME) +
" dimes, and " +
myPurse.countCoin(Coin.QUARTER)
+ "quarters.");
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
System.out.println("Spending all my money...");
myPurse.spendCoin(Coin.DIME);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.NICKEL);
myPurse.spendCoin(Coin.PENNY);
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
}
}
Output from PurseTester
Created my purse!
My Purse = Purse[]
Total value = $0.00
Trying to add an invalid coin called Dollar...
My Purse = Purse[]
Total value = $0.00
Adding coins to purse
My Purse = Purse[Penny,Nickel,Penny,Penny,Quarter,Quarter]
I have 3 pennies, 1 nickels, 0 dimes, and 2 quarters.
Total value = $0.58
Attempting to spend dime that you don't have.
No Dime was found in purse!
Adding a dime.
My Purse = Purse[Penny,Nickel,Penny,Penny,Quarter,Quarter,Dime]
I have 3 pennies, 1 nickels, 1 dimes, and 2 quarters.
Total value = $0.68
Spending all my money...
My Purse = Purse[]
Total value = $0.00
contains a collection of coins. Each coin object must contain its
name. You should not put a limit on the number of coins that a purse
can hold. This program should provide the user with the ability to
display the contents of the purse, add coins, count coins, calculate
the amount of money in purse and for extra credit, spend coins. You
will need 2 Java Object Classes: one to define the Coin objects, and
one for the Purse object.
There is a sample PurseTester class and its output. You are required
to use this class to test your code but you may make modifications to
the method calls to match your method names.
public class PurseTester {
public static void main(String[] args)
{
Purse myPurse = new Purse();
System.out.println("Created my purse!");
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
System.out.println("Trying to add an invalid coin called
Dollar...");
myPurse.addCoin(new Coin("Dollar"));
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
System.out.println("Adding coins to purse");
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.NICKEL));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.QUARTER));
myPurse.addCoin(new Coin(Coin.QUARTER));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) +
" pennies, " +
myPurse.countCoin(Coin.NICKEL)
+ " nickels, " +
myPurse.countCoin(Coin.DIME) +
" dimes, and " +
myPurse.countCoin(Coin.QUARTER)
+ "quarters.");
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
/* extra credit from here on down */
System.out.println("Attempting to spend dime that you don't have.");
if (myPurse.spendCoin(Coin.DIME))
System.out.println(Coin.DIME + " was spent!");
else
System.out.println("No " + Coin.DIME + " was found in
purse!");
System.out.println("\nAdding a dime.");
myPurse.addCoin(new Coin(Coin.DIME));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) +
" pennies, " +
myPurse.countCoin(Coin.NICKEL)
+ " nickels, " +
myPurse.countCoin(Coin.DIME) +
" dimes, and " +
myPurse.countCoin(Coin.QUARTER)
+ "quarters.");
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
System.out.println("Spending all my money...");
myPurse.spendCoin(Coin.DIME);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.NICKEL);
myPurse.spendCoin(Coin.PENNY);
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
}
}
Output from PurseTester
Created my purse!
My Purse = Purse[]
Total value = $0.00
Trying to add an invalid coin called Dollar...
My Purse = Purse[]
Total value = $0.00
Adding coins to purse
My Purse = Purse[Penny,Nickel,Penny,Penny,Quarter,Quarter]
I have 3 pennies, 1 nickels, 0 dimes, and 2 quarters.
Total value = $0.58
Attempting to spend dime that you don't have.
No Dime was found in purse!
Adding a dime.
My Purse = Purse[Penny,Nickel,Penny,Penny,Quarter,Quarter,Dime]
I have 3 pennies, 1 nickels, 1 dimes, and 2 quarters.
Total value = $0.68
Spending all my money...
My Purse = Purse[]
Total value = $0.00