Results 1 to 5 of 5
  1. #1
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Currency converter, cutting down on if/else statements

    I made a currency converter a while back which I showed my friend whos a programmer and he said I could cut down on those if statements by using a HashMap. The problem I'm having is that I want to have it set up so that the user first enters a currency and then a currency to convert to. How can you use the HashMap or something else in a way that you wouldn't have to use the equals() method which leads to if statements. I used the map in this code, but its not really necessary for the way I did it.

    Java Code:
    import java.util.HashMap;
    import java.util.Scanner;
    
    class ConvertClass {
    	private String first;
    	private String second;
    	private double amount;
    	private HashMap<String, Double> map = new HashMap<String, Double>();
    	
    	ConvertClass (String first, String second, double amount){
    		this.first = first;
    		this.second = second;
    		this.amount = amount;
    		
    	}
    	void conversion(){
    		if("usd".equals(first) && "gbp".equals(second)){
    			map.put("usd_gbp", 0.6306 * amount);
    			System.out.printf("%s converted to %s is: %f", first, second, map.get("usd_gbp"));
    		}
    		else if("usd".equals(first) && "cad".equals(second)){
    			map.put("usd_cad", 0.9983 * amount);
    			System.out.printf("%s converted to %s is: %f", first, second, map.get("usd_cad"));
    		}
    		else if("gbp".equals(first) && "usd".equals(second)){
    			map.put("gbp_usd", 1.5858 * amount);
    			System.out.printf("%s converted to %s is: %f", first, second, map.get("gbp_usd"));
    		}
    		else if("gbp".equals(first) && "cad".equals(second)){
    			map.put("gbp_cad", 1.5831 * amount);
    			System.out.printf("%s converted to %s is: %f", first, second, map.get("gbp_cad"));
    		}
    		else if("cad".equals(first) && "usd".equals(second)){
    			map.put("cad_usd", 1.0017 * amount);
    			System.out.printf("%s converted to %s is: %f", first, second, map.get("cad_usd"));
    		}
    		else if("cad".equals(first) && "gbp".equals(second)){
    			map.put("cad_gbp", 0.6317 * amount);
    			System.out.printf("%s converted to %s is: %f", first, second, map.get("cad_gbp"));
    		}
    		
    	}
    	
    }
    public class Simulation3 {
        public static void main(String[] args) { 	
        	Scanner scan = new Scanner(System.in);
        	
        	double amount;
        	String firstCurrency, secondCurrency;
    
        	System.out.println("\n**CURRENCY CONVERTER**");
    		System.out.println("----------------------");
    		System.out.println("United States Dollar (USD)");
    		System.out.println("Canadian Dollar (CAD)");
    		System.out.println("British Pound (GBP)");
    		System.out.println();
        	
    		System.out.println("Enter an above currency: ");
    		firstCurrency = scan.nextLine();
    
    		System.out.println("Enter a currency to convert to: ");
    		secondCurrency = scan.nextLine();
    		
        	System.out.println("Enter currency amount: ");
        	amount = scan.nextDouble();
        	
        	ConvertClass convert = new ConvertClass(firstCurrency, secondCurrency, amount);
        	convert.conversion();
        	
        }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Currency converter, cutting down on if/else statements

    You're map is almost entirely useless there, as it stands.

    It ought (in this case) to be a static final variable that contains all that data:
    Java Code:
       private static final HashMap<String, Double> map = new HashMap<String, Double>();
       static {
          map.put("usd_gbp", 0.6306);
          // etc.
       }
    That is a static block that will initialise the map with the currency conversion data.
    Your conversion() method then uses the two currency codes to reference that value and then multiply the 'amount'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Currency converter, cutting down on if/else statements

    I guess you have lots of Currency needed to convert , therefore your Java Programmer friend will ask you to code it by using HashMap.
    Actually HashMap is really a good choice for your problem.


    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package simulation3;
    
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    
    
    public class Simulation3 {
        public static void main(String[] args) {   
            Map<String, Double> m = new HashMap<>();
            m.put("USD CAD",0.2);
            m.put("CAD USD",0.2);
            m.put("GBP USD",0.2);
            m.put("USD GBP",0.2);
            m.put("GBP CAD",0.2);
            m.put("CAD GBP",0.2);
                
            
            Scanner scan = new Scanner(System.in);       
            double amount,convert_amount;
            String firstCurrency, secondCurrency ,key;
     
            System.out.println("\n**CURRENCY CONVERTER**");
            System.out.println("----------------------");
            System.out.println("United States Dollar (USD)");
            System.out.println("Canadian Dollar (CAD)");
            System.out.println("British Pound (GBP)");
            System.out.println();
             
            System.out.println("Enter an above currency: ");
            firstCurrency = scan.nextLine();
     
            System.out.println("Enter a currency to convert to: ");
            secondCurrency = scan.nextLine();
             
            System.out.println("Enter currency amount: ");
            amount = scan.nextDouble();
             
            key=(firstCurrency+" "+secondCurrency).toUpperCase();
            convert_amount=m.get(key)*amount;
            System.out.println(firstCurrency+" converted to "+ secondCurrency+" is : "+ convert_amount);
            
             
        }
    }


    Ksharp

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Currency converter, cutting down on if/else statements

    Please don't spoonfeed...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Re: Currency converter, cutting down on if/else statements

    Thank you all for your help, much appreciated!

Similar Threads

  1. JScrollPane is Cutting off my Image upon Scrolling
    By Ryan.Vincent in forum AWT / Swing
    Replies: 6
    Last Post: 04-19-2012, 05:16 AM
  2. Replies: 5
    Last Post: 03-28-2012, 11:16 AM
  3. Currency Converter Applet Help...
    By MarkL410 in forum Java Applets
    Replies: 3
    Last Post: 11-09-2010, 05:27 PM
  4. Cutting .wav file
    By qxc in forum Advanced Java
    Replies: 0
    Last Post: 06-27-2010, 09:50 AM
  5. Currency Converter!!!!
    By Pascal Nouma in forum AWT / Swing
    Replies: 2
    Last Post: 04-02-2009, 07:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •