Moortiii

Assignment02 - New

Jan 15th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.54 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4.  
  5. public class Assignment02Functions {
  6.     public double requestCurrencyAmount() {
  7.         while(true) {
  8.             try {
  9.                 return Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter an amount to convert"));
  10.             } catch(NumberFormatException e) {
  11.                 JOptionPane.showMessageDialog(null, "Please enter a valid amount");
  12.             }
  13.         }
  14.     }
  15.  
  16.     public String chooseCurrency(String conversionType) {
  17.         String[] choices = { "NOK", "EUR", "JPY", "USD" };
  18.         return (String)JOptionPane.showInputDialog(
  19.                 null,
  20.                 conversionType,
  21.                 "Choose currency",
  22.                 JOptionPane.QUESTION_MESSAGE,
  23.                 null,
  24.                 choices,
  25.                 choices[1]);
  26.     }
  27.  
  28.     public int keepConverting() {
  29.         Object[] options = { "Yes", "No" };
  30.         return JOptionPane.showOptionDialog(
  31.                 null,
  32.                 "Do you wish to convert another currency?",
  33.                 "Currency Converter",
  34.                 JOptionPane.DEFAULT_OPTION,
  35.                 JOptionPane.QUESTION_MESSAGE,
  36.                 null,
  37.                 options,
  38.                 options[0]);
  39.     }
  40.  
  41.     public double NOK_to_EUR(double amount) {
  42.         return amount * 0.12;
  43.     }
  44.     public double NOK_to_JPY(double amount) {
  45.         return amount * 17;
  46.     }
  47.     public double NOK_to_USD(double amount) {
  48.         return amount * (1 / 8);
  49.     }
  50.     public double EUR_to_NOK(double amount) {
  51.         return amount * (1 / 0.12);
  52.     }
  53.     public double EUR_to_JPY(double amount) {
  54.         return NOK_to_JPY(EUR_to_NOK(amount));
  55.     }
  56.     public double EUR_to_USD(double amount) {
  57.         return NOK_to_USD(EUR_to_NOK(amount));
  58.     }
  59.     public double JPY_to_NOK(double amount) {
  60.         return amount * 1/17;
  61.     }
  62.     public double JPY_to_EUR(double amount) {
  63.         return NOK_to_EUR(JPY_to_NOK(amount));
  64.     }
  65.     public double JPY_to_USD(double amount) {
  66.         return NOK_to_USD(JPY_to_NOK(amount));
  67.     }
  68.     public double USD_to_NOK(double amount) {
  69.         return amount * 8.0;
  70.     }
  71.     public double USD_to_EUR(double amount) {
  72.         return NOK_to_EUR(USD_to_NOK(amount));
  73.     }
  74.     public double USD_to_JPY(double amount) {
  75.         return NOK_to_JPY(USD_to_NOK(amount));
  76.     }
  77.  
  78.     public void showConversion(double amount, String conversionCurrency, String convertedCurrency) {
  79.         switch(conversionCurrency) {
  80.             case "EUR":
  81.                 switch(convertedCurrency) {
  82.                     case "EUR":
  83.                         JOptionPane.showMessageDialog(null, amount + " EUR is " + amount + " EUR.");
  84.                         break;
  85.                     case "JPY":
  86.                         JOptionPane.showMessageDialog(null, amount + " EUR is " + EUR_to_JPY(amount) + " JPY.");
  87.                         break;
  88.                     case "NOK":
  89.                         JOptionPane.showMessageDialog(null, amount + " EUR is " + EUR_to_NOK(amount) + " NOK.");
  90.                         break;
  91.                     case "USD":
  92.                         JOptionPane.showMessageDialog(null, amount + " EUR is " + EUR_to_USD(amount) + " USD.");
  93.                         break;
  94.                 }
  95.                 break;
  96.             case "NOK":
  97.                 switch(convertedCurrency) {
  98.                     case "EUR":
  99.                         JOptionPane.showMessageDialog(null, amount + " NOK is " + NOK_to_EUR(amount) + " EUR.");
  100.                         break;
  101.                     case "JPY":
  102.                         JOptionPane.showMessageDialog(null, amount + " NOK is " + NOK_to_JPY(amount) + " JPY.");
  103.                         break;
  104.                     case "NOK":
  105.                         JOptionPane.showMessageDialog(null, amount + " NOK is " + amount + " NOK.");
  106.                         break;
  107.                     case "USD":
  108.                         JOptionPane.showMessageDialog(null, amount + " NOK is " + NOK_to_USD(amount) + " USD.");
  109.                         break;
  110.                 }
  111.                 break;
  112.             case "JPY":
  113.                 switch(convertedCurrency) {
  114.                     case "EUR":
  115.                         JOptionPane.showMessageDialog(null, amount + " JPY is " + JPY_to_EUR(amount) + " EUR.");
  116.                         break;
  117.                     case "JPY":
  118.                         JOptionPane.showMessageDialog(null, amount + " JPY is " + amount + " JPY.");
  119.                         break;
  120.                     case "NOK":
  121.                         JOptionPane.showMessageDialog(null, amount + " JPY is " + JPY_to_NOK(amount) + " NOK.");
  122.                         break;
  123.                     case "USD":
  124.                         JOptionPane.showMessageDialog(null, amount + " JPY is " + JPY_to_USD(amount) + " USD.");
  125.                         break;
  126.                 }
  127.                 break;
  128.             case "USD":
  129.                 switch(convertedCurrency) {
  130.                     case "EUR":
  131.                         JOptionPane.showMessageDialog(null, amount + " USD is " + USD_to_EUR(amount) + " EUR.");
  132.                         break;
  133.                     case "JPY":
  134.                         JOptionPane.showMessageDialog(null, amount + " USD is " + USD_to_JPY(amount) + " JPY.");
  135.                         break;
  136.                     case "NOK":
  137.                         JOptionPane.showMessageDialog(null, amount + " USD is " + USD_to_NOK(amount) + " NOK.");
  138.                         break;
  139.                     case "USD":
  140.                         JOptionPane.showMessageDialog(null, amount + " USD is " + amount + " USD.");
  141.                         break;
  142.                 }
  143.         }
  144.     }
  145. }
  146.  
  147. public class Assignment02Runner {
  148.     public void run_assignment()
  149.     {
  150.         Assignment02Functions assignment02Functions = new Assignment02Functions();
  151.         do {
  152.             String conversionCurrency = assignment02Functions.chooseCurrency("Which currency do you want to convert from?");
  153.             String convertedCurrency = assignment02Functions.chooseCurrency("Which currency do you want to convert to?");
  154.             double amount = assignment02Functions.requestCurrencyAmount();
  155.             assignment02Functions.showConversion(amount, conversionCurrency, convertedCurrency);
  156.         } while(assignment02Functions.keepConverting() != 1);
  157.     }
  158. }
  159.  
  160. public class Main
  161. {
  162.     public static void main(String[] args)
  163.     {
  164.         Assignment02Runner assignment02Runner = new Assignment02Runner();
  165.         assignment02Runner.run_assignment();
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment