Java CurrencyTranslator

By | 13/01/2015

Program shows the exchange rate (Doesn’t show real rates) for a requested sum between euros and USD.

Edit: Testing GitHub Gists

Old:

import java.util.Scanner;

public class CurrencyTranslator {
 Scanner reader=new Scanner(System.in);
 boolean repeat=true;
	
 double euroTOdollar=1.18;//€->$ rate
 double dollarTOeuro=0.85;//$->€ rate
	
 private void run(){
  System.out.println("*******************\nCurrency translator 1.0");
  while(repeat){
   int requestedOperation=getOperation();//euro->dollar or dollar->euro or exit
   executeRequestedOperation(requestedOperation);
  }
 }
 private int getOperation(){
  int input=0;
  System.out.println("*******************");
  System.out.print("\n(1) EUR -> USD\n(2) USD -> EUR\n(3) Exit\nChoose: ");
  input=reader.nextInt();
  if(input==3){
   repeat=false;
   System.out.println("\nGood bye!");
  }
  return input;
 }
 private void executeRequestedOperation(int input){
  if(input<1||input>3){
   System.out.println("\nInvalid choice\n");
  }
  else if(input==1){
   double sum=0;
   double euros=0;
   System.out.print("\nInsert sum (EUR): ");
   euros=reader.nextDouble();
   sum=euros*euroTOdollar;
   String sSum=String.format("%.2f", (sum));//Round the sum to two decimals.
   System.out.println("\n\n"+euros+" EUR = "+sSum+" USD\n");
		}
  else if(input==2){
   double sum=0;
   double dollars=0;
   System.out.print("\nInsert sum (USD): ");
   dollars=reader.nextDouble();
   sum=dollars*dollarTOeuro;
   String sSum=String.format("%.2f", (sum));//Round the sum to two decimals.
   System.out.println("\n\n"+dollars+" USD = "+sSum+" EUR\n");
  }
 }
 public static void main(String[]args){
  CurrencyTranslator translator=new CurrencyTranslator();
  translator.run();
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *