简体   繁体   中英

How to use remainder to find the minimum number of bills or coins Dr Java

I am writing a program that takes user input of food order and amount paid and prompts the amount of currency units to be doled out. If the bill is 30.89 and the after tax bill is 31.89 than the expected result is:

$20: 1
$10: 1
$5: 0 

.. and so on till the last penny.

So basically i tried to find the minimum bills needed to give for change but I kept getting a really long decimal instead of integers. I tried formatting it but it wont work. Do I have to change my variable conventions (eg int to double or double to int??) if so which ones? Here is what I have implemented:

Welcome to Sonia Shawarma!
Price List
Cheeseburger: $4.69
Falafel in a pita: $6.00

Poutine: $4.50

import java.util.Scanner;
public class PointOfSale
{
  public static void main(String [] args)

//declaring variables
    int burgers;
    int poutines;
    int falafels;
    double burger = 4.69;
    double poutine = 4.50;
    double falafel = 6.00;
    double item1;
    double item2;
    double item3;
    double totalbefore_tax;
    final double TAX;
    double tax_total;
    double total_final;
    double rounded_final;
    double money_paid;
    int twenties ;
    int tens;
    int fives;
    int toonies;
    int loonies;
    double quarters;
    double dimes;
    double nickels;
    double change1;
    double change2;
    double amount_bills20;
    double amount_bills10;
    double amount_bills5;
    double dollars2;
    double dollars1;
    double cents25;
    double cents10;
    double cents5;
    String date1;
    String cashier1;
    Scanner input = new Scanner(System.in);


    System.out.println("Welcome to Sonia Shawarma!");
    System.out.println("Price List");
    System.out.println("Cheeseburger: $4.69");
    System.out.println("Falafel in a pita: $6.00");
    System.out.println("Poutine: $4.50");

    //prompt user for number cheeseburgers,poutines,fries
    System.out.println("Enter the number of Cheeseburgers:");
    burgers = input.nextInt();
    System.out.println("Enter the number of Falafels:");
    falafels = input.nextInt();
    System.out.println("Enter the number of Poutines:");
    poutines = input.nextInt();

    //calculating
    item1 = (burger*burgers);
    item2 = (falafel*falafels);
    item3 = (poutine*poutines);
    totalbefore_tax = (item1+(item2)+(item3));
    TAX = 0.13;
    tax_total = totalbefore_tax*TAX;
    total_final = totalbefore_tax*1.13;
    rounded_final = 0.05*(Math.round(total_final/0.05));

    //total
    System.out.format("%-5s%-3.2f\n", "Total before tax: ",totalbefore_tax);
    System.out.format("%-5s%-3.2f\n", "Tax:", tax_total);
    System.out.format("%-5s%-3.2f\n","Final total:", rounded_final);
    System.out.format("%-5s%-3.2f\n", "Please pay:", rounded_final);

   //prompt user for twenties to nickels
    System.out.println("Enter the number of $20:");
    twenties = input.nextInt();
    System.out.println("Enter the number of $10:");
    tens = input.nextInt();
    System.out.println("Enter the number of $5:");
    fives = input.nextInt();
    System.out.println("Enter the number of Toonies($2):");
    toonies = input.nextInt();
    System.out.println("Enter the number of Loonies($1):");
    loonies = input.nextInt();
    System.out.println("Enter the number of Quarters($0.25):");
    quarters = input.nextDouble();
    System.out.println("Enter the number of Dimes($0.10):");
    dimes = input.nextDouble();
    System.out.println("Enter the number of Nickels($0.05):");
    nickels = input.nextDouble();

    //prompt money paid
    System.out.println("Customer paid:");
    money_paid = input.nextDouble();

    change1 = money_paid - rounded_final;

    System.out.format("%-5s%-3.2f\n","The change is:", change1);

    change2 = (change1*100);

    System.out.println("The minimum number of bills or coins is:");


    //calculating minumum change
    amount_bills20 = change2/2000;
    change2 = change2%2000;
    amount_bills10 = change2/1000;
    change2 = change2%1000;
    amount_bills5 = change2/500;
    change2 = change2%500;
    dollars2 = change2/200;
    change2 = change2%200;
    dollars1 = change2/100;
    change2 = change2%100;
    cents25 = change2/25;
    change2 = change2%25;
    cents10 = change2/10;
    change2 = change2%10;
    cents5  = change2/5;
    change2 = change2%5;

    //minimum number of bills needed
    System.out.println("$20:" + amount_bills20);
    System.out.println("$10:" + amount_bills10);
    System.out.println("$5:" + amount_bills5);
    System.out.println("Toonies:" + dollars2);
    System.out.println("Loonies:" + dollars1);
    System.out.println("Quarters:" + cents25);
    System.out.println("Dimes:" + cents10);
    System.out.println("Nickels:" + cents5);

    System.out.println("Printing receipt...");

The output i get is Welcome to Sonia Shawarma!

Price List
Cheeseburger: $4.69
Falafel in a pita: $6.00
Poutine: $4.50
Enter the number of Cheeseburgers:
 [2]
Enter the number of Falafels:
 [1]
Enter the number of Poutines:
 [1]
Total before tax: 19.88
Tax: 2.58
Final total:22.45
Please pay:22.45
Enter the number of $20:
 [1]
Enter the number of $10:
 [1]
Enter the number of $5:
 [DrJava Input Box]
Enter the number of Toonies($2):
 [DrJava Input Box]
Enter the number of Loonies($1):
 [DrJava Input Box]
Enter the number of Quarters($0.25):
 [DrJava Input Box]
Enter the number of Dimes($0.10):
 [DrJava Input Box]
Enter the number of Nickels($0.05):
 [DrJava Input Box]
Customer paid:
 [30.00]
The change is:7.55
The minimum number of bills or coins is:
$20:0.3774999999999999
$10:0.7549999999999998
$5:1.5099999999999996
Toonies:1.2749999999999988
Loonies:0.5499999999999977
Quarters:2.199999999999991
Dimes:0.49999999999997724
Nickels:0.9999999999999545
Printing receipt...
Sonia Shawarma

If you want to work with double, you need to work with DecimalFormat to restrict the decimal fields as explained in: How to round a number to n decimal places in Java

DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.CEILING);
System.out.println((total_bill/20));
System.out.println(df.format(total_bill/20));

output: 1.8829999999999998 and 1.89

The other part is that your number of notes is decimal, which ofcourse is not acceptable. Since they are double, you need to cast them into int to get a whole number

double total_bill = new Double(21.6643);
System.out.println("20$ notes:" +(int)total_bill/20); 
// casting to int gives the quotient integer. Output is 20$ notes:1
System.out.println("Remainder:" + df.format(total_bill%20));
//Output is: Remainder:1.664.

You then use this logic on all other notes and coins where the remainder becomes your total as you were doing or use a loop.

For the assignment this may be acceptable but in real world scenarios, you may need to use BigDecimal instead of double. And as always, stackoverflow has a wonderful post for that: Double vs. BigDecimal? .

However BigDecimal doesnt overload the normal math operators and has functions for all operations:

//retpre : is a BigDecimal - Total cash to return
System.out.println("20$ notes: " + retpre.divideToIntegralValue(new BigDecimal("20")).intValue());
BigDecimal tens =retpre.remainder(new BigDecimal("20"),mc);
//mc is MathContext to define precision and rounding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM