Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import javax.swing.*;
- public class Assignment02Functions {
- public double requestCurrencyAmount() {
- while(true) {
- try {
- return Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter an amount to convert"));
- } catch(NumberFormatException e) {
- JOptionPane.showMessageDialog(null, "Please enter a valid amount");
- }
- }
- }
- public String chooseCurrency(String conversionType) {
- String[] choices = { "NOK", "EUR", "JPY", "USD" };
- return (String)JOptionPane.showInputDialog(
- null,
- conversionType,
- "Choose currency",
- JOptionPane.QUESTION_MESSAGE,
- null,
- choices,
- choices[1]);
- }
- public int keepConverting() {
- Object[] options = { "Yes", "No" };
- return JOptionPane.showOptionDialog(
- null,
- "Do you wish to convert another currency?",
- "Currency Converter",
- JOptionPane.DEFAULT_OPTION,
- JOptionPane.QUESTION_MESSAGE,
- null,
- options,
- options[0]);
- }
- public double NOK_to_EUR(double amount) {
- return amount * 0.12;
- }
- public double NOK_to_JPY(double amount) {
- return amount * 17;
- }
- public double NOK_to_USD(double amount) {
- return amount * (1 / 8);
- }
- public double EUR_to_NOK(double amount) {
- return amount * (1 / 0.12);
- }
- public double EUR_to_JPY(double amount) {
- return NOK_to_JPY(EUR_to_NOK(amount));
- }
- public double EUR_to_USD(double amount) {
- return NOK_to_USD(EUR_to_NOK(amount));
- }
- public double JPY_to_NOK(double amount) {
- return amount * 1/17;
- }
- public double JPY_to_EUR(double amount) {
- return NOK_to_EUR(JPY_to_NOK(amount));
- }
- public double JPY_to_USD(double amount) {
- return NOK_to_USD(JPY_to_NOK(amount));
- }
- public double USD_to_NOK(double amount) {
- return amount * 8.0;
- }
- public double USD_to_EUR(double amount) {
- return NOK_to_EUR(USD_to_NOK(amount));
- }
- public double USD_to_JPY(double amount) {
- return NOK_to_JPY(USD_to_NOK(amount));
- }
- public void showConversion(double amount, String conversionCurrency, String convertedCurrency) {
- switch(conversionCurrency) {
- case "EUR":
- switch(convertedCurrency) {
- case "EUR":
- JOptionPane.showMessageDialog(null, amount + " EUR is " + amount + " EUR.");
- break;
- case "JPY":
- JOptionPane.showMessageDialog(null, amount + " EUR is " + EUR_to_JPY(amount) + " JPY.");
- break;
- case "NOK":
- JOptionPane.showMessageDialog(null, amount + " EUR is " + EUR_to_NOK(amount) + " NOK.");
- break;
- case "USD":
- JOptionPane.showMessageDialog(null, amount + " EUR is " + EUR_to_USD(amount) + " USD.");
- break;
- }
- break;
- case "NOK":
- switch(convertedCurrency) {
- case "EUR":
- JOptionPane.showMessageDialog(null, amount + " NOK is " + NOK_to_EUR(amount) + " EUR.");
- break;
- case "JPY":
- JOptionPane.showMessageDialog(null, amount + " NOK is " + NOK_to_JPY(amount) + " JPY.");
- break;
- case "NOK":
- JOptionPane.showMessageDialog(null, amount + " NOK is " + amount + " NOK.");
- break;
- case "USD":
- JOptionPane.showMessageDialog(null, amount + " NOK is " + NOK_to_USD(amount) + " USD.");
- break;
- }
- break;
- case "JPY":
- switch(convertedCurrency) {
- case "EUR":
- JOptionPane.showMessageDialog(null, amount + " JPY is " + JPY_to_EUR(amount) + " EUR.");
- break;
- case "JPY":
- JOptionPane.showMessageDialog(null, amount + " JPY is " + amount + " JPY.");
- break;
- case "NOK":
- JOptionPane.showMessageDialog(null, amount + " JPY is " + JPY_to_NOK(amount) + " NOK.");
- break;
- case "USD":
- JOptionPane.showMessageDialog(null, amount + " JPY is " + JPY_to_USD(amount) + " USD.");
- break;
- }
- break;
- case "USD":
- switch(convertedCurrency) {
- case "EUR":
- JOptionPane.showMessageDialog(null, amount + " USD is " + USD_to_EUR(amount) + " EUR.");
- break;
- case "JPY":
- JOptionPane.showMessageDialog(null, amount + " USD is " + USD_to_JPY(amount) + " JPY.");
- break;
- case "NOK":
- JOptionPane.showMessageDialog(null, amount + " USD is " + USD_to_NOK(amount) + " NOK.");
- break;
- case "USD":
- JOptionPane.showMessageDialog(null, amount + " USD is " + amount + " USD.");
- break;
- }
- }
- }
- }
- public class Assignment02Runner {
- public void run_assignment()
- {
- Assignment02Functions assignment02Functions = new Assignment02Functions();
- do {
- String conversionCurrency = assignment02Functions.chooseCurrency("Which currency do you want to convert from?");
- String convertedCurrency = assignment02Functions.chooseCurrency("Which currency do you want to convert to?");
- double amount = assignment02Functions.requestCurrencyAmount();
- assignment02Functions.showConversion(amount, conversionCurrency, convertedCurrency);
- } while(assignment02Functions.keepConverting() != 1);
- }
- }
- public class Main
- {
- public static void main(String[] args)
- {
- Assignment02Runner assignment02Runner = new Assignment02Runner();
- assignment02Runner.run_assignment();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment