package Chap3;
import java.io.*;

// CSC108 Chapter 3, Question 19
// Name:  Iam Me         Student ID: 555555555
// Tutor: Andria Hunter  Prof: Ken Jackson
//
// Program Description: Reads an integer value (some dollar
//     amount between 0 and 100), and prints out the amount of
//     change that would be received from one dollar.  The amount
//     is expressed in quarters, dimes, nickels, and pennies.
//     This program is a modification of Question 3.18 that continues
//     processing until the user enters a sentinel value of -1.

class q19 {

   // Main method to read the integer purchase amount, and to
   // determine and print the change.

   public static void main (String[] args) throws IOException {

      // Declare stdin so data can be read from input.
      DataInputStream stdin = new DataInputStream (System.in);

      final int SENTINEL = -1; // Stops processing

      final int DOLLAR = 100;  // One dollar in cents
      final int QUARTER = 25;  // One quarter in cents
      final int DIME = 10;     // One dime in cents
      final int NICKEL = 5;    // One nickel in cents

      int value;               // Integer value read from input

      // Read dollar amount from stdin.
      System.out.println ("Enter the purchase amount [0-100]: ");
      value = Integer.parseInt(stdin.readLine());

      while (value != SENTINEL) {

         // Report error if invalid integer is entered.
         if (value<0 || value>DOLLAR) {
            System.out.println ("Invalid purchase amount. Must be between 0 and 100.");
            break;
         }
   
         // Subtract the purchase amount from 1 dollar, and determine
         // the number of quarters, dimes, nickels, and pennies that
         // this change amount can be divided into.
   
         else {
            // Amount of change that would be received from one dollar
            int change = DOLLAR - value;
   
            int num_quarter=0;       // Counts number of quarters
            int num_dime=0;          // Counts number of dimes
            int num_nickel=0;        // Counts number of nickels
            int num_penny=0;         // Counts number of pennies
   
            // Use integer division to determine how many quarters, and
            // then use mod operation to determine the remaining change.
            // Repeat for dimes, nickels, and pennies.
   
            num_quarter = change / QUARTER;
            change = change % QUARTER;
   
            num_dime = change / DIME;
            change = change % DIME;
   
            num_nickel = change / NICKEL;
            change = change % NICKEL;
   
            num_penny = change;
   
            // Report change to standard out.
   
            if (DOLLAR-value == 1)
               System.out.println ("Your change of "+(DOLLAR-value)+" cent is given as:");
            else
               System.out.println ("Your change of "+(DOLLAR-value)+" cents is given as:");
   
            if (num_quarter == 1)
               System.out.println ("  "+num_quarter+" Quarter");
            else
               System.out.println ("  "+num_quarter+" Quarters");
   
            if (num_dime == 1)
               System.out.println ("  "+num_dime+" Dime");
            else
               System.out.println ("  "+num_dime+" Dimes");
   
            if (num_nickel == 1)
               System.out.println ("  "+num_nickel+" Nickel");
            else
               System.out.println ("  "+num_nickel+" Nickels");
   
            if (num_penny == 1)
               System.out.println ("  "+num_penny+" Penny");
            else
               System.out.println ("  "+num_penny+" Pennies");
         }

      // Read next dollar amount from stdin.
      System.out.println ("\nEnter next purchase amount [0-100]: ");
      value = Integer.parseInt(stdin.readLine());

      }
   }
}

