In: Computer Science
JAVA PROGRAMMING
Objectives JAVA
Specifications
Think of three countries you’d like to travel to and look up the conversion rate between dollars and the money used in those countries. (See http://www.ratesfx.com/ rates/rate-converter.html for conversion rates.) Write a program that will convert money from dollars into each of those three types of currency, or from one of the foreign currencies to US dollars. The program should display a menu that asks the user to choose a currency. The final selection on the menu should let the user quit the program.
When the user selects a currency, the program should ask whether the conversion is to or from dollars, request an amount, and display the result. The program should then display the menu again. This process should be repeated until the user chooses to exit the program.
Your job is to write a program with three classes:
Keep it simple! Make sure you change your program a VERY small amount at each step, so that it always compiles.
Submit the following:
MoneyChanger.java
public class MoneyChanger {
private double amount;
public MoneyChanger()
{
this.amount = 0.0;
}
public MoneyChanger(double amount)
{
this.amount = amount;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public double dollarToYen()
{
return(this.amount * 108.24);
}
public double yenToDollar()
{
return(this.amount / 108.24);
}
public double dollarToEuro()
{
return(this.amount * 0.90);
}
public double euroToDollar()
{
return(this.amount / 0.90);
}
public double dollarToAustralianDollar()
{
return(this.amount * 1.45);
}
public double australianDollarToDollar()
{
return(this.amount / 1.45);
}
}
MoneyChangerView.java
import java.util.Scanner;
public class MoneyChangerView {
public MoneyChangerView()
{
Scanner sc = new Scanner(System.in);
String toFrom;
double amount;
int choice;
do
{
displayMenu();
choice = Integer.parseInt(sc.nextLine().trim());
switch(choice)
{
case 1:
{
System.out.print("\nConvert to Yen or from Yen? (enter to/from):
");
toFrom = sc.nextLine().trim();
while(!toFrom.equalsIgnoreCase("to") &&
!toFrom.equalsIgnoreCase("from"))
{
System.out.println("Invalid input for to/from clause!");
System.out.print("Convert to Yen or from Yen? (enter to/from):
");
toFrom = sc.nextLine().trim();
}
System.out.print("Enter the amount to convert: ");
amount = Double.parseDouble(sc.nextLine().trim());
MoneyChanger changer = new MoneyChanger(amount);
if(toFrom.equalsIgnoreCase("to"))
{
double dollarToYen = changer.dollarToYen();
System.out.println(amount + " Dollars = " + String.format("%.2f",
dollarToYen) + " Yen\n");
}
else
{
double yenToDollar = changer.yenToDollar();
System.out.println(amount + " Yen = " + String.format("%.2f",
yenToDollar) + " Dollars\n");
}
break;
}
case 2:
{
System.out.print("\nConvert to Euro or from Euro? (enter to/from):
");
toFrom = sc.nextLine().trim();
while(!toFrom.equalsIgnoreCase("to") &&
!toFrom.equalsIgnoreCase("from"))
{
System.out.println("Invalid input for to/from clause!");
System.out.print("Convert to Euro or from Euro? (enter to/from):
");
toFrom = sc.nextLine().trim();
}
System.out.print("Enter the amount to convert: ");
amount = Double.parseDouble(sc.nextLine().trim());
MoneyChanger changer = new MoneyChanger(amount);
if(toFrom.equalsIgnoreCase("to"))
{
double dollarToEuro = changer.dollarToEuro();
System.out.println(amount + " Dollars = " + String.format("%.2f",
dollarToEuro) + " Euro\n");
}
else
{
double euroToDollar = changer.euroToDollar();
System.out.println(amount + " Euro = " + String.format("%.2f",
euroToDollar) + " Dollars\n");
}
break;
}
case 3:
{
System.out.print("\nConvert to Australian Dollar or from Australian
Dollar? (enter to/from): ");
toFrom = sc.nextLine().trim();
while(!toFrom.equalsIgnoreCase("to") &&
!toFrom.equalsIgnoreCase("from"))
{
System.out.println("Invalid input for to/from clause!");
System.out.print("\nConvert to Australian Dollar or from Australian
Dollar? (enter to/from): ");
toFrom = sc.nextLine().trim();
}
System.out.print("Enter the amount to convert: ");
amount = Double.parseDouble(sc.nextLine().trim());
MoneyChanger changer = new MoneyChanger(amount);
if(toFrom.equalsIgnoreCase("to"))
{
double dollarToAustralianEuro =
changer.dollarToAustralianDollar();
System.out.println(amount + " Dollars = "
+ String.format("%.2f", dollarToAustralianEuro) + " Australian
Euro\n");
}
else
{
double australianDollarToDollar =
changer.australianDollarToDollar();
System.out.println(amount + " Australian Euro = "
+ String.format("%.2f", australianDollarToDollar) + "
Dollars\n");
}
break;
}
case 4:
{
System.out.println("\nThanks for using the Money Changer
App!\nGoodbye.\n");
System.exit(0);
}
default:
System.out.println("\nInvalid selection!\n");
}
}while(choice != 4);
}
public final void displayMenu()
{
System.out.print("Money Changer Program. Please select the currency
from below:\n"
+ "1. Yen\n"
+ "2. Euro\n"
+ "3. Australian Dollar\n"
+ "4. Exit\n"
+ "Enter your selection: ");
}
}
MoneyChangerController.java (Driver class)
public class MoneyChangerController {
public static void main(String[] args)
{
new MoneyChangerView();
}
}
****************************************************************** SCREENSHOT *********************************************************