/*  AddMoney.java  -  Written by Craig Persiko

  This program adds up the total amount of money the user has.
  It does so by asking the user how many coins she has
  and the value of each coin.
  Erroneous entries are caught and the user is
  offered the chance to re-enter.
*/

import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;

class AddMoney
{
  public static void main(String args[])
  {
    Scanner keyIn = new Scanner (System.in);
    int coin_no, num_coins, coin_val;
    double total = 0.0;
    boolean invalid_coin;
    String input;

    NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(Locale.US);

    do
    {
      System.out.print("How many coins do you have? ");
      num_coins = keyIn.nextInt();
    }while(num_coins < 0);

    for(coin_no=1; coin_no<=num_coins; coin_no++)
    {
      do  // Get coin value, and loop if entry is invalid.
      {
        invalid_coin = false;    // Used to indicate if coin value needs to be re-entered.

        System.out.print("Enter the integer value of coin #" + coin_no + ": ");
        coin_val = keyIn.nextInt();
        keyIn.nextLine(); // dispose of the newline character

        switch (coin_val)
        {
          case 1: // A dollar coin or a penny
            // The following variable will only be declared in this case,
            // and it will exist only inside this switch block.
            boolean keep_looping;

            do  // Ask if penny or dollar, and loop if entry is invalid.
            {
              keep_looping = false; // Assume entry is valid

              System.out.print("Is that one cent or one dollar? Enter c or d: ");
              input = keyIn.nextLine();
              switch (input.charAt(0)) // switch on first char in input string.
              {
                case 'c': case 'C':
                  total += 0.01;
                  break;
                case 'd': case 'D':
                  total += 1.00;
                  break;
                default:
                  System.out.println("You must specify c or d. Please re-enter.");
                  keep_looping = true;
              }
            }while(keep_looping);  // Loop if c or d not entered.
            break;

          case 5: // A nickel
            total += 0.05;
            break;
          case 10: // A dime
            total += 0.10;
            break;
          case 25: // A quarter
            total += 0.25;
            break;
          default:
            System.out.println("You have entered an invalid coin denomination.");
            invalid_coin = true;
        }  // Closing brace of switch-statement on coin value.

      }while(invalid_coin);  // Loop if coin denomination invalid.

    } // Closing brace of for-loop (loops for each coin)

    System.out.println("You have a total of "
                       + dollarFormat.format(total) + " in coins.");
  }
}

/* Sample Output:

How many coins do you have? -5
How many coins do you have? -1
How many coins do you have? 3
Enter the integer value of coin #1: 20
You have entered an invalid coin denomination.
Enter the integer value of coin #1: 12
You have entered an invalid coin denomination.
Enter the integer value of coin #1: 10
Enter the integer value of coin #2: 25
Enter the integer value of coin #3: 1
Is that one cent or one dollar? Enter c or d: c
You have a total of $0.36 in coins.


How many coins do you have? 4
Enter the integer value of coin #1: 1
Is that one cent or one dollar? Enter c or d: 1
You must specify c or d. Please re-enter.
Is that one cent or one dollar? Enter c or d: d
Enter the integer value of coin #2: 1
Is that one cent or one dollar? Enter c or d: C
Enter the integer value of coin #3: 5
Enter the integer value of coin #4: 25
You have a total of $1.31 in coins.

*/


syntax highlighted by Code2HTML, v. 0.9