+1 (315) 557-6473 

Program To Build Foreign Currency and Perfect Change, Using Java Programming Language Assignment Solutions.


Instructions

Objective
Write a java assignment program to build foreign currency and perfect change.

Requirements and Specifications

At present, you have decided only to trade in the following currencies: GBP (British Pounds), EUR (Common Market Euros), and JPY (Japanese Yen). When it starts, your program will ask for the current exchange rates, after which it will begin a ‘valuation loop’ that lets you select the currency type and then enter the amount of that currency, whereupon the program tells you how much that would be in US dollars. A sample run would look like this (items in bold are user inputs):
In this program (closely related to the MakeChange calculator done previously) you will make change for a dollar from a specific number of available coins. To do this, a method that runs at the beginning will ask the user for a specific set of coins to use. Note that in part B, you will incorporate the MakeChange algorithm by making the choice of using a coinset an option.
Source Code
FOREIGN CURRENCY
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
public class ForeignCurrency {
    enum Currency {
        EUR("Euros"),
        GBP("Pounds Sterling"),
        JPY("Yen"),
        CAD("Canadian dollars"),
        RUB("Russian Rubles");
        private final String fullName;
        Currency(String fullName) {
            this.fullName = fullName;
        }
        public String getFullName() {
            return fullName;
        }
    }
    private static Map rate = new HashMap<>();
    private static Map units = new HashMap<>();
    private static Map prices = new HashMap<>();
    private static double total = 0.0;
    private static void readRates(Scanner scanner) {
        System.out.println("Please enter the currency rate Per US $ of the following currencies:");
        System.out.println();
        for (Currency c : Currency.values()) {
            System.out.print(c.toString() + ": ");
            double d = Double.parseDouble(scanner.nextLine());
            rate.put(c, d);
        }
        System.out.println();
    }
    private static int processChoice(Scanner scanner) {
        System.out.print("Currency for valuation (1=EUR, 2=GBP, 3=JPY, 4=CAD, 5=RUB, 9=New Rates, 0=Quit): ");
        int choice = Integer.parseInt(scanner.nextLine());
        Currency c = null;
        switch (choice) {
            case 1:
                c = Currency.EUR;
                break;
            case 2:
                c = Currency.GBP;
                break;
            case 3:
                c = Currency.JPY;
                break;
            case 4:
                c = Currency.CAD;
                break;
            case 5:
                c = Currency.RUB;
                break;
        }
        if (c != null) {
            System.out.print("How many " + c.getFullName() + " are you buying? ");
            int value = Integer.parseInt(scanner.nextLine());
            System.out.println();
            double price = value * rate.get(c);
            units.merge(c, value, Integer::sum);
            prices.merge(c, price, (a,b) -> a+b);
            total += price;
            System.out.println("That will have a current value of $ " + String.format("%,.2f", price));
        }
        System.out.println();
        return choice;
    }
    public static void main(String[] args) {
        Locale.setDefault(Locale.US);
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("Welcome to the currency calculator!");
            System.out.println();
            boolean isOver = false;
            while (!isOver) {
                readRates(scanner);
                boolean newRates = false;
                while (!newRates) {
                    int choice = processChoice(scanner);
                    if (choice == 9) {
                        newRates = true;
                    }
                    else if (choice == 0) {
                        newRates = true;
                        isOver = true;
                    }
                }
            }
            for(Currency c : Currency.values()) {
                int unit = units.getOrDefault(c, 0);
                double price = prices.getOrDefault(c, 0.0);
                System.out.println("\t" + c + ": " + unit + " units costing $" + String.format("%,.2f", price));
            }
            System.out.println("The total value of the proposed currency purchases was: $" + String.format("%,.2f", total));
            System.out.println();
            System.out.println("Thanks for using the currency calculator!");
        }
    }
}
PERFECT CHANGE
import java.util.Arrays;
import java.util.Scanner;
public class PerfectChange {
    private static final int MAX_VALUE = 100;
    private static final int[] VALUES = {25, 10, 5, 1};
    private static int[][] solve(int[] counts) {
        int[][][] A = new int[5][MAX_VALUE + 1][];
        for (int i = 0; i < MAX_VALUE + 1; i++) {
            if (i == 0) {
                A[0][i] = new int[]{0, 0, 0, 0};
            } else {
                A[0][i] = null;
            }
        }
        for (int i = 1; i < 5; i++) {
            for (int j = 0; j < MAX_VALUE + 1; j++) {
                if (A[i - 1][j] != null) {
                    A[i][j] = Arrays.copyOf(A[i - 1][j], 4);
                } else {
                    for (int k = 1; k <= counts[i - 1]; k++) {
                        if (j - k * VALUES[i - 1] >= 0 && A[i - 1][j - k * VALUES[i - 1]] != null) {
                            A[i][j] = Arrays.copyOf(A[i - 1][j - k * VALUES[i - 1]], 4);
                            A[i][j][i - 1] += k;
                            break;
                        }
                    }
                }
            }
        }
        return A[4];
    }
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("Welcome to the Perfect Change Calculator!");
            System.out.println();
            boolean isOver = false;
            while (!isOver) {
                System.out.print("Do you have a set of coins for making change? (Y/N): ");
                boolean coinset = scanner.nextLine().trim().toUpperCase().equals("Y");
                int quarters = 4;
                int dimes = 10;
                int nickels = 20;
                int pennies = 1000;
                if (coinset) {
                    System.out.println();
                    System.out.print("No of Quarters: ");
                    quarters = Integer.parseInt(scanner.nextLine());
                    System.out.print("No of Dimes: ");
                    dimes = Integer.parseInt(scanner.nextLine());
                    System.out.print("No of Nickels: ");
                    nickels = Integer.parseInt(scanner.nextLine());
                    System.out.print("No of Pennies: ");
                    pennies = Integer.parseInt(scanner.nextLine());
                }
                int[][] solution = solve(new int[]{quarters, dimes, nickels, pennies});
                System.out.println();
                boolean newCoinSet = false;
                while (!newCoinSet) {
                    System.out.print("What value would you like change for? (1-100 cents, 101=New Coin set, 102=all values or 0=quit): ");
                    int value = Integer.parseInt(scanner.nextLine());
                    if (value <= 0) {
                        newCoinSet = true;
                        isOver = true;
                    } else if (value == 101) {
                        newCoinSet = true;
                    } else if (value > 101) {
                        for (int i = 0; i <= MAX_VALUE; i++) {
                            int[] sol = solution[i];
                            if (sol != null) {
                                System.out.print("For " + i + " cent(s) I give: ");
                                System.out.println(sol[0] + " quarters, " + sol[1] + " dimes, " + sol[2] + " nickels, " + sol[3] + " pennies");
                            } else {
                                int j = 1;
                                while (solution[i - j] == null) {
                                    j++;
                                }
                                System.out.println("I could not make change for: " + i + " cents (I am short: " + j + " cents)");
                            }
                        }
                    } else {
                        int[] sol = solution[value];
                        if (sol == null) {
                            int i = 1;
                            while (solution[value - i] == null) {
                                i++;
                            }
                            System.out.println("I could not make change for: " + value + " cents (I am short: " + i + " cents)");
                        } else {
                            System.out.print("For " + value + " cent(s) I give:");
                            if (coinset) {
                                System.out.println();
                                System.out.print("\t");
                            } else {
                                System.out.print(" ");
                            }
                            System.out.print(sol[0] + " quarters, " + sol[1] + " dimes, " + sol[2] + " nickels, " + sol[3] + " pennies");
                            if (coinset) {
                                System.out.print(" leaving");
                            }
                            System.out.println();
                            if (coinset) {
                                System.out.println("\t" + (quarters - sol[0]) + " quarters, " + (dimes - sol[1]) + " dimes, " + (nickels - sol[2]) + " nickels, " + (pennies - sol[3]) + " pennies ");
                            }
                        }
                    }
                    System.out.println();
                }
            }
            System.out.println("Thanks for using Perfect Change calculator!");
        }
    }
}