HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMajor

Introductory Currency Converter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
introductoryconvertercurrency

Problem

This is my basic currency converter for my intro to Java class. I'm supposed to be able to convert between Yen, Dollars, Pounds and Euros using static rates. It works, but I was curious to know if I did it in the most efficient way possible. It seems quite long and looks like a huge mess. Just wanting some feedback.

```
import java.util.Scanner;

public class currency
{
public currency()
{
char us_dollar_sym = 36;
char pound_sym = 163;
char yen_sym = 165;
char euro_sym = 8364;

String us_dollar = "Dollars";
String pound = "Pounds";
String yen = "Yen";
String euro = "Euros";
double rate = 0;

// Interface
System.out.println("Welcome to the Currency Converter Program \n");
System.out.println("Use the following codes to input your currency choices: \n 1 - US dollars \n 2 - Euros \n 3 - British Pounds \n 4 - Japanese Yen \n");

//
System.out.println("Please choose the input currency:");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();

String inType = null;
switch(choice) {
case 1: inType = "US Dollars >> " + us_dollar_sym; break;
case 2: inType = "Euros >> " + euro_sym; break;
case 3: inType = "British Pounds >> " + pound_sym; break;
case 4: inType = "Japanese Yen >> " + yen_sym; break;
default:
System.out.println("Please restart the program & enter a number from the list.");
return;
}

System.out.println("Please choose the output currency");
int output = in.nextInt();

System.out.printf("Now enter the input in " + inType);
double input = in.nextDouble();

if (choice == output)
System.out.println("Same currency no need to convert");

if (choice == 1 && output == 2)
{
double dollar_euro_rate = 0.78391;
rate = input * dollar_euro_rate;
System.out.

Solution

Your assessment of your own code is pretty close to accurate: it seems quite long and looks like a huge mess.

On the other hand, you have worked through what appear to be all the use cases, and you have comprehensively solved the problem. You have put a lot of work in to manually building out a logic tree, and calculation system, to get the conversions right. Frankly, you have done so much of it by hand, that there's little left for the system to compute. Your determination is commendable.

Addressing the mess first. A simple re-formatting of the code would do wonders for it. I took the liberty of using Eclipse, and running it through the auto-formatter:

```
import java.util.Scanner;

