Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package make_change;
- import java.util.*;
- public class Makechange {
- public static Scanner scanner = new Scanner(System.in);
- public static int totalchange = 0;
- public static void main(String[] args) {
- //Get values
- int pricedollars = getInt("Price dollars: ") * 100;
- int pricecents = getInt("Price cents: ");
- int givendollars = getInt("Given dollars: ") * 100;
- int givencents = getInt("Given cents: ");
- totalchange = (givendollars + givencents) - (pricedollars + pricecents);
- System.out.println("");
- //Calculate
- if(totalchange < 0) {
- System.out.println("Not given enough money!");
- } else {
- System.out.println("Dollar(s): " + getAmount(100));
- System.out.println("Quarter(s): " + getAmount(25));
- System.out.println("Dime(s): " + getAmount(10));
- System.out.println("Nickel(s): " + getAmount(5));
- System.out.println("Pennie(s): " + totalchange);
- }
- }
- //Gets output from the console and returns it
- public static int getInt(String print) {
- System.out.print(print);
- return scanner.nextInt();
- }
- //Gets the amount based on the type of change. Also subtracts from totalchange
- public static int getAmount(int type) {
- int amount = totalchange / type;
- totalchange = totalchange - (amount * type);
- return amount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement