Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  2.         // TODO add your handling code here:
  3.         JFileChooser chooser = new JFileChooser();
  4.         chooser.setDialogTitle("Vyberte soubor ke zpracování");
  5.         chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  6.         chooser.addChoosableFileFilter(new FileNameExtensionFilter("Data v csv (*.csv)", "csv"));
  7.         int result = chooser.showOpenDialog(this);
  8.         if(result == JFileChooser.APPROVE_OPTION) {
  9.             File file = chooser.getSelectedFile();
  10.             if(file == null || !file.exists()){
  11.                 JOptionPane.showMessageDialog(this, "Chyba, soubor neexistuje!", "Chyba!", JOptionPane.ERROR_MESSAGE);
  12.                 return;
  13.             }
  14.             if(!file.canRead()){
  15.                 JOptionPane.showMessageDialog(this, "Chyba, nemáš oprávnění k souboru!", "Chyba!", JOptionPane.ERROR_MESSAGE);
  16.                 return;
  17.             }
  18.             int count = 0;
  19.             int max = Integer.MIN_VALUE;
  20.             int lastTests = -1;
  21.             try (BufferedReader br = new BufferedReader(new FileReader(file))){
  22.                 String line;
  23.                 boolean first = true;
  24.                 while ((line = br.readLine()) != null){
  25.                     if(first){
  26.                         first = false;
  27.                         continue;
  28.                     }
  29.                     String[] data = line.split(",");
  30.                     if(data.length != 3){
  31.                         continue;
  32.                     }
  33.                     //String dateRaw = data[0]; toto nepotřebujeme
  34.                     if(isNumber(data[1]) && isNumber(data[2])){
  35.                         int testsDay = getNumber(data[1]);
  36.                         int testsSum = getNumber(data[2]);
  37.                        
  38.                         /*
  39.                         Zde moc nerozumím v zadání "celkový počet položek - struktur odpovídající jednomu dni"
  40.                         Tak volím cestu "položky které mají správný syntax struktury
  41.                         */
  42.                         count++;
  43.                        
  44.                         lastTests = testsSum;
  45.                         if(testsDay > max){
  46.                             max = testsDay;
  47.                         }
  48.                     }
  49.                 }
  50.             } catch (Exception ignored){
  51.                
  52.             }
  53.             double avg = (double) lastTests / (double) count;
  54.            
  55.             String out = "";
  56.             out += "<p>Počet položek: <b>" + count + "</b></p>";
  57.             out += "<p>Poslední počet testovaných: <b>" + lastTests + "</b></p>";
  58.             out += "<p>Průměr testů: <b>" + String.format("%.2f", avg) + "</b></p>";
  59.             out += "<p>Nejvíce otestovaných za jeden dne: <b>" + max + "</b></p>";
  60.             output.setText(out);
  61.         }else{
  62.             JOptionPane.showMessageDialog(this, "Soubor nebyl vybrán...", "Chyba!", JOptionPane.ERROR_MESSAGE);
  63.         }
  64.     }                                        
  65.  
  66.     public int getNumber(String s){
  67.         try{
  68.             return Integer.parseInt(s);
  69.         }catch(Exception ignored){
  70.             return 0;
  71.         }
  72.     }
  73.    
  74.     public boolean isNumber(String s){
  75.         try{
  76.             int i = Integer.parseInt(s);
  77.             return true;
  78.         }catch(Exception ignored){
  79.             return false;
  80.         }
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement