Write a third class called Print that will be used to output information. Complete the class TestPurse.
import javax.swing.JOptionPane;
class Coin
{
//Set up variables for each denomination
private int count;
private String name;
Coin(String name, int count)
{
//Create a new Coin object
this.count = count;
this.name = name;
}
String nameOfCoin()
{
//Implementation - Return the name of the coin
return name;
}
int getValue()
{
//Implementation - Return the amount of money value of the coin
return count;
}
}
class Purse
{
private static final double NICKEL_VALUE = 0.05;
private static final double DIME_VALUE = 0.10;
private static final double QUARTER_VALUE = 0.25;
// Complete the declaration of the constants representing the coins
private double total;
Purse()
{
total = 0;
}
public void add(Coin aCoin)
{
// Calculate the amount of money in the purse.
if (aCoin.nameOfCoin().equalsIgnoreCase("NICKEL"))
total = total + aCoin.getValue()*NICKEL_VALUE;
else if (aCoin.nameOfCoin().equalsIgnoreCase("DIME"))
total = total + aCoin.getValue()* DIME_VALUE;
else if (aCoin.nameOfCoin().equalsIgnoreCase("QUARTER"))
total = total + aCoin.getValue()* QUARTER_VALUE;
}
double getTotal()
{
return total;
}
}
class Print
{
{
System.out.println(myPurse.nameOfCoin() + myPurse.getValue()+"amount of money is"+ myPurse.getTotal());
}//error, but why?
}
/**
This program tests the Purse class.
*/
class TestPurse
{
public static void main(String[] args)
{
// Create an empty Purse object here
Purse myPurse = new Purse ();
boolean done = false;
while (!done)
{
String input = JOptionPane.showInputDialog("Enter name of coin or click the Cancel button");
if (input.equals(""))
done = true;
else
{
input = JOptionPane.showInputDialog("Enter number of coins");
int numberOfCoin = Integer.parseInt(input);
// Create coin object
Coin myCoin = new Coin(input, numberOfCoin);
// Add coin object to purse
myPurse.add(myCoin);
/* Pass the coin object to the print class to
(1)To print the name of each coin, and
(2)the total value of each coin object
*/
}
}
// Use the class Print to print the total amount of money in the purse.
Bookmarks