Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.gabrielluz.megasenaanalyzer; import com.gabrielluz.htmltableparser.HtmlTableParser; import org.apache.commons.lang3.math.NumberUtils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.logging.Level; import java.util.logging.Logger; import java.util.zip.DataFormatException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * * @author luzgabriel */ public class MegaSenaAnalyzer { private ArrayList<ArrayList<String>> _finalArrayTable; private ArrayList<ArrayList<Integer>> _pickedNumbers; public static byte[] downloadFile(String fileUrl) throws IOException { java.net.CookieManager cm; cm = new java.net.CookieManager(); java.net.CookieHandler.setDefault(cm); URL url = new URL(fileUrl); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); int responseCode = httpConn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { try (InputStream inputStream = httpConn.getInputStream()) { return MegaSenaAnalyzer.unZipIt(inputStream); } } else { System.out.println("No file to download. Server replied HTTP code: " + responseCode); httpConn.disconnect(); return null; } } public static byte[] unZipIt(InputStream is) throws IOException { byte[] buffer = new byte[1024]; ByteArrayOutputStream out = new ByteArrayOutputStream(); try (ZipInputStream zis = new ZipInputStream(is)) { ZipEntry ze = zis.getNextEntry(); while (ze != null) { if (ze.getName().compareToIgnoreCase("D_MEGA.HTM") == 0) { int len; while ((len = zis.read(buffer)) > 0) { out.write(buffer, 0, len); } } ze = zis.getNextEntry(); } zis.closeEntry(); } return out.toByteArray(); } public void openLotteryHtml() throws DataFormatException { try { String resultsUrl = "http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip"; HtmlTableParser tableParser = new HtmlTableParser(); String strTableFile = new String(MegaSenaAnalyzer.downloadFile(resultsUrl), Charset.defaultCharset()); tableParser.parse(strTableFile); String[][] arrayTableFile = tableParser.getContentsArray(); //arrayTableFIle transformed to ArrayList finalArrayTable this._finalArrayTable = new ArrayList<>(); for (String[] str1 : arrayTableFile) { this._finalArrayTable.add(new ArrayList<>(Arrays.asList(str1))); } //Removing useless data for (int i = 0; i < this._finalArrayTable.size(); i++) { ArrayList<String> a = this._finalArrayTable.get(i); a.remove(10); //Remove city a.remove(10); //Remove state if (!NumberUtils.isNumber(a.get(0))) { this._finalArrayTable.remove(i); i--; } } System.out.println("_finalArrayTable = " + _finalArrayTable.size()); //Set all picked numbers this._pickedNumbers = new ArrayList<>(); ArrayList<Integer> tempArray; for (int i = 0; i < this._finalArrayTable.size(); i++) { tempArray = new ArrayList<>(); tempArray.add(Integer.parseInt(this._finalArrayTable.get(i).get(2))); tempArray.add(Integer.parseInt(this._finalArrayTable.get(i).get(3))); tempArray.add(Integer.parseInt(this._finalArrayTable.get(i).get(4))); tempArray.add(Integer.parseInt(this._finalArrayTable.get(i).get(5))); tempArray.add(Integer.parseInt(this._finalArrayTable.get(i).get(6))); tempArray.add(Integer.parseInt(this._finalArrayTable.get(i).get(7))); Collections.sort(tempArray); this._pickedNumbers.add(tempArray); } } catch (IOException ex) { Logger.getLogger(MegaSenaAnalyzer.class.getName()).log(Level.SEVERE, null, ex); } } public ArrayList<ArrayList<Integer>> getPickedNumbers() { return this._pickedNumbers; } //public ArrayList<String[]> public boolean hasAlreadyWon(int a1, int a2, int a3, int a4, int a5, int a6) { boolean won = false; ArrayList<Integer> inputArray = new ArrayList<>(); inputArray.add(a1); inputArray.add(a2); inputArray.add(a3); inputArray.add(a4); inputArray.add(a5); inputArray.add(a6); Collections.sort(inputArray); for (ArrayList<Integer> temp : this._pickedNumbers) { if (temp.equals(inputArray)) { won = true; } } return won; } public Integer[] getTimesNumbersPicked() { return this.getTimesNumbersPicked(this._pickedNumbers.size()); } public Integer[] getTimesNumbersPicked(int ultimosSorteios) { Integer[] counterArray = new Integer[60]; int temp = (this._pickedNumbers.size() - ultimosSorteios); for (int i = 0; i < counterArray.length; i++) { counterArray[i] = 0; } for (int i = temp; i < this._pickedNumbers.size(); i++) { ArrayList<Integer> intArray2 = this._pickedNumbers.get(i); intArray2.stream().forEach((Integer j) -> { counterArray[j - 1]++; }); } return counterArray; } }