Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 3 And now for the fun part, the challenge. This challenge is asking you to

ID: 3755903 • Letter: P

Question

Part 3 And now for the fun part, the challenge. This challenge is asking you to write a program that will simulate a cash register. Ask for the price of an item, the payment amount from the customer, and then print out the change due to the customer but print it out as dollars, the number of quarters, the number of dimes, the number of nickels, and the number of pennies. Hint, this is a good opportunity to work with modules or remainder division. Also allow the user to enter multiple transactions. You have 1 hour and a half to complete this task, so take your time and have fun. After you write the program and test it try to write an explanation to tell us how did you solve your problem, using your own words.

Explanation / Answer

Since you didn’t specify the language. Here is the completed code for this problem in Java. Comments are included, go through it, learn how things work and let me know if you have any doubts. If you need this in a different language, let me know. Also the logic stays the same for any other languages. If you are satisfied with the solution, please upvote the answer. Thanks

// CashRegister.java

import java.util.Scanner;

public class CashRegister {

      /**

      * method to display the change

      * @param itemPrice - price of item

      * @param payment payment in dollars

      */

      static void displayChange(double itemPrice, double payment) {

            if (payment < itemPrice) {

                  System.out.println("Payment cannot be smaller than item price!");

            } else {

                  // finding the change

                  double change = (payment - itemPrice);

                  // converting to cents

                  change *= 100;

                  // extracting dollars

                  int dollars = (int) (change / 100);

                  // subtracting dollars

                  change -= dollars*100;

                  // finding the remaining, here Math.round() is used because of the

                  // weird precision troubles in Java that occur sometimes

                  int remaining = (int) Math.round(change);

                  //finding the quarters (25 cents)

                  int quarters = remaining / 25;

                  //subtracting the quarters

                  remaining = remaining % 25;

                  //finding the dimes (10 cents)

                  int dimes = remaining / 10;

                  //subtracting the dimes

                  remaining = remaining % 10;

                  //finding the nickels (5 cents)

                  int nickels = remaining / 5;

                  //finding the remaning as pennies

                  int pennies = remaining % 5;

                  //displaying change

                  System.out.printf("Here's your change: %d dollars, %d quarters,"

                              + " %d dimes, %d nickels and %d pennies ", dollars,

                              quarters, dimes, nickels, pennies);

            }

      }

      public static void main(String[] args) {

            //scanner to read input

            Scanner scanner = new Scanner(System.in);

            //initializing variables

            double itemPrice = 0, paymentAmount;

            //looping until a negative value is entered

            while (itemPrice >= 0) {

                  System.out.print("Enter the item price (or -1 to quit): ");

                  itemPrice = scanner.nextDouble();

                  if (itemPrice >= 0) {

                        //getting payment amount in dollars

                        System.out.print("Enter the payment amount: ");

                        paymentAmount = scanner.nextDouble();

                        //displaying the change

                        displayChange(itemPrice, paymentAmount);

                  }

            }

      }

}

/*OUTPUT*/

Enter the item price (or -1 to quit): 4.87

Enter the payment amount: 5

Here's your change: 0 dollars, 0 quarters, 1 dimes, 0 nickels and 3 pennies

Enter the item price (or -1 to quit): 3.22

Enter the payment amount: 4

Here's your change: 0 dollars, 3 quarters, 0 dimes, 0 nickels and 3 pennies

Enter the item price (or -1 to quit): 12

Enter the payment amount: 15

Here's your change: 3 dollars, 0 quarters, 0 dimes, 0 nickels and 0 pennies

Enter the item price (or -1 to quit): 12.50

Enter the payment amount: 15

Here's your change: 2 dollars, 2 quarters, 0 dimes, 0 nickels and 0 pennies

Enter the item price (or -1 to quit): -1