CurrencyProg.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package currencyprog;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*
* @author sun
*/
public class CurrencyProg {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Currency myCurrency=new Currency();
//double amount;
char choice = 0;
System.out.println("Welcome to the Currency Converter!");
System.out.print("Enter an amount in GBP:");
myCurrency.pound=readNumber();
do{
menu();
choice=readOption();
switch(choice){
case 'e':
case 'E':
System.out.println("The equivalent amount in EUR is: "+myCurrency.convertToEUR());
break;
case 'd':
case 'D':
System.out.println("The equivalent amount in DOLLAR is: "+myCurrency.convertToUSD());
break;
case 'u':
case 'U':
System.out.print("Enter a new amount in GBP: ");
myCurrency.updateGBP(readNumber());
break;
case 'q':
case 'Q':
System.out.println("PROGRAM ENDED ");
System.exit(0);
break;
}
}while(choice!='q');
}
//////////////////////////////////////////////////////////
static double readNumber(){
double number=0;
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
number = Double.parseDouble(br.readLine());
} catch(Exception e){
System.out.println("Notice " +e.getMessage());
System.exit(0); }
return number;
}
//////////////////////////////////////////////////////////
static void menu(){
System.out.println("Type:");
System.out.println("e to print the equivalent amount in EUR");
System.out.println("d to print the equivalent amount in USD");
System.out.println("u to update the amount in GBP");
System.out.println("q to quit");
System.out.print("Enter selection: ");
}
//////////////////////////////////////////////////////////
static char readOption(){
char x = 0;
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
x= br.readLine().toUpperCase().charAt(0);
} catch(Exception e){
System.out.println("Notice " +e.getMessage());
System.exit(0); }
return x;
}
}
currency.java
package currencyprog;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author sun
*/
public class Currency {
double euro;
double pound;
double dollar;
Currency(){
euro=1.2;
pound=1.0;
dollar=1.6;
}
double convertToEUR(){
return pound*euro;
}
double convertToUSD(){
return pound*dollar;
}
void updateGBP(double x){
pound=x;
}
}
No comments:
Post a Comment