Guest User

Untitled

a guest
Jun 24th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. public class Calculator {
  2.     private final DecimalFormat df;
  3.    
  4.     public Calculator() {
  5.         this.df = new DecimalFormat("#.##");
  6.     }
  7.    
  8.     public double convert(Currency from, Currency to, double fromAmount) {
  9.         if (!(from instanceof Currency) || (to instanceof Currency)) {
  10.             throw new IllegalArgumentException(
  11.                     "Object 'other' not instance of class 'Currency'");
  12.         }
  13.         if (fromAmount <= 0) {
  14.             throw new IllegalArgumentException("Amount cannot be less than 0!");
  15.         }
  16.         if (from == to) {
  17.             return fromAmount;
  18.         }
  19.  
  20.         double amountInNOK = 0;
  21.         double toAmount = 0;
  22.         if (from.getUnit() == 100 && to.getUnit() == 100) {
  23.             amountInNOK = fromAmount * (from.getRate() / 100);
  24.             toAmount = amountInNOK * (to.getRate() / 100);
  25.         } else if (from.getUnit() == 1 && to.getUnit() == 1) {
  26.             amountInNOK = fromAmount * from.getRate();
  27.             toAmount = amountInNOK * to.getRate();
  28.         } else if (from.getUnit() == 1 && to.getUnit() == 100) {
  29.             amountInNOK = fromAmount * from.getRate();
  30.             toAmount = amountInNOK * (to.getRate() / 100);
  31.         } else { // from.getUnit() == 100 && to.getUnit() == 1
  32.             amountInNOK = fromAmount * (from.getRate() / 100);
  33.             toAmount = amountInNOK * to.getRate();
  34.         }
  35.         return Double.parseDouble(df.format(toAmount));
  36.     }
  37. }
Add Comment
Please, Sign In to add comment