UNB/ CS/ David Bremner/ teaching/ java/ Coins6.java
public class Coins6
{  public static void main(String[] args)
   {  Purse thePurse = new Purse();
      ConsoleReader console = new ConsoleReader(System.in);

      Coin coin1 = new Coin(0.01, "pennies");
      Coin coin2 = new Coin(0.05, "nickels");
      Coin coin3 = new Coin(0.10, "dimes");
      Coin coin4 = new Coin(0.25, "quarters");

      System.out.println("How many " + coin1.getName() 
         + " do you have?");
      int coin1Count = console.readInt();         

      System.out.println("How many " + coin2.getName() 
         + " do you have?");
      int coin2Count = console.readInt();         

      System.out.println("How many " + coin3.getName() 
         + " do you have?");
      int coin3Count = console.readInt();         

      System.out.println("How many " + coin4.getName() 
         + " do you have?");
      int coin4Count = console.readInt();         

      thePurse.addCoins(coin1Count, coin1);
      thePurse.addCoins(coin2Count, coin2);
      thePurse.addCoins(coin3Count, coin3);
      thePurse.addCoins(coin4Count, coin4);
            
      System.out.println("The total value is $" 
         + thePurse.getTotal());
   }
}

class Coin
{  // a constructor for constructing a coin 

   public Coin(double aValue, String aName)
   {  value = aValue;
      name = aName;
   }

   // methods to access the value and name

   public double getValue()
   {  return value;
   }

   public String getName()
   {  return name;
   }

   // instance variables to store the value and name
 
   private double value;
   private String name;
}

class Purse
{  // default constructor makes purse with zero total

   public Purse()
   {  total = 0;
   }

   // methods to add coins and to get the total

   public void addCoins(int coinCount, Coin coinType)
   {  double value = coinCount * coinType.getValue();
      total = total + value;
   }

   public double getTotal()
   {  return total;
   }

   // instance variable to store the total

   private double total;
}