Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- import javax.swing.filechooser.FileNameExtensionFilter;
- /*
- * 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.
- */
- /**
- *
- * @author rukes
- */
- public class Main extends javax.swing.JFrame {
- /**
- * Creates new form Main
- */
- public Main() {
- initComponents();
- setLocationRelativeTo(null);
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jButton1 = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- vystup = new javax.swing.JTextPane();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Informace z otevřených dat");
- setMinimumSize(new java.awt.Dimension(450, 350));
- setResizable(false);
- jButton1.setText("Zvol soubor");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- vystup.setContentType("text/html"); // NOI18N
- jScrollPane1.setViewportView(vystup);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1)
- .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jButton1)
- .addGap(18, 18, 18)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- JFileChooser chooser = new JFileChooser();
- chooser.setDialogTitle("Vyber soubor ke zpracování");
- //MOHOU být další filtry
- chooser.addChoosableFileFilter(new FileNameExtensionFilter("Data v csv (*.csv)", "csv"));
- //NEMOHOU být další filtry
- //chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter());
- //pokud 1 == 1?
- //pokud 2 == 1?
- if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
- File file = chooser.getSelectedFile();
- if(file == null || !file.exists()){
- JOptionPane.showMessageDialog(this, "Soubor neexistuje", "Chyba!", JOptionPane.ERROR_MESSAGE);
- return;
- }
- if(!file.canRead()){
- JOptionPane.showMessageDialog(this, "Nemáš práva ke čtení", "Chyba!", JOptionPane.ERROR_MESSAGE);
- return;
- }
- //1. úkol - celkový počet položek
- int pocetPolozek = 0;
- //2. úkol - Poslední kumulativní číslo provedených testů (hodnota 3)
- int posledni = 0;
- //3. úkol - Průměrný počet testů na 2 desetinná místa (průměr hodnoty 2) - POTŘEBUJEME SOUČET
- int soucet = 0;
- //4. úkol - největší (max) hodnota
- int max = Integer.MIN_VALUE; //v případě min hodnoty: int min = Integer.MAX_VALUE;
- try(BufferedReader br = new BufferedReader(new FileReader(file))) {
- String line;
- //řádek != null -> do cyklu
- //řádek == null -> pryč
- while((line = br.readLine()) != null){
- //odstranit mezery na začátku a na konci
- line = line.trim(); //"jenda" -> "jenda", " mirek mir ek " -> "mirek mir ek"
- //1. úkol - celkový počet položek
- pocetPolozek++;
- //csv = datum,testy_den,testy_celkem -> 0 = datum, 1 = testy_den, 2 = testy_celkem
- String[] hodnoty = line.split(",");
- String datum = hodnoty[0];
- String dnesRaw = hodnoty[1];
- String celkemRaw = hodnoty[2];
- int dnes;
- int celkem;
- try {
- dnes = Integer.parseInt(dnesRaw);
- celkem = Integer.parseInt(celkemRaw);
- } catch (Exception ex){
- continue;
- }
- //2 úkol
- posledni = celkem;
- //3. úkol
- soucet += dnes;
- //soucet = soucet + dnes;
- //4. úkol
- if(dnes > max){
- max = dnes;
- }
- }
- } catch (Exception ex){
- JOptionPane.showMessageDialog(this, "Během čtení souboru nastala chyba :/", "Chyba!", JOptionPane.ERROR_MESSAGE);
- return;
- }
- //3. úkol - výpočet průměru
- double prumer = (double) soucet / pocetPolozek;
- double prumer2 = soucet / pocetPolozek;
- String vysledek = "";
- vysledek += "Celkový počet položek: <b>" + pocetPolozek + "</b><br>";
- vysledek += "Poslední kumulativní číslo provedených testů: <b>" + posledni + "</b><br>";
- vysledek += "Průměr testů na den: <b>" + String.format("%.2f", prumer) + "</b><br>";
- vysledek += "Průměr testů na den: <b>" + String.format("%.2f", prumer2) + "</b><br>";
- vysledek += "Maximálně testů v jednom dni: <b>" + max + "</b>";
- vystup.setText(vysledek);
- }else{
- JOptionPane.showMessageDialog(this, "Soubor nebyl vybrán", "Chyba!", JOptionPane.ERROR_MESSAGE);
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Windows".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Main().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTextPane vystup;
- // End of variables declaration
- }
Add Comment
Please, Sign In to add comment