/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package netukar.exchange.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import netukar.exchange.ExchangeList; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForward; /** * * @author radovan */ public class CalculateExchangeAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ExchangeListException { try { ExchangeList exchange = new ExchangeList(); double amount = Double.parseDouble(request.getParameter("amount")); String from = request.getParameter("from"); String to = request.getParameter("to"); double result = exchange.calculate(amount, from, to); request.setAttribute("result", exchange.format(result)); request.setAttribute("amount", amount); request.setAttribute("from", from); request.setAttribute("to", to); return mapping.findForward("success"); } catch (NumberFormatException ex) { throw new ExchangeListException("Invalid number format"); } catch (IllegalArgumentException ex) { throw new ExchangeListException("Unsupported currency"); } } }