Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 enum change {
- fiftyPounds(5000, "Fifty Pounds"),
- twentyPounds(2000, "Twenty Pounds"),
- tenPounds(1000, "Ten Pounds"),
- fivePounds(500, "Five Pounds"),
- twoPounds(200, "Two Pounds"),
- onePound(100, "One Pound"),
- fiftyPence(50, "Fifty Pence"),
- twentyPence(20, "Twenty Pence"),
- tenPence(10, "Ten Pence"),
- fivePence(5, "Five Pence"),
- twoPence(2, "Two Pence"),
- onePence(1, "One Pence");
- private final int denom;
- private final String price;
- change(int denomination, String value){
- int denom = denomination;
- String price = value;
- }
- public int getDenom(){
- return denom;
- }
- public String getPrice(){
- return price;
- }
- }
- public static void calculateChange(double purchasePrice, double cash) {
- int purchasePriceIn = (int) purchasePrice * 100;
- int cashIn = (int) cash * 100;
- /**
- int changeOwed = 0;
- int finalDenom = 0;
- int denomPrintAmount = 0;
- String printState = ""
- String addValue
- */
- String finalStatement = "";
- if(purchasePriceIn < cashIn){
- int changeOwed = cashIn - purchasePriceIn;
- for(change money : change.values()){
- int finalDenom = (int) money.getDenom();
- if(finalDenom <= changeOwed){
- int denomPrintAmount = (int) Math.floor(changeOwed/finalDenom);
- changeOwed -= denomPrintAmount * finalDenom;
- if(denomPrintAmount > 0){
- String printState = (String) money.getPrice() + ", ";
- String addValue = printState.repeat(denomPrintAmount);
- finalStatement = finalStatement + addValue;
- }
- }
- }
- System.out.println("Here");
- System.out.println(finalStatement);
- }
- else if(purchasePriceIn == cashIn){
- System.out.println("Zero");
- }
- else{
- System.out.println("ERROR");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement