{"id":6854,"date":"2014-06-01T14:02:09","date_gmt":"2014-06-01T12:02:09","guid":{"rendered":"http:\/\/www.palentino.es\/blog\/?p=6854"},"modified":"2014-06-01T14:15:56","modified_gmt":"2014-06-01T12:15:56","slug":"ejemplo-formativo-java-aplicacion-loterias-y-apuestas","status":"publish","type":"post","link":"https:\/\/www.palentino.es\/blog\/ejemplo-formativo-java-aplicacion-loterias-y-apuestas\/","title":{"rendered":"Ejemplo formativo JAVA, aplicaci\u00f3n Loter\u00edas y Apuestas."},"content":{"rendered":"<p style=\"text-align: justify;\">En esta entrada dejo un ejemplo formativo de Aplicaci\u00f3n creada en<strong> <span style=\"color: #000080;\">JAVA-J2SE<\/span><\/strong> (IDE \u00a0Netbeans).<br \/>\nPermite realizar juegos de <strong>primitiva, quiniela y loter\u00edas, aleatorios y manuales<\/strong>. Ideal para prop\u00f3sitos formativos. Aprendizaje de las t\u00e9cnicas de programaci\u00f3n.<\/p>\n<p>Se encuentra compuesta por <strong>2 clases en un paquete llamado Aplicaci\u00f3nJuego<\/strong>.<\/p>\n<p>Veamos c\u00f3mo resolver el problema&#8230;<\/p>\n<p><!--more--><\/p>\n<p><strong>AplicacionJuego.java<\/strong><\/p>\n<pre class=\"lang:java decode:true\">package AplicacionJuego;\r\n\r\n\r\nimport java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\nimport java.util.Date;\r\nimport java.util.logging.Level;\r\nimport java.util.logging.Logger;\r\n\r\n\r\n\/**\r\n *\r\n * @author Oscar de la Cuesta Campillo\r\n *\/\r\npublic class AplicacionJuego {\r\n    \r\n     static  Date fecha;\r\n     static double totalapuestas=0;\r\n   \r\n     public static void main(String[] args) {\r\n        try { \r\n            Juego mijuego = new Juego();\r\n            BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));\r\n            String texto=\"\";\r\n            char opcion=' ';\r\n            do {\r\n                fecha=new Date();\r\n                System.out.println(\"**********************\");\r\n                System.out.println(\"LOTERIAS Y APUESTAS\");\r\n                System.out.println(\"**********************\");\r\n                System.out.println(\"1 - Primitiva.\");\r\n                System.out.println(\"2 - Quiniela.\");\r\n                System.out.println(\"3 - Loteria\");\r\n                System.out.println(\"0 - Salir\");\r\n                System.out.println(\"\\nElige juego:\");\r\n                texto=teclado.readLine();\r\n                opcion=texto.charAt(0);\r\n                if (opcion=='1'){\r\n                    \/\/Llamo al m\u00e9todo juego de la Primitiva          \r\n                    mijuego.primitiva();\r\n                    }\r\n                 if (opcion=='2'){\r\n                    \/\/Llamo al m\u00e9todo juego de la Quiniela \r\n                      mijuego.quiniela();\r\n                    }\r\n                  if (opcion=='3'){\r\n                    \/\/Llamo al m\u00e9todo juego de la Loter\u00eda Nacional\r\n                      mijuego.loteria_nacional();\r\n                    }\r\n               \r\n            } while ( opcion!='0' );\r\n            \r\n            \/\/ Mostramos el total mediante la variable de clase tipo static (no se han instanciado objetos.\r\n            System.out.println(\"........................\");\r\n            System.out.println(\"Total a Pagar: \" + Double.toString(mijuego.totalapuestas) + \" \u20ac\");\r\n            \r\n            \r\n        } catch (IOException ex) {\r\n            Logger.getLogger(Juego.class.getName()).log(Level.SEVERE, null, ex);\r\n        }\r\n    }\r\n}\r\n \r\n<\/pre>\n<p>Juego.java<\/p>\n<pre class=\"lang:java decode:true\">package AplicacionJuego;\r\nimport com.sun.org.apache.bcel.internal.generic.InstructionConstants;\r\nimport java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.logging.Level;\r\nimport java.util.logging.Logger;\r\nimport java.util.Date;\r\n\/\/ Clases a\u00f1adidas para el correcto funcionamiento del programa\r\nimport java.util.Calendar;\r\nimport java.util.GregorianCalendar;\r\n\/\/ Para los numeros aleatorios\r\nimport java.util.Random;\r\n\/**\r\n *\r\n * @author Oscar de la Cuesta - palentino.es\r\n * Creado con NetBeans\r\n *\/\r\npublic class Juego {\r\n  \/\/Creo una variable de clase para almacenar el total. Esta variable es visible para el resto de m\u00e9todos de la clase\r\n    static  String fechacadena;\r\n    static  Date fecha;\r\n    static double totalapuestas=0;\r\n \r\n    private static void borrar_pantalla()\r\n    {\r\n      for ( int i=0;i&lt;50;i++)\r\n            System.out.println(\"\\n\");\r\n       \r\n    }\r\n    private static void tecla_para_continuar() throws IOException\r\n    {\r\n        String texto;\r\n        BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));\r\n        System.out.println(\"\\nPulse una tecla para continuar ...\");\r\n        texto=teclado.readLine();\r\n    }\r\n    \r\n    \r\n    public static String sumar_fechas_dias(Date fch, int dias) {\r\n        Calendar cal = new GregorianCalendar();\r\n        cal.setTimeInMillis(fch.getTime());\r\n        cal.add(Calendar.DATE, dias);\r\n        \/\/ Direccion  para ver los delimitadores http:\/\/javatechniques.com\/blog\/dateformat-and-simpledateformat-examples\/\r\n        SimpleDateFormat formateador = new SimpleDateFormat(\"EEEE dd 'de' MMMM 'de' yyyy\");\r\n        String formateada = formateador.format(cal.getTime());\r\n      return formateada;\r\n       \r\n    }\r\n       \r\n    \/\/ Metodo de clase que calcula el dia del sorteo.\r\n    private static String dia_del_sorteo (Date dia_actual, int tipo_sorteo)\r\n    {\r\n       \r\n      Date fecha;\r\n      fecha=new Date();\r\n      int Dias_A_Anadir=0;\r\n      String fechacadena,diaactual;\r\n      fechacadena=fecha.toString();\r\n      fechacadena=fechacadena.substring(0,3).toLowerCase();\r\n      \r\n      if (tipo_sorteo==1) \/\/ Sorteo de la primitiva\r\n      {\r\n         if (fechacadena.equals(\"thu\"))\r\n         { \r\n       \r\n          return fecha.toString();\r\n         }\r\n        else\r\n         {\r\n            if (fechacadena.equals(\"mon\"))\r\n             Dias_A_Anadir=3;\r\n            if (fechacadena.equals(\"tue\"))\r\n             Dias_A_Anadir=2;\r\n            if (fechacadena.equals(\"wed\"))\r\n             Dias_A_Anadir=1;\r\n            if (fechacadena.equals(\"fri\"))\r\n             Dias_A_Anadir=6;\r\n            if (fechacadena.equals(\"sat\"))\r\n             Dias_A_Anadir=5;\r\n            if (fechacadena.equals(\"sun\"))\r\n             Dias_A_Anadir=4;  \r\n         }\r\n      }\r\n      \r\n      if (tipo_sorteo==2) \/\/ Sorteo de la Quiniela\r\n      {\r\n         if (fechacadena.equals(\"sun\"))\r\n         { \r\n          System.out.println(fechacadena);\r\n          return fecha.toString();\r\n         }\r\n        else\r\n         {\r\n            if (fechacadena.equals(\"mon\"))\r\n             Dias_A_Anadir=6;\r\n            if (fechacadena.equals(\"tue\"))\r\n             Dias_A_Anadir=5;\r\n            if (fechacadena.equals(\"wed\"))\r\n             Dias_A_Anadir=4;\r\n            if (fechacadena.equals(\"thu\"))\r\n             Dias_A_Anadir=3;\r\n            if (fechacadena.equals(\"fri\"))\r\n             Dias_A_Anadir=2;\r\n            if (fechacadena.equals(\"sat\"))\r\n             Dias_A_Anadir=1;  \r\n         }\r\n      }\r\n      \r\n        if (tipo_sorteo==3) \/\/ Sorteo de la Loteria Nacional\r\n      {\r\n         if (fechacadena.equals(\"sat\"))\r\n         { \r\n          System.out.println(fechacadena);\r\n          return fecha.toString();\r\n         }\r\n        else\r\n         {\r\n            if (fechacadena.equals(\"mon\"))\r\n             Dias_A_Anadir=5;\r\n            if (fechacadena.equals(\"tue\"))\r\n             Dias_A_Anadir=4;\r\n            if (fechacadena.equals(\"wed\"))\r\n             Dias_A_Anadir=3;\r\n            if (fechacadena.equals(\"thu\"))\r\n             Dias_A_Anadir=2;\r\n            if (fechacadena.equals(\"fri\"))\r\n             Dias_A_Anadir=1;\r\n            if (fechacadena.equals(\"sun\"))\r\n             Dias_A_Anadir=6;  \r\n         }\r\n      }\r\n    \r\n    return sumar_fechas_dias(fecha, Dias_A_Anadir); \r\n    \r\n    }\r\n     \r\n    public static void primitiva() throws IOException{\/\/En este m\u00e9todo de clase resolveremos el problema de la Primitiva   \r\n      borrar_pantalla();\r\n      fechacadena = dia_del_sorteo(fecha,1).toString();\r\n      \/\/ Seguimos con lo que pide el enunciado.\r\n       System.out.println(\"Pr\u00f3ximo Sorteo :\" + fechacadena);  \r\n       int NumerosPrimitiva;\r\n       \r\n       \/\/inicializamos los numeros del array\r\n        System.out.print(\"Apuesta: \");\r\n       for (int i=0;i&lt;=7;i++)\r\n       {\r\n           NumerosPrimitiva=(int) Math.floor(Math.random()*49 +1);\r\n           if (i&lt;6)\r\n               System.out.print(Integer.toString(NumerosPrimitiva)+ \" \");\r\n           if (i==6) \/\/ Para el complementario\r\n               System.out.println(\"\\nComplementario:\" +  Integer.toString(NumerosPrimitiva)+ \" \");\r\n           if (i==7)  \r\n           {\r\n               NumerosPrimitiva=(int) Math.floor(Math.random()*9 );\r\n               System.out.print(\"Reintegro:\" + Integer.toString(NumerosPrimitiva)+ \" \");\r\n           }\r\n       }\r\n        \/\/ Sumamos el coste de la apuesta\r\n         totalapuestas++;\r\n         tecla_para_continuar();\r\n    }\r\n  \r\n    \/\/En este m\u00e9todo de clase resolveremos el problema de la Quiniela.\r\n    public static void quiniela() throws IOException\r\n    {\r\n      borrar_pantalla();\r\n      fechacadena = dia_del_sorteo(fecha,2).toString();\r\n      System.out.println(\"Pr\u00f3xima Jornada :\" + fechacadena);  \r\n      System.out.print(\"\u00bfCu\u00e1ntas apuestas quieres (2-8)?\");\r\n      BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));\r\n      int opcion=0;\r\n      String texto=\"\";\r\n      char caracter;\r\n      do\r\n      {\r\n      texto=teclado.readLine();\r\n      caracter= texto.charAt(0);\r\n      opcion=caracter-48; \/\/ 48 es el valor ASCII de 0\r\n      if (opcion&lt;=1 || opcion&gt;=8)\r\n               System.out.println(\"N\u00famero de apuestas incorrecto\");\r\n               System.out.println(caracter);\r\n      }\r\n      while (opcion&lt;=1 || opcion&gt;=8);\r\n      \r\n      int ApuestasX12;\r\n      \/\/ApuestasX12 = new int [15];\r\n      for(int i=0;i&lt;opcion;i++)\r\n         {\r\n             System.out.print(\"Apuesta \" + Integer.toString(i+1) + \":\");\r\n             for (int j=0;j&lt;=14;j++)\r\n               {\r\n                 ApuestasX12=(int) Math.floor(Math.random()*3+1 );\r\n                 if (ApuestasX12==1)\r\n                       System.out.print(\"1 \");\r\n                 if (ApuestasX12==2)\r\n                      System.out.print(\"X \");\r\n                 if (ApuestasX12==3)\r\n                      System.out.print(\"3 \");\r\n               }\r\n             System.out.println(\"\");\r\n         }\r\n      \/\/Calculamos las apuestas en funci\u00f3n del numero de opciones introducido\r\n      tecla_para_continuar();\r\n      totalapuestas=totalapuestas + (0.5)*opcion;\r\n    }\r\n    \r\n    \/\/En este m\u00e9todo de clase resolveremos el problema de la Loteria Nacional\r\n    public static void loteria_nacional() throws IOException\r\n    {\r\n      borrar_pantalla();\r\n      fechacadena = dia_del_sorteo(fecha,3).toString();\r\n      System.out.println(\"Pr\u00f3ximo Sorteo :\" + fechacadena);  \r\n      System.out.println(\"Elige la terminaci\u00f3n hasta 3 cifras: (ENTER si te da igual):\");\r\n      BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));\r\n      String texto=\"\";\r\n      texto=teclado.readLine();\r\n      int numero;\r\n      \/\/ En caso de ser ENTER, todos los numeros, controlo si ya ha aparecido, numeros hasta el 70000\r\n      if (texto.isEmpty())\r\n            {\r\n                \r\n                numero=(int) Math.floor(Math.random()*70000+1);\r\n                       \r\n            }   \r\n      else\r\n            {\r\n                \/\/ Comprobamos cuantos digitos ha metido\r\n                if (texto.length()== 3)  \r\n                {\r\n                    String numeroconstruido;\r\n                    int numero1,numero2;\r\n                    numero1=(int)(Math.floor(Math.random()*9));\r\n                    numero2=(int)(Math.floor(Math.random()*9));\r\n                    numeroconstruido= Integer.toString(numero1)+ Integer.toString(numero2)+ texto;\r\n                    System.out.println(\"Numero con el que juegas: \" + numeroconstruido);\r\n                }\r\n                if (texto.length()== 2)  \r\n                {\r\n                    String numeroconstruido;\r\n                    int numero1,numero2,numero3;\r\n                    numero1=(int)(Math.floor(Math.random()*9));\r\n                    numero2=(int)(Math.floor(Math.random()*9));\r\n                    numero3=(int)(Math.floor(Math.random()*9));\r\n                    numeroconstruido= Integer.toString(numero1)+ Integer.toString(numero2)+ Integer.toString(numero3)+ texto;\r\n                    System.out.println(\"Numero con el que juegas: \" + numeroconstruido);\r\n                }\r\n                if (texto.length()== 1)  \r\n                {\r\n                    String numeroconstruido;\r\n                    int numero1,numero2,numero3, numero4;\r\n                    numero1=(int)(Math.floor(Math.random()*9));\r\n                    numero2=(int)(Math.floor(Math.random()*9));\r\n                    numero3=(int)(Math.floor(Math.random()*9));\r\n                    numero4=(int)(Math.floor(Math.random()*9));\r\n                    numeroconstruido= Integer.toString(numero1)+ Integer.toString(numero2)+ Integer.toString(numero3)+  Integer.toString(numero4)+texto;\r\n                    System.out.println(\"Numero con el que juegas: \" + numeroconstruido);\r\n                    \r\n                    \r\n                }\r\n            }\r\n      tecla_para_continuar(); \r\n      totalapuestas=totalapuestas + 12;\r\n    }\r\n    \r\n\r\n}\r\n<\/pre>\n<p>Espero que os sirva.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>En esta entrada dejo un ejemplo formativo de Aplicaci\u00f3n creada en JAVA-J2SE (IDE \u00a0Netbeans). Permite realizar juegos de primitiva, quiniela y loter\u00edas, aleatorios y manuales. Ideal para prop\u00f3sitos formativos. Aprendizaje de las t\u00e9cnicas de programaci\u00f3n. Se encuentra compuesta por 2 clases en un paquete llamado Aplicaci\u00f3nJuego. Veamos c\u00f3mo resolver el problema&#8230;<\/p>\n","protected":false},"author":1,"featured_media":6855,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[492,5],"tags":[57,44],"class_list":["post-6854","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","category-programacion","tag-java","tag-programacion-2"],"_links":{"self":[{"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/posts\/6854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/comments?post=6854"}],"version-history":[{"count":7,"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/posts\/6854\/revisions"}],"predecessor-version":[{"id":6862,"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/posts\/6854\/revisions\/6862"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/media\/6855"}],"wp:attachment":[{"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/media?parent=6854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/categories?post=6854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.palentino.es\/blog\/wp-json\/wp\/v2\/tags?post=6854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}