Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. package make_change;
  2. import java.util.*;
  3.  
  4. public class Makechange {
  5.     public static Scanner scanner = new Scanner(System.in);
  6.     public static int totalchange = 0;
  7.  
  8.     public static void main(String[] args) {
  9.         //Get values
  10.         int pricedollars = getInt("Price dollars: ") * 100;
  11.         int pricecents = getInt("Price cents: ");
  12.         int givendollars = getInt("Given dollars: ") * 100;
  13.         int givencents = getInt("Given cents: ");
  14.         totalchange = (givendollars + givencents) - (pricedollars + pricecents);
  15.         System.out.println("");
  16.        
  17.         //Calculate
  18.         if(totalchange < 0) {
  19.             System.out.println("Not given enough money!");
  20.         } else {
  21.             System.out.println("Dollar(s): " + getAmount(100));
  22.             System.out.println("Quarter(s): " + getAmount(25));
  23.             System.out.println("Dime(s): " + getAmount(10));
  24.             System.out.println("Nickel(s): " + getAmount(5));
  25.             System.out.println("Pennie(s): " + totalchange);
  26.         }
  27.     }
  28.    
  29.     //Gets output from the console and returns it
  30.     public static int getInt(String print) {
  31.         System.out.print(print);
  32.         return scanner.nextInt();
  33.     }
  34.    
  35.     //Gets the amount based on the type of change. Also subtracts from totalchange
  36.     public static int getAmount(int type) {
  37.         int amount = totalchange / type;
  38.         totalchange = totalchange - (amount * type);
  39.         return amount;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement