/home/wpollock1/public_html/AJava/CoinPurse.java

// Demo of enums.  The enum Coin defines a type to represent all
// U.S. coins.  These are declared in the correct order, so the
// ordinal value of any coin can be compared with another.
//
// Here we create a List CoinPurse and add to it all the coins
// listed on the command line.  The the value of the coins in
// the purse are totaled and printed.  Note the use of the
// implicit enum methods "values()" (returns an array of the
// values of the enum type, in the order they were declared) and
// "valueOf(String)" (returns the enum constant with the specified
// name; throws an exception if no such constant).
// A more complete CoinPurse class would keep track of the number
// of each type of coin in the purse.
//
// Additional Java features demonstrated here include static import,
// use of collections with generics, and the "enhanced" for loop
// ("for-each" loop).
//
// Written 1/2010 by Wayne Pollock, Tampa Florida USA

import java.util.*;
import static java.lang.System.out;

enum Coin {

   PENNY( 1 ), NICKEL( 5 ), DIME( 10 ), QUARTER( 25 ),
   HALF_DOLLAR( 50 ), DOLLAR( 100 );

   private final int value;

   private Coin ( int value )  { this.value = value; }

   public int getValue() { return this.value; }
}

class CoinPurse
{
   public static void main ( String [] args ) {
      List<Coin> coinPurse = new ArrayList<Coin>();

      // Add some coins to the purse:
      for ( String coinName: args )
      {  try {
            Coin coin = Coin.valueOf( coinName.toUpperCase() );
            coinPurse.add( coin );
         } catch (IllegalArgumentException e) {
            if ( coinName.equalsIgnoreCase("help") ) {
               out.println( "Enter the names of some coins.  Legal names "
                 + "(case-insensitive) are:" );
               out.println( Arrays.toString( Coin.values() ) );
               System.exit( 0 );
            } else {
               out.println( "*** \"" + coinName
               + "\" is not legal tender!\n" );
            }
         }
      }

      int value = calcTotal( coinPurse );
      int dollars = value / 100;
      int cents   = value % 100;
      out.printf( "Value of coins in purse is $%01d.%02d.", dollars, cents );
   }

   public static int calcTotal ( List<Coin> purse ) {
      int total = 0;
      for ( Coin coin : purse )
         total += coin.getValue();
      return total;
   }
}