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

    Default How could I improve this currency converter using a HashMap?

    My friend whos pretty proficient in Java said I could cut down a lot of this code by using a HashMap, could someone give me an example how exactly you would do that. I'm just not really sure how I would approach that. I know the if/else statements are really repetitive so that must be what I should fix. Any other comments or suggestions, let me know.

    Java Code:
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    class CurrencyConversion {
    	
    	private String type1, type2;
    	private double amount;
    	private static final double USD_GBP = 0.6306;
    	private static final double USD_CAD = 0.9983;
    	private static final double USD_JPY = 82.705;
    	private static final double USD_EUR = 0.7493;
    	private static final double CAD_USD = 1.0017;
    	private static final double CAD_GBP = 0.6317;
    	private static final double CAD_JPY = 82.845;
    	private static final double CAD_EUR = 0.7554 ;
    	private static final double GBP_USD = 1.5858;
    	private static final double GBP_CAD = 1.5831;
    	private static final double GBP_JPY = 131.1535;
    	private static final double GBP_EUR = 1.1957;
    	private static final double JPY_USD = 0.0121;
    	private static final double JPY_CAD = 0.0121;
    	private static final double JPY_GBP = 0.0076;
    	private static final double JPY_EUR = 0.009;
    	private static final double EUR_USD = 1.3345;
    	private static final double EUR_CAD = 1.3238;
    	private static final double EUR_GBP = 0.8364;
    	private static final double EUR_JPY = 110.5363;
    	
    	public CurrencyConversion(String cur1, String cur2, double money){
    		type1 = cur1;
    		type2 = cur2;
    		amount = money;
    	}
    	public void getType() {
    		if("usd".equalsIgnoreCase(type1)){
    			usd();
    		}
    		else if ("cad".equalsIgnoreCase(type1)) {
    			cad();
    		}
    		else if ("gbp".equalsIgnoreCase(type1)) {
    			gbp();
    		}
    		else if("jpy".equalsIgnoreCase(type1)) {
    			jpy();
    		}
    		else if("eur".equalsIgnoreCase(type1)) {
    			eur();
    		}
    	}
    	
    	public void usd() {
    		
    		if("gbp".equalsIgnoreCase(type2)) {
    			amount *= USD_GBP;
    			gbpSymbol();
    		}
    		else if("cad".equalsIgnoreCase(type2)) {
    			amount *= USD_CAD;
    			cadSymbol();
    		}
    		else if("jpy".equalsIgnoreCase(type2)){
    			amount *= USD_JPY;
    			jpySymbol();
    		}
    		else if("eur".equalsIgnoreCase(type2)) {
    			amount *= USD_EUR;
    			eurSymbol();
    		}
    		else if ("usd".equalsIgnoreCase(type2)) {	
    			usdSymbol();
    		}
    		 
    	}
    	public void cad() {
    		
    		if("usd".equalsIgnoreCase(type2)) {
    			amount *= CAD_USD;
    			usdSymbol();
    		}
    		else if("gbp".equalsIgnoreCase(type2)) {
    			amount *= CAD_GBP;
    			gbpSymbol();
    		}
    		else if("jpy".equalsIgnoreCase(type2)){
    			amount *= CAD_JPY;
    			jpySymbol();
    		}
    		else if("eur".equalsIgnoreCase(type2)) {
    			amount *= CAD_EUR;
    			eurSymbol();
    		}
    		else if ("cad".equalsIgnoreCase(type2)) {	
    			cadSymbol();
    		}
    		
    	}
    	public void gbp() {
    		if("usd".equalsIgnoreCase(type2)) {
    			amount *= GBP_USD;	 
    			usdSymbol();
    		}
    		else if("cad".equalsIgnoreCase(type2)) {
    			amount *= GBP_CAD;	
    			cadSymbol();
    		}
    		else if("jpy".equalsIgnoreCase(type2)) {
    			amount *= GBP_JPY;
    			jpySymbol();
    		}
    		else if("eur".equalsIgnoreCase(type2)) {
    			amount *= GBP_EUR;
    			eurSymbol();
    		}
    		else if ("gbp".equalsIgnoreCase(type2)) {
    			gbpSymbol();
    		}
    	}
    	public void jpy() {
    		if("usd".equalsIgnoreCase(type2)) {
    			amount *= JPY_USD;	 
    			usdSymbol();
    		}
    		else if("cad".equalsIgnoreCase(type2)) {
    			amount *= JPY_CAD;	
    			cadSymbol();
    		}
    		else if ("gbp".equalsIgnoreCase(type2)) {
    			amount *= JPY_GBP;
    			gbpSymbol();
    		}
    		else if("eur".equalsIgnoreCase(type2)) {
    			amount *= JPY_EUR;
    			eurSymbol();
    		}
    		else if("jpy".equalsIgnoreCase(type2)){
    			jpySymbol();
    		}
    	}
    	
    	public void eur() {
    		if("usd".equalsIgnoreCase(type2)) {
    			amount *= EUR_USD;	 
    			usdSymbol();
    		}
    		else if("cad".equalsIgnoreCase(type2)) {
    			amount *= EUR_CAD;	
    			cadSymbol();
    		}
    		else if ("gbp".equalsIgnoreCase(type2)) {
    			amount *= EUR_GBP;
    			gbpSymbol();
    		}
    		else if("jpy".equalsIgnoreCase(type2)){
    			amount *= EUR_JPY;
    			jpySymbol();
    		}
    		else if("eur".equalsIgnoreCase(type2)) {
    			eurSymbol();
    		}
    	
    		
    		
    	}
    	
    	public void usdSymbol(){
    		 System.out.printf("%s converted to %s currency is: $%f ", 
    				type1.toUpperCase(), type2.toUpperCase(), amount);
    	}
    	
    	public void cadSymbol() {
    		System.out.printf("%s converted to %s currency is: C$%f ", 
    				type1.toUpperCase(), type2.toUpperCase(), amount);
    	}
    	
    	public void gbpSymbol() {
    		System.out.printf("%s converted to %s currency is: £%f ", 
    				type1.toUpperCase(), type2.toUpperCase(), amount);
    	}
    	
    	public void jpySymbol() {
    		System.out.printf("%s converted to %s currency is: ¥%f ", 
    				type1.toUpperCase(), type2.toUpperCase(), amount);
    	}
    	public void eurSymbol() {
    		System.out.printf("%s converted to %s currency is: €%f ", 
    				type1.toUpperCase(), type2.toUpperCase(), amount);
    	}
    
    }
    public class Simulation2 {
        public static void main(String[] args) { 
        	
        	Scanner scan = new Scanner(System.in);
        	
        	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("Japanese Yen (JPY)");
        	System.out.println("Euro (EUR)");
        	System.out.println();
        	    	
        	String cur1, cur2;
        	double money = 0;
        	
        	while(true) {
        		System.out.println("Enter an above currency: ");
        		cur1 = scan.next();
        		
        			if("usd".equalsIgnoreCase(cur1) || "cad".equalsIgnoreCase(cur1) || "gbp".equalsIgnoreCase(cur1) || 
        					"jpy".equalsIgnoreCase(cur1) || "eur".equalsIgnoreCase(cur1)){
        				break;
        			}
        			else {
        				System.out.println("*Invalid currency*");
        			}
        		}
       
        	while(true) {
        		System.out.println("Enter an above currency to convert to: ");
        		cur2 = scan.next();
        		
        			if("usd".equalsIgnoreCase(cur2) || "cad".equalsIgnoreCase(cur2) || "gbp".equalsIgnoreCase(cur2) || 
        					"jpy".equalsIgnoreCase(cur2)  || "eur".equalsIgnoreCase(cur2)){
        				break;
        			}
        			else {
        				System.out.println("*Invalid currency*");
        			}
        		}
        	
        	while(true){
        		System.out.println("Enter currency amount: ");
        		
        		try {
        			money = scan.nextDouble();
        			break;
        		}
        		catch(InputMismatchException e) {
        			System.out.println("*Please enter a numerical amount*");	
        			scan.nextLine();
        		}
        	}
        	
    		CurrencyConversion con = new CurrencyConversion(cur1, cur2, money);
            con.getType();
        
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: How could I improve this currency converter using a HashMap?

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: How could I improve this currency converter using a HashMap?

    Map of Maps.
    Outer Map has currency key referencing an inner Map.
    Inner Map has currency key referencing the exchange rate.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: How could I improve this currency converter using a HashMap?

    Quote Originally Posted by Tolls View Post
    Map of Maps.
    Outer Map has currency key referencing an inner Map.
    Inner Map has currency key referencing the exchange rate.
    Why not have one Map from (Currency*Currency) --> Exchange Rate? The OP already has those numbers (and names of the Cartesian products).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: How could I improve this currency converter using a HashMap?

    I'm not a fan of concatentation for keys.
    No great reason, I just don't like it.
    I think it comes from visualising this sort of problem as a database table, where your from-currency maps to a to-currency to give you a result.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: How could I improve this currency converter using a HashMap?

    Quote Originally Posted by Tolls View Post
    I'm not a fan of concatentation for keys.
    No great reason, I just don't like it.
    I think it comes from visualising this sort of problem as a database table, where your from-currency maps to a to-currency to give you a result.
    True, but then I'd suggest a matrix of currencies; er[from[to]= exchange rate from 'from' to 'to'. If an exchange rate is not present in a row of the matrix, it possibly can be found by calculating the closure over the rows of the matrix (e.g. for euros to dollars to gbp gives the rate for euros to gbp). You only'd need a map from curency to index value then ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. java currency
    By alfarida in forum New To Java
    Replies: 4
    Last Post: 05-23-2011, 01:11 PM
  2. Currency Converter Applet Help...
    By MarkL410 in forum Java Applets
    Replies: 3
    Last Post: 11-09-2010, 05:27 PM
  3. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  4. Currency Converter!!!!
    By Pascal Nouma in forum AWT / Swing
    Replies: 2
    Last Post: 04-02-2009, 07:23 AM
  5. Setting currency
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-16-2007, 02:08 PM

Posting Permissions

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