Java tutorial
/* * Copyright (C) 2017 Radosaw Karol Gre, * University of Technology and Life Sciences in Bydgoszcz, * Poland * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package app; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.DefaultComboBoxModel; import javax.swing.JOptionPane; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.io.FileUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class MainPanel extends javax.swing.JPanel { private HashMap<String, HashMap> pozycje; public MainPanel() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { buttonGroup1 = new javax.swing.ButtonGroup(); jButton1 = new javax.swing.JButton(); jComboBox1 = new javax.swing.JComboBox<>(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jComboBox2 = new javax.swing.JComboBox<>(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jLabel10 = new javax.swing.JLabel(); jLabel11 = new javax.swing.JLabel(); jLabel12 = new javax.swing.JLabel(); jLabel13 = new javax.swing.JLabel(); jLabel14 = new javax.swing.JLabel(); jLabel15 = new javax.swing.JLabel(); jRadioButton1 = new javax.swing.JRadioButton(); jRadioButton2 = new javax.swing.JRadioButton(); jComboBox3 = new javax.swing.JComboBox<>(); jComboBox4 = new javax.swing.JComboBox<>(); jComboBox5 = new javax.swing.JComboBox<>(); setBackground(new java.awt.Color(255, 255, 255)); jButton1.setText("Pobierz kursy walut"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "A", "B", "C" })); jLabel1.setText("Tabela:"); jLabel2.setText( "Tabela A kursw rednich walut obcych publikowana (aktualizowana) jest na stronie internetowej NBP w dni robocze, pomidzy godzin 11:45 a 12:15,"); jLabel3.setText( "Tabela B kursw rednich walut obcych publikowana (aktualizowana) jest na stronie internetowej NBP w rody, pomidzy godzin 11:45 a 12:15,"); jLabel4.setText( "Tabela C kursw kupna i sprzeday walut obcych oraz tabela kursw jednostek rozliczeniowych publikowane (aktualizowane) s na stronie internetowej NBP w dni robocze, pomidzy godzin 7:45 a 8:15."); jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "PLN" })); jComboBox2.setEnabled(false); jComboBox2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox2ActionPerformed(evt); } }); jLabel5.setText("Waluta:"); jLabel6.setText("Kod waluty:"); jLabel7.setText("Nazwa waluty:"); jLabel8.setText("Przelicznik:"); jLabel9.setText("Kurs redni:"); jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel14.setText("Kurs sprzeday:"); jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jRadioButton1.setBackground(new java.awt.Color(255, 255, 255)); buttonGroup1.add(jRadioButton1); jRadioButton1.setSelected(true); jRadioButton1.setText("Ostatnie"); jRadioButton2.setBackground(new java.awt.Color(255, 255, 255)); buttonGroup1.add(jRadioButton2); jRadioButton2.setText("Data:"); jComboBox3.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "2017", "2016", "2015", "2014", "2013", "2012", "2011", "2010", "2009", "2008", "2007", "2006", "2005", "2004", "2003", "2002" })); jComboBox4.setModel(new javax.swing.DefaultComboBoxModel<>( new String[] { "Stycze", "Luty", "Marzec", "Kwiecie", "Maj", "Czerwiec", "Lipiec", "Sierpie", "Wrzesie", "Padziernik", "Listopad", "Grudzie" })); jComboBox5.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" })); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addComponent(jLabel5) .addGap(79, 79, 79) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jRadioButton2).addComponent(jRadioButton1) .addComponent(jLabel2).addComponent(jLabel3).addComponent(jLabel4)) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(24, 24, 24) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18).addComponent(jLabel2).addGap(18, 18, 18).addComponent(jLabel3) .addGap(18, 18, 18).addComponent(jLabel4).addGap(18, 18, 18).addComponent(jRadioButton1) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jRadioButton2) .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(47, 47, 47) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9).addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel14).addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap())); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed if (downloadXML()) { parseLatestValues(); if (pozycje != null) { ArrayList<String> walutyTemp = new ArrayList<>(); pozycje.forEach((name, value) -> { walutyTemp.add(name); }); String[] waluty = walutyTemp.toArray(new String[walutyTemp.size()]); jComboBox2.setModel(new DefaultComboBoxModel<>(waluty)); jComboBox2.setEnabled(true); } } }//GEN-LAST:event_jButton1ActionPerformed private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed String waluta = jComboBox2.getSelectedItem().toString(); HashMap temp = pozycje.get(waluta); jLabel10.setText(temp.get("kod_waluty").toString()); jLabel11.setText(temp.get("nazwa_waluty").toString()); jLabel12.setText(temp.get("przelicznik").toString()); if (temp.containsKey("kurs_sredni")) { jLabel9.setText("Kurs redni"); jLabel13.setText(temp.get("kurs_sredni").toString()); } else { jLabel9.setText("Kurs kupna"); jLabel13.setText(temp.get("kurs_kupna").toString()); jLabel14.setText("Kurs sprzeday"); jLabel15.setText(temp.get("kurs_sprzedazy").toString()); } }//GEN-LAST:event_jComboBox2ActionPerformed private boolean downloadXML() { try { String filename = ""; if (jRadioButton1.isSelected()) { filename = "http://www.nbp.pl/kursy/xml/Last" + jComboBox1.getSelectedItem().toString() + ".xml"; } else { int year = Calendar.getInstance().get(Calendar.YEAR); String filenameDir; if (Integer.parseInt(jComboBox3.getSelectedItem().toString()) == year) { filenameDir = "http://www.nbp.pl/kursy/xml/dir.txt"; } else { filenameDir = "http://www.nbp.pl/kursy/xml/dir" + jComboBox3.getSelectedItem().toString() + ".txt"; } URL urlDir = new URL(filenameDir); File fileDir = new File("dir.txt"); FileUtils.copyURLToFile(urlDir, fileDir); String[] files = readLines("dir.txt"); String date = jComboBox3.getSelectedItem().toString(); date = date.replace("20", ""); date = date.concat(String.format("%02d", 1 + jComboBox4.getSelectedIndex())); date = date .concat(String.format("%02d", Integer.parseInt(jComboBox5.getSelectedItem().toString()))); String filenamePart = ""; for (String file : files) { if (file.contains(date)) { if (file.contains(jComboBox1.getSelectedItem().toString().toLowerCase())) { filenamePart = file; break; } } } filename = "http://www.nbp.pl/kursy/xml/" + filenamePart + ".xml"; } URL url = new URL(filename); File file = new File("temp.xml"); FileUtils.copyURLToFile(url, file); return true; } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null, "Dla tej daty brak plikw na serwerze NBP!"); } catch (MalformedURLException ex) { Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex); } return false; } public String[] readLines(String filename) throws IOException { FileReader fileReader = new FileReader(filename); BufferedReader bufferedReader = new BufferedReader(fileReader); ArrayList<String> lines = new ArrayList<String>(); String line = null; while ((line = bufferedReader.readLine()) != null) { lines.add(line); } bufferedReader.close(); return lines.toArray(new String[lines.size()]); } private void parseLatestValues() { pozycje = new HashMap<>(); try { File file = new File("temp.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(file); document.getDocumentElement().normalize(); NodeList nList = document.getElementsByTagName("tabela_kursow").item(0).getChildNodes(); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if ("pozycja".equals(eElement.getNodeName())) { NodeList hList = eElement.getChildNodes(); HashMap tempList = readNodes(hList); pozycje.put(tempList.get("kod_waluty").toString(), tempList); } } } } catch (ParserConfigurationException | SAXException | IOException ex) { Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex); } } private HashMap readNodes(NodeList hList) { HashMap<String, Object> returnValue = new HashMap<>(); for (int temp = 0; temp < hList.getLength(); temp++) { Node hNode = hList.item(temp); if (hNode.getNodeType() == Node.ELEMENT_NODE) { Element hElement = (Element) hNode; returnValue.put(hElement.getNodeName(), hElement.getTextContent()); } } return returnValue; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.ButtonGroup buttonGroup1; private javax.swing.JButton jButton1; private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JComboBox<String> jComboBox2; private javax.swing.JComboBox<String> jComboBox3; private javax.swing.JComboBox<String> jComboBox4; private javax.swing.JComboBox<String> jComboBox5; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel15; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JRadioButton jRadioButton1; private javax.swing.JRadioButton jRadioButton2; // End of variables declaration//GEN-END:variables }