|
Par
cysboy
Mise à jour : 24/07/2009
10 160 visites depuis 7 jours,
dont 282 sur ce chapitre
classé 25/786
|








1 2 3 4 5 6 7 | package com.sdz.model.exception; public class IllegalGameException extends Exception { public IllegalGameException(){ super("Vous avez choisi plusieurs fois le même numéro ! !"); } } |
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 | package com.sdz.model; import java.util.ArrayList; import com.sdz.model.exception.IllegalGameException; public class LoterieZ { /** * Liste des numéros du tirage */ private ArrayList<Integer> listNumero = new ArrayList<Integer>(); /** * Constructeur */ public LoterieZ(){ for(int i=0; i < 2; i++){ int num = (int)(Math.random() * 10); while(listNumero.contains(num)){ num = (int)(Math.random() * 10); } listNumero.add(num); } } /** * Méthode qui permet de voir si on a gagné * @param val1 * @param val2 * @param val3 * @return * @throws IllegalGameException */ public boolean match(int val1, int val2) throws IllegalGameException{ if(val1 == val2 ) throw new IllegalGameException(); else{ return listNumero.contains(new Integer(val1)) && listNumero.contains(new Integer(val2)); } } /** * Méthode qui retourne la liste des numéros du tirage * @return */ public ArrayList<Integer> getTirage(){ return this.listNumero; } } |
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 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body style="width:50%; margin:auto;"> <h2 style="text-align:center;color:white;background-color:#787878">Bienvenue à la loterieZ !</h2> <p style="text-align:center;border:1px dashed black; padding:5px;background-color:#efefef"> <span style="text-decoration:underline;font-style:italic;font-size:1.1em"> Le but du jeu est simple : </span><br /> Vous devez choisir 3 numéros différents dans les listes suivantes !<br /> Ensuite, vous n'avez plus qu'à valider et voir si vous avez gagné...<br /> Bonne chance. :) </p> <form action="tirage.do" method="post" style="text-align:center"> <% //On génère les champs. for(int i = 1; i < 3; i++){ out.println("Numéro " + i + ": <select name=\"number"+i+"\">"); for(int j = 1; j <= 10; j++){ out.println("<option value=\""+j+"\">"+ j + "</option>"); } out.println("</select><br />"); } %> <br /> <input type="submit" value="Valider" /> </form> </body> </html> |
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 | <%@ page import="java.util.ArrayList" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body style="width:50%; margin:auto;"> <h2 style="text-align:center;color:white;background-color:blue;border:2px solid black"> Bienvenue au tirage de la loterieZ !<br /> Voici le tirage d'aujourd'hui : <br /> <span style="font-size:1.4em;"> <% //On récupère les numéros de la loterie et on les affiches ArrayList<Integer> list = (ArrayList<Integer>)request.getAttribute("tirage"); for(int i = 0; i < list.size();i++){ out.println(list.get(i)); if(i < list.size() -1 ) out.println(" - "); } %> </span> </h2> <h2 style="text-align:center"> Vous aviez joué : <%=request.getAttribute("number1").toString() %> - <%=request.getAttribute("number2").toString() %><br /> <%=request.getAttribute("message").toString() %> </h2> <h2 style="text-align:center"> Vous pouvez tenter à nouveau votre chance en suivant <a href="home">ce lien</a> </h2> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body style="width:50%; margin:auto;"> <h2 style="text-align:center;color:white;background-color:red;border:2px solid black"> Une erreur est survenue : <br /> <%=request.getAttribute("error").toString() %> </h2> <h2 style="text-align:center">Vous pouvez retenter votre chance en suivant <a href="home">ce lien</a></h2> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.sdz.control; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Index extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ //Bon, là, c'est simple tout de même... request.getRequestDispatcher("index.jsp").forward(request, response); } } |
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 | package com.sdz.control; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sdz.model.LoterieZ; import com.sdz.model.exception.IllegalGameException; public class FormulaireAction extends HttpServlet { public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ LoterieZ loto = new LoterieZ(); String message = ""; try { //On teste si le client a gagné ou non if(loto.match( Integer.parseInt(request.getParameter("number1")), Integer.parseInt(request.getParameter("number2")))){ message = "Félicitations, vous avez gagné ! ! ! "; } else{ message = "Quel dommage, vous avez perdu... "; } //On affecte des attributs à notre JSP pour l'affichage request.setAttribute("message", message); request.setAttribute("number1", Integer.parseInt(request.getParameter("number1"))); request.setAttribute("number2", Integer.parseInt(request.getParameter("number2"))); request.setAttribute("tirage", loto.getTirage()); request.getRequestDispatcher("show.jsp").forward(request, response); } catch (IllegalGameException e) { //Si le contrôle à levé une exception, on prend la JSP correspondante request.setAttribute("error", e.getMessage()); request.getRequestDispatcher("error.jsp").forward(request, response); } } } |
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 | <web-app> <servlet> <servlet-class>com.sdz.control.Index</servlet-class> <servlet-name>StartPage</servlet-name> </servlet> <servlet> <servlet-class>com.sdz.control.FormulaireAction</servlet-class> <servlet-name>FormAction</servlet-name> </servlet> <servlet-mapping> <servlet-name>StartPage</servlet-name> <url-pattern>/home</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>StartPage</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>FormAction</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> |