public class currency {

public currency() {
char us_dollar_sym = 36;
char pound_sym = 163;
char yen_sym = 165;
char euro_sym = 8364;

String us_dollar = "Dollars";
String pound = "Pounds";
String yen = "Yen";
String euro = "Euros";
double rate = 0;

// Interface
System.out.println("Welcome to the Currency Converter Program \n");
System.out
.println("Use the following codes to input your currency choices: \n 1 - US dollars \n 2 - Euros \n 3 - British Pounds \n 4 - Japanese Yen \n");

//
System.out.println("Please choose the input currency:");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();

String inType = null;
switch (choice) {
case 1:
inType = "US Dollars >> " + us_dollar_sym;
break;
case 2:
inType = "Euros >> " + euro_sym;
break;
case 3:
inType = "British Pounds >> " + pound_sym;
break;
case 4:
inType = "Japanese Yen >> " + yen_sym;
break;
default:
System.out
.println("Please restart the program & enter a number from the list.");
return;
}

System.out.println("Please choose the output currency");
int output = in.nextInt();

System.out.printf("Now enter the input in " + inType);
double input = in.nextDouble();

if (choice == output)
System.out.println("Same currency no need to convert");

if (choice == 1 && output == 2) {
double dollar_euro_rate = 0.78391;
rate = input * dollar_euro_rate;
System.out.printf("%s" + input + " at a conversion rate of "
+ dollar_euro_rate + " Dollars to %s = %.2f\n",
(char) us_dollar_sym, euro, rate);
} else if (choice == 1 && output == 3) {
double dollar_pound_rate = 0.621484;
rate = input * dollar_pound_rate;
System.out.printf("%s" + input + " at a conversion rate of "
+ dollar_pound_rate + " Dollars to %s = %.2f\n",
(char) us_dollar_sym, pound, rate);
} else if (choice == 1 && output == 4) {
double dollar_yen_rate = 107.174;
rate = input * dollar_yen_rate;
System.out.printf("%s" + input + " at a conversion rate of "
+ dollar_yen_rate + " Dollars to %s = %.2f\n",
(char) us_dollar_sym, yen, rate);
}
if (choice == 2 && output == 1) {
double euro_dollar_rate = 1.27579;
rate = input * euro_dollar_rate;
System.out.printf("%s" + input + " at a conversion rate of "
+ euro_dollar_rate + " Euros to %s = %.2f\n",
(char) euro_sym, us_dollar, rate);
} else if (choice == 2 && output == 3) {
double euro_pound_rate = 0.792648;
rate = input * euro_pound_rate;
System.out.printf("%s" + input + " at a conversion rate of "
+ euro_pound_rate + " Euros to %s = %.2f\n",
(char) euro_sym, pound, rate);
} else if (choice == 2 && output == 4) {
double euro_yen_rate = 136.708;
rate = input * euro_yen_rate;
System.out.printf("%s" + input + " at a conversion rate of "
+ euro_yen_rate + " Euros to %s = %.2f\n", (char) euro_sym,
yen, rate);
}
if (choice == 3 && output == 1) {
double pound_dollar_rate = 1.60972;
System.out.printf("%s" + input + " at a conversion rate of "
+ pound_dollar_rate + " Pounds to %s = %.2f\n",
(char) pound_sym, us_dollar, rate);
} else if (choice == 3 && output == 2) {
double pound_euro_rate = 1.26161;
System.out.printf("%s" + input + " at a conversion rate of "
+ pound_euro_rate + " Pounds to %s = %.2f\n",
(char) pound_sym, euro, rate);
} else if (choice == 3 && output == 4) {
double pound_yen_rate = 172.511;

Code Snippets

import java.util.Scanner;

public class currency {

    public currency() {
        char us_dollar_sym = 36;
        char pound_sym = 163;
        char yen_sym = 165;
        char euro_sym = 8364;

        String us_dollar = "Dollars";
        String pound = "Pounds";
        String yen = "Yen";
        String euro = "Euros";
        double rate = 0;

        // Interface
        System.out.println("Welcome to the Currency Converter Program \n");
        System.out
                .println("Use the following codes to input your currency choices: \n 1 - US dollars \n 2 - Euros \n 3 - British Pounds \n 4 - Japanese Yen \n");

        //
        System.out.println("Please choose the input currency:");
        Scanner in = new Scanner(System.in);
        int choice = in.nextInt();

        String inType = null;
        switch (choice) {
        case 1:
            inType = "US Dollars >> " + us_dollar_sym;
            break;
        case 2:
            inType = "Euros >> " + euro_sym;
            break;
        case 3:
            inType = "British Pounds >> " + pound_sym;
            break;
        case 4:
            inType = "Japanese Yen >> " + yen_sym;
            break;
        default:
            System.out
                    .println("Please restart the program & enter a number from the list.");
            return;
        }

        System.out.println("Please choose the output currency");
        int output = in.nextInt();

        System.out.printf("Now enter the input in " + inType);
        double input = in.nextDouble();

        if (choice == output)
            System.out.println("Same currency no need to convert");

        if (choice == 1 && output == 2) {
            double dollar_euro_rate = 0.78391;
            rate = input * dollar_euro_rate;
            System.out.printf("%s" + input + " at a conversion rate of "
                    + dollar_euro_rate + " Dollars to %s = %.2f\n",
                    (char) us_dollar_sym, euro, rate);
        } else if (choice == 1 && output == 3) {
            double dollar_pound_rate = 0.621484;
            rate = input * dollar_pound_rate;
            System.out.printf("%s" + input + " at a conversion rate of "
                    + dollar_pound_rate + " Dollars to %s = %.2f\n",
                    (char) us_dollar_sym, pound, rate);
        } else if (choice == 1 && output == 4) {
            double dollar_yen_rate = 107.174;
            rate = input * dollar_yen_rate;
            System.out.printf("%s" + input + " at a conversion rate of "
                    + dollar_yen_rate + " Dollars to %s = %.2f\n",
                    (char) us_dollar_sym, yen, rate);
        }
        if (choice == 2 && output == 1) {
            double euro_dollar_rate = 1.27579;
            rate = input * euro_dollar_rate;
            System.out.printf("%s" + input + " at a conversion rate of "
                    + euro_dollar_rate + " Euros to %s = %.2f\n",
                    (char) euro_sym, us_dollar, rate);
public class Currency {
}
USD
USD     1.0
Euro    0.78391
GBP     0.621484
Yen   107.174
public class Currency {
    private final String name;
    private final double rate;
    public Currency(String name, double rate) {
        this.name = name;
        this.rate = rate;
    }
}
Currency[] currencies = {
    new Currency("USD", 1.0),
    new Currency("Euro", 0.78391),
      ....
};

Context

StackExchange Code Review Q#67272, answer score: 21

Revisions (0)

No revisions yet.