TablaMultiplicarServelet.java 4.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

package com.roshka.webprojecttest.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/tabla-multiplicar")
public class TablaMultiplicarServelet extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		PrintWriter pw = new PrintWriter(resp.getOutputStream());
		Boolean valido=true;
		String parametro =  req.getParameter("number");
		if(parametro == null) {
			initForm(pw);
		}else {
			
			if(parametro.length() <1)
				valido=false;
			for(int i = 0 ; i < parametro.length(); i++) {
				if(  ( !Character.isDigit(parametro.charAt(i)) && i > 0) && !(Character.isDigit(parametro.charAt(i)) && i == 0 && parametro.charAt(i) != '-' )  ) {
					valido=false;
					break;
				}
			}
			if (!valido) {
				invalid(pw, parametro);
			}
			else {
				Integer numero = Integer.parseInt(parametro);
				if(numero > 100) {
					greaterThan100(pw, numero);
					
				}
				else if(numero<0) {
					negative(pw);
					
				}
				else{
					theTable(pw, numero);			
				}
			}
		}
		pw.close();
	}
	
	void initForm(PrintWriter pw) {
		pw.write("<html>"
				+ "<head>"
				+ "<title>Hola, Aqui te enseño las tablas de la multiplicacion</title>"
				+ "</head>"
				+ "<body>"
				+ "<h1>Soy un generador de tablas de multiplicar</h1>"
				+ "<div>"
				+ "<form action=\"tabla-multiplicar\">"
				+ "Enter 1st number: <input type=\"text\"  name=number><br>"
				+ "<input type=\"submit\">"
				+ "</form>"
				+ "</div>"
				+ "</body>"
				+ "</html>");
	}
	
	void theTable(PrintWriter pw, Integer numero) {
		pw.write("<html>"
				+ "<head>"
				+ "<title>Hola, bienvenidos al multiplicador</title>"
				+ "</head>"
				+ "<body>"
				+ "<h1>Soy un generador de tablas de multiplicar</h1>"
				+ "<div>"
				+ "<form action=\"tabla-multiplicar\">"
				+ "Enter 1st number: <input type=\"text\"  name=number><br>"
				+ "<input type=\"submit\">"
				+ "</form>"
				+ "</div>"
				+ "<table style=\"width:5%\">"
				+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>1 </th>" +"<th>=</th>" + "<th>"+(numero*1) + "</th>" + "</tr>"
				+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>2 </th>" +"<th>=</th>" + "<th>"+(numero*2) + "</th>" + "</tr>"
				+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>3 </th>" +"<th>=</th>" + "<th>"+(numero*3) + "</th>" + "</tr>"
				+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>4 </th>" +"<th>=</th>" + "<th>"+(numero*4) + "</th>" + "</tr>"
				+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>5 </th>" +"<th>=</th>" + "<th>"+(numero*5) + "</th>" + "</tr>"
				+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>6 </th>" +"<th>=</th>" + "<th>"+(numero*6) + "</th>" + "</tr>"
				+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>7 </th>" +"<th>=</th>" + "<th>"+(numero*7) + "</th>" + "</tr>"
				+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>8 </th>" +"<th>=</th>" + "<th>"+(numero*8) + "</th>" + "</tr>"
				+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>9 </th>" +"<th>=</th>" + "<th>"+(numero*9) + "</th>" + "</tr>"
				+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>10</th>" +"<th>=</th>" + "<th>"+(numero*10)+ "</th>" + "</tr>"
				+ "</tdr>"
				+ "</body>"
				+ "</html>");	
	}
	
	void invalid(PrintWriter pw, String parametro) {
		pw.write("<html>"
				+ "<head>"
				+ "<title>Error de parametro</title>"
				+ "</head>"
				+ "<body>"
				+ "No se puede generarla tabla de multiplicar. Valor del parametro \"" + parametro + "\" invalido."
				+ "</body>"
				+ "</html>");
	}
	void greaterThan100(PrintWriter pw, Integer numero) {
		pw.write("<html>"
				+ "<head>"
				+ "<title>Error de parametro</title>"
				+ "</head>"
				+ "<body>"
				+ "Mi programa solo conoce la tabla de multiplicar hasta el numero 100. El numero pasado fue: "+numero
				+ "</body>"
				+ "</html>");
	}
	void negative(PrintWriter pw) {
		pw.write("<html>"
				+ "<head>"
				+ "<title>Error de parametro</title>"
				+ "</head>"
				+ "<body>"
				+ "Mi programa no sabe las tablas de multiplicar de numeros negativos"
				+ "</body>"
				+ "</html>");
	}
}