Last active
December 7, 2016 04:06
-
-
Save sachi-d/3c5946066e53300ecc678e25633af526 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exchangerate; | |
import java.util.HashMap; | |
/** | |
* | |
* @author sachithra | |
*/ | |
public class CurrencyCalculator { | |
private HashMap<String, Double> rates; | |
private String base; | |
public CurrencyCalculator() { | |
this.rates = new HashMap(); | |
this.rates.put("USD", 1.0); | |
this.rates.put("EUR", 0.933); | |
this.rates.put("LKR", 149.132); | |
this.rates.put("AUD", 1.345); | |
this.rates.put("YEN", 114.123); | |
this.base = "USD"; | |
} | |
public double getExchangeRate(String from, String to) { | |
if (rates.containsKey(from) && rates.containsKey(to)) { | |
double toVal = rates.get(to); | |
double fromVal = rates.get(from); | |
double rate = toVal / fromVal; | |
return rate; | |
} | |
return 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exchangerate; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.UriInfo; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PUT; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.core.MediaType; | |
/** | |
* REST Web Service | |
* | |
* @author sachithra | |
*/ | |
@Path("rates") | |
public class ExchangeRate { | |
@Context | |
private UriInfo context; | |
private final CurrencyCalculator rates; | |
/** | |
* Creates a new instance of ExchangeRate | |
*/ | |
public ExchangeRate() { | |
rates = new CurrencyCalculator(); | |
} | |
/** | |
* Retrieves representation of an instance of exchangerate.ExchangeRate | |
* | |
* @param from | |
* @param to | |
* @return an instance of java.lang.String | |
*/ | |
@GET | |
@Path("{from}/{to}") | |
@Produces(MediaType.TEXT_PLAIN) | |
public String getRates(@PathParam("from") String from, @PathParam("to") String to) { | |
double value = rates.getExchangeRate(from, to); | |
String resultText = ""; | |
if(value!=0){ | |
resultText = "1 " + from + " is equal to " + value + " " + to; | |
}else{ | |
resultText = "Sorry, the currencies you defined are invalid or not yet implemented."; | |
} | |
return resultText; | |
} | |
/** | |
* PUT method for updating or creating an instance of ExchangeRate | |
* | |
* @param content representation for the resource | |
*/ | |
@PUT | |
@Consumes(MediaType.TEXT_PLAIN) | |
public void putText(String content) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment