1,105,585 Community Members

Creating a Currency Converter.

Member Avatar
Ranek
Newbie Poster
9 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

So, i'm working on creating a very basic currency converter from Pounds to Euros or Euros to Pounds. It takes the exchange rate from the user then asks which way to convert. Then asks the amount of currency.

This was easy for me to do in just one main class, but I want the logic to be seperate from the interface, and this is confusing me. In two classes I am able to take the user input for the conversion rate, then work out both Euros to Pounds and Pounds to Euros. But I can't work out how to make a system where the user chooses which one to do. I'll include my code below for people to see:

import java.util.Scanner;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);

        String outcome1 = "";
        String outcome2 = "";
        int conversion, result;
        float exchange, input, output123;

        CurrencyConverter converter = new CurrencyConverter();

        System.out.println("Welcome to the Currency Converter!");
        System.out.println("What is the current exchange rate from Pounds to Euro?");
        exchange = scan.nextFloat();

        System.out.println("Enter 1 to convert from Pounds to Euros and 2 to convert from Euros to Pounds");
        

        System.out.println("How much would you like to convert?");
        input = scan.nextFloat();

        output123 = converter.PoundEuro(input, exchange);

        System.out.println(output123);

        output123 = converter.EuroPound(input, exchange);

        System.out.println(output123);


    }
}

CurrencyConverter

public class CurrencyConverter {

    private float exchange,  output,  input;
    private int conversion;
    

    public CurrencyConverter() {
    }

    public float EuroPound(float input, float exchange) {
        
       
        output = (input / exchange);
        return output;
        
    }

    public float PoundEuro(float input, float exchange) {
        
      
        output = (input * exchange);
        return output;
    
    }

    public void checkForCurrency() {

        if (conversion == 1) {


        } else if (conversion == 2) {

        }

    }
}

I realise there are a few bits of code that actually do nothing, thats mainly because I was working on different things that I eventually deleted because they didn't work.

Does anyone have any advice on how to proceed? The main thing I want to achieve is allowing the user to pick the conversion by entering 1 or 2. And having this choice influence the sum carried out.

Cheers

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

The whole "enter 1 or 2" thing is part of the UI, so I would put that logic in the UI class. Ie, if the user enters 1, Main should call PoundEuro, if its 2 call the other one (or vice versa?).

Member Avatar
Ranek
Newbie Poster
9 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok cheers, i've done that and its working fine. I'm not sure if its better to try and shift the actually if statements into the other class. This is the bit I always get confused with, i find it difficult to get my head around classes which is my downfall. Here is what I have now though:

public class Main {

       public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int result;
        float exchange, input, output123;

        CurrencyConverter converter = new CurrencyConverter();

        System.out.println("Welcome to the Currency Converter!");
        System.out.println("What is the current exchange rate from Pounds to Euro?");
        exchange = scan.nextFloat();

        System.out.println("Enter 1 to convert from Pounds to Euros and 2 to convert from Euros to Pounds");
        result = scan.nextInt();


        System.out.println("How much would you like to convert?");
        input = scan.nextFloat();

        while ((result < 1) || (result > 2)) {
            System.out.println("Enter 1 to convert from Pounds to Euros and 2 to convert from Euros to Pounds");
            result = scan.nextInt();

        }
        if (result == 1) {

            output123 = converter.PoundEuro(input, exchange);
            System.out.println(output123);

        } else if (result == 2) {

            output123 = converter.EuroPound(input, exchange);
            System.out.println(output123);

        }

CurrencyConverter

public class CurrencyConverter {

    private float exchange,  output,  input;
    private int conversion;

    public CurrencyConverter() {
    }

    public float EuroPound(float input, float exchange) {


        output = (input / exchange);
        return output;


    }

    public float PoundEuro(float input, float exchange) {


        output = (input * exchange);
        return output;

    }
}

If anyone has any further suggestions for improvement i'll look into them. Its good to get the ideas and then i'll attempt to take a look myself.

Thanks

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article