/*  AddMoneyFun.java  -  Written by Craig Persiko

  This program uses 2 functions to add 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;


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

    NumberFormat moneyFormat = 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++)
      total += getCoinValue(coin_no);

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

  // Asks user to input value of coin number coin_no.

  // Returns the value of the coin in dollars.

  static double getCoinValue(int coin_no)
  {
    Scanner keyIn = new Scanner(System.in);
    int coin_val;

    /* The following loop and switch statements are kind of strange.
       I could have written them just like in the version of this
       program without functions, but I tried something different.
       If the user enters a valid coin denomination, this function
       returns the value of that coin immediately.  Since return
       statements are used in the switch statement, and return
       statements prevent the program from continuing further
       in this function, no break statements are necessary after
       each case.  For the same reason, the loop looks like an
       endless loop, but it simply loops until a value is returned.

       Some compilers might produce a warning because they don't
       see how a value is returned in every case, but you can
       ignore that warning or put in a dummy line
       (like return 0; at the end of the function) to satisfy it,
       as long as you're sure the function really works as you
       want it to in every possible case.

       I DON'T RECOMMEND WRITING YOU CODE THIS WAY,
       BUT YOU SHOULD BE ABLE TO UNDERSTAND IT.
    */
    while(true)  // Get coin value, and loop until valid entry is returned

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


      switch (coin_val)
      {
        case 1: // A dollar coin or a penny

          return get_cent_or_dollar_value();
        case 5: // A nickel

          return 0.05;
        case 10: // A dime

          return 0.10;
        case 25: // A quarter

          return 0.25;
        default:
          System.out.println("You have entered an invalid coin denomination.");
      }  // Closing brace of switch-statement on coin value.

    }  // Closing brace of while-loop

  } // closing brace of getCoinValue method



  // Pre-condition: User has specified a coin value of 1

  // Function asks user if that is one dollar or one cent

  // Function returns the value of the coin (1.0 or 0.01)

  static double get_cent_or_dollar_value()
  {
    Scanner keyIn = new Scanner(System.in);
    String input;
    boolean keep_looping;
    double value = 0;  // this initial value of 0 is needed to satisfy the compiler

                       // that a value is always assigned before it's returned.


    // The following loop and switch statement are more clear

    // than above, so I prefer this style:


    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':
          value = 0.01;
          break;
        case 'd': case 'D':
          value = 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.


    return value;
  }
}

/* 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