VendingMachine.java.html

  1  import java.util.Scanner;
  2  
  3  /**
  4     This program simulates a vending machine that gives change.
  5  */
  6  public class VendingMachine
  7  {
  8     public static void main(String[] args)
  9     {
 10        Scanner in = new Scanner(System.in);
 11  
 12        final int PENNIES_PER_DOLLAR = 100;
 13        final int PENNIES_PER_QUARTER = 25;
 14  
 15        System.out.print("Enter bill value (1 = $1 bill, 5 = $5 bill, etc.): ");
 16        int billValue = in.nextInt();
 17        System.out.print("Enter item price in pennies: ");
 18        int itemPrice = in.nextInt();
 19  
 20        // Compute change due
 21  
 22        int changeDue = PENNIES_PER_DOLLAR * billValue - itemPrice;
 23        int dollarCoins = changeDue / PENNIES_PER_DOLLAR;
 24        changeDue = changeDue % PENNIES_PER_DOLLAR;
 25        int quarters = changeDue / PENNIES_PER_QUARTER;
 26  
 27        // Print change due
 28        
 29        System.out.printf("Dollar coins: %6d", dollarCoins);
 30        System.out.println();
 31        System.out.printf("Quarters:     %6d", quarters);
 32        System.out.println();
 33     }
 34  }