Created
November 14, 2019 15:19
-
-
Save mtov/72a7da4aed79818fbd6aae4e0774d6a5 to your computer and use it in GitHub Desktop.
Exemplo de Testabilidade - Servlet
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
import javax.servlet.*; | |
import javax.servlet.http.*; | |
import java.io.*; | |
class IMCModel { | |
public double calculaIMC(String p1, String a1) | |
throws NumberFormatException { | |
double p = Double.parseDouble(p1); | |
double a = Double.parseDouble(a1); | |
double imc = p / (a * a); | |
return Math.floor(imc * 100) / 100; | |
} | |
} | |
public class IMCServlet extends HttpServlet { | |
IMCModel model = new IMCModel(); | |
public void doGet(HttpServletRequest req, HttpServletResponse res) | |
throws IOException, ServletException { | |
res.setContentType("text/html"); | |
PrintWriter out = res.getWriter(); | |
String peso = req.getParameter("peso"); | |
String altura = req.getParameter("altura"); | |
try { | |
double imc = model.calculaIMC(peso, altura); | |
out.println("IMC: " + imc); | |
} | |
catch (NumberFormatException e) { | |
out.println("Dados devem ser numéricos"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment