Rukes

Untitled

May 11th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.36 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import javax.swing.JFileChooser;
  6. import javax.swing.JOptionPane;
  7. import javax.swing.filechooser.FileNameExtensionFilter;
  8.  
  9. /*
  10.  * To change this license header, choose License Headers in Project Properties.
  11.  * To change this template file, choose Tools | Templates
  12.  * and open the template in the editor.
  13.  */
  14.  
  15. /**
  16.  *
  17.  * @author rukes
  18.  */
  19. public class Main extends javax.swing.JFrame {
  20.  
  21.     /**
  22.      * Creates new form Main
  23.      */
  24.     public Main() {
  25.         initComponents();
  26.         setLocationRelativeTo(null);
  27.     }
  28.  
  29.     /**
  30.      * This method is called from within the constructor to initialize the form.
  31.      * WARNING: Do NOT modify this code. The content of this method is always
  32.      * regenerated by the Form Editor.
  33.      */
  34.     @SuppressWarnings("unchecked")
  35.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  36.     private void initComponents() {
  37.  
  38.         jButton1 = new javax.swing.JButton();
  39.         jScrollPane1 = new javax.swing.JScrollPane();
  40.         vystup = new javax.swing.JTextPane();
  41.  
  42.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  43.         setTitle("Informace z otevřených dat");
  44.         setMinimumSize(new java.awt.Dimension(450, 350));
  45.         setResizable(false);
  46.  
  47.         jButton1.setText("Zvol soubor");
  48.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  49.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  50.                 jButton1ActionPerformed(evt);
  51.             }
  52.         });
  53.  
  54.         vystup.setContentType("text/html"); // NOI18N
  55.         jScrollPane1.setViewportView(vystup);
  56.  
  57.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  58.         getContentPane().setLayout(layout);
  59.         layout.setHorizontalGroup(
  60.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  61.             .addGroup(layout.createSequentialGroup()
  62.                 .addContainerGap()
  63.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  64.                     .addComponent(jScrollPane1)
  65.                     .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
  66.                 .addContainerGap())
  67.         );
  68.         layout.setVerticalGroup(
  69.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  70.             .addGroup(layout.createSequentialGroup()
  71.                 .addContainerGap()
  72.                 .addComponent(jButton1)
  73.                 .addGap(18, 18, 18)
  74.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
  75.                 .addContainerGap())
  76.         );
  77.  
  78.         pack();
  79.     }// </editor-fold>                        
  80.  
  81.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  82.         // TODO add your handling code here:
  83.        
  84.        
  85.         JFileChooser chooser = new JFileChooser();
  86.         chooser.setDialogTitle("Vyber soubor ke zpracování");
  87.        
  88.         //MOHOU být další filtry
  89.         chooser.addChoosableFileFilter(new FileNameExtensionFilter("Data v csv (*.csv)", "csv"));
  90.         //NEMOHOU být další filtry
  91.         //chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter());
  92.        
  93.         //pokud 1 == 1?
  94.         //pokud 2 == 1?
  95.         if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
  96.            
  97.            
  98.             File file = chooser.getSelectedFile();
  99.             if(file == null || !file.exists()){
  100.                 JOptionPane.showMessageDialog(this, "Soubor neexistuje", "Chyba!", JOptionPane.ERROR_MESSAGE);
  101.                 return;
  102.             }
  103.            
  104.             if(!file.canRead()){
  105.                 JOptionPane.showMessageDialog(this, "Nemáš práva ke čtení", "Chyba!", JOptionPane.ERROR_MESSAGE);
  106.                 return;
  107.             }
  108.            
  109.             //1. úkol - celkový počet položek
  110.             int pocetPolozek = 0;
  111.             //2. úkol - Poslední kumulativní číslo provedených testů (hodnota 3)
  112.             int posledni = 0;
  113.             //3. úkol - Průměrný počet testů na 2 desetinná místa (průměr hodnoty 2) - POTŘEBUJEME SOUČET
  114.             int soucet = 0;
  115.             //4. úkol - největší (max) hodnota
  116.             int max = Integer.MIN_VALUE; //v případě min hodnoty: int min = Integer.MAX_VALUE;
  117.            
  118.            
  119.             try(BufferedReader br = new BufferedReader(new FileReader(file))) {
  120.                
  121.                 String line;
  122.                 //řádek != null -> do cyklu
  123.                 //řádek == null -> pryč
  124.                 while((line = br.readLine()) != null){
  125.                    
  126.                     //odstranit mezery na začátku a na konci
  127.                     line = line.trim(); //"jenda" -> "jenda", "  mirek  mir ek  " -> "mirek  mir ek"
  128.                    
  129.                     //1. úkol - celkový počet položek
  130.                     pocetPolozek++;
  131.                    
  132.                     //csv = datum,testy_den,testy_celkem -> 0 = datum, 1 = testy_den, 2 = testy_celkem
  133.                     String[] hodnoty = line.split(",");
  134.                    
  135.                     String datum = hodnoty[0];
  136.                     String dnesRaw = hodnoty[1];
  137.                     String celkemRaw = hodnoty[2];
  138.                    
  139.                     int dnes;
  140.                     int celkem;
  141.                     try {
  142.                         dnes = Integer.parseInt(dnesRaw);
  143.                         celkem = Integer.parseInt(celkemRaw);
  144.                     } catch (Exception ex){
  145.                         continue;
  146.                     }
  147.                    
  148.                     //2 úkol
  149.                     posledni = celkem;
  150.                    
  151.                     //3. úkol
  152.                     soucet += dnes;
  153.                     //soucet = soucet + dnes;
  154.                    
  155.                     //4. úkol
  156.                     if(dnes > max){
  157.                         max = dnes;
  158.                     }
  159.                    
  160.                 }
  161.                
  162.             } catch (Exception ex){
  163.                 JOptionPane.showMessageDialog(this, "Během čtení souboru nastala chyba :/", "Chyba!", JOptionPane.ERROR_MESSAGE);
  164.                 return;
  165.             }
  166.            
  167.             //3. úkol - výpočet průměru
  168.             double prumer = (double) soucet / pocetPolozek;
  169.             double prumer2 = soucet / pocetPolozek;
  170.            
  171.             String vysledek = "";
  172.             vysledek += "Celkový počet položek: <b>" + pocetPolozek + "</b><br>";
  173.             vysledek += "Poslední kumulativní číslo provedených testů: <b>" + posledni + "</b><br>";
  174.             vysledek += "Průměr testů na den: <b>" + String.format("%.2f", prumer) + "</b><br>";
  175.             vysledek += "Průměr testů na den: <b>" + String.format("%.2f", prumer2) + "</b><br>";
  176.             vysledek += "Maximálně testů v jednom dni: <b>" + max + "</b>";
  177.            
  178.             vystup.setText(vysledek);
  179.            
  180.         }else{
  181.             JOptionPane.showMessageDialog(this, "Soubor nebyl vybrán", "Chyba!", JOptionPane.ERROR_MESSAGE);
  182.         }
  183.        
  184.        
  185.        
  186.        
  187.        
  188.        
  189.     }                                        
  190.  
  191.     /**
  192.      * @param args the command line arguments
  193.      */
  194.     public static void main(String args[]) {
  195.         /* Set the Nimbus look and feel */
  196.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  197.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  198.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  199.          */
  200.         try {
  201.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  202.                 if ("Windows".equals(info.getName())) {
  203.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  204.                     break;
  205.                 }
  206.             }
  207.         } catch (ClassNotFoundException ex) {
  208.             java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  209.         } catch (InstantiationException ex) {
  210.             java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  211.         } catch (IllegalAccessException ex) {
  212.             java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  213.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  214.             java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  215.         }
  216.         //</editor-fold>
  217.  
  218.         /* Create and display the form */
  219.         java.awt.EventQueue.invokeLater(new Runnable() {
  220.             public void run() {
  221.                 new Main().setVisible(true);
  222.             }
  223.         });
  224.     }
  225.  
  226.     // Variables declaration - do not modify                    
  227.     private javax.swing.JButton jButton1;
  228.     private javax.swing.JScrollPane jScrollPane1;
  229.     private javax.swing.JTextPane vystup;
  230.     // End of variables declaration                  
  231. }
Add Comment
Please, Sign In to add comment