Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Calculate the Change Programming challenge description: The goal of this question is to design a cash register program. Your register currently has the following

Java

Calculate the Change

Programming challenge description:

The goal of this question is to design a cash register program. Your register currently has the following notes/coins within it:

  • One Pence: .01
  • Two Pence: .02
  • Five Pence: .05
  • Ten Pence: .10
  • Twenty Pence: .20
  • Fifty Pence: .50
  • One Pound: 1
  • Two Pounds: 2
  • Five Pounds: 5
  • Ten Pounds: 10
  • Twenty Pounds: 20
  • Fifty Pounds: 50

The aim of the program is to calculate the change that has to be returned to the customer with the least number of coins/notes. Note that the expectation is to have an object-oriented solution - think about creating classes for better reusability.

Input:

Your program should read lines of text from standard input (this is already part of the initial template). Each line contains two numbers which are separated by a semicolon. The first is the Purchase price (PP) and the second is the cash(CH) given by the customer.

Output:

For each line of input print a single line to standard output which is the change to be returned to the customer. If CH == PP, print out Zero. If CH > PP, print the amount that needs to be returned (in terms of the currency values). Any other case where the result is an error, the output should be ERROR.

The output should change from highest to lowest denomination.

Test 1

Test 1 Input

11.25

20

Expected Output

Five Pounds, Two Pounds, One Pound, Fifty Pence, Twenty Pence, Five Pence

Test 2

Test 2 Input

8.75

50

Expected Output

Twenty Pounds, Twenty Pounds, One Pound, Twenty Pence, Five Pence

Test 3

Test InputDownload Test 3 Input

20

10

Expected OutputDownload Test 3 Input

ERROR

Code below:

please fill the functions and or classes here. thank you for help.

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets;

public class Main { /** * Iterate through each line of input. */ public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(reader);

try { double purchasePrice = Double.parseDouble(in.readLine()); double cash = Double.parseDouble(in.readLine()); Main.calculateChange(purchasePrice, cash); } catch (Exception e) { System.out.println(e); } }

public static void calculateChange(double purchasePrice, double cash) { // Access your code here. Feel free to create other classes as required }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain why and how imagery can be polysensory.

Answered: 1 week ago