Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.89 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package GUI;
  7.  
  8. import JDBC.DB_Access;
  9. import MAIN.DB_VIEW;
  10. import java.net.URL;
  11. import java.sql.Connection;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import java.util.ResourceBundle;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import javafx.collections.FXCollections;
  20. import javafx.collections.ObservableList;
  21. import javafx.collections.transformation.*;
  22. import javafx.event.ActionEvent;
  23. import javafx.fxml.FXML;
  24. import javafx.fxml.Initializable;
  25. import javafx.scene.control.Button;
  26. import javafx.scene.control.TableColumn;
  27. import javafx.scene.control.TableView;
  28. import javafx.scene.control.TextField;
  29. import javafx.scene.control.cell.PropertyValueFactory;
  30. import javafx.scene.input.MouseEvent;
  31. import javafx.scene.input.TouchEvent;
  32.  
  33. /**
  34.  * FXML Controller class
  35.  *
  36.  * @author Kamil
  37.  */
  38. public class SklepController implements Initializable {
  39.    
  40.     private int ID_Przedmiotu;
  41.     private ObservableList<DB_VIEW> obList = FXCollections.observableArrayList();
  42.    
  43.      public void updateList() throws ClassNotFoundException, SQLException
  44.     {
  45.         obList.clear();
  46.         //System.out.println("Załadowane poprawnie dane do tabeli.");
  47.         ResultSet rs = DB_Access.executeSelect("SELECT * FROM Magazyn");
  48.         try{
  49.             while(rs.next()){
  50.         //System.out.println("Nazwa: "+rs.getString("Nazwa_Przedmiotu")+" Rodzaj: "+ rs.getString("Rodzaj_Przedmiotu"));
  51.                 obList.add(new DB_VIEW(rs.getString("Nazwa_Przedmiotu"), rs.getString("Rodzaj_Przedmiotu"),
  52.                      rs.getDouble("Cena_Przedmiotu"),(int) rs.getDouble("Ilosc"), (int) rs.getDouble("ID_Przedmiotu")));
  53.             }
  54.         }catch(SQLException e){
  55.             throw new RuntimeException(e.getMessage());
  56.         }
  57.         TableView.setItems(obList);
  58.     }
  59.      public void refreshScene(){
  60.          SceneManager.renderScene("Sklep");
  61.      }
  62.    
  63.    
  64.     @FXML
  65.     private Button Przycisk_Aktualizuj;
  66.     @FXML
  67.     private Button Przycisk_Dodaj;
  68.     @FXML
  69.     private TextField TextField_Wyszukiwanie;
  70.     @FXML
  71.     private Button Przycisk_Menu_5;
  72.     @FXML
  73.     private TableColumn<DB_VIEW, String> Kolumna_Nazwa;
  74.     @FXML
  75.     private TableColumn<DB_VIEW, String> Kolumna_Rodzaj;
  76.     @FXML
  77.     private TableColumn<DB_VIEW, Double> Kolumna_Ilość_Sztuk;
  78.     @FXML
  79.     private TableView<DB_VIEW> TableView;
  80.     @FXML
  81.     private TableColumn<DB_VIEW, Double> Kolumna_Cena;
  82.     @FXML
  83.     private TextField TextField_Nazwa;
  84.     @FXML
  85.     private TextField TextField_Rodzaj;
  86.     @FXML
  87.     private TextField TextField_Ilosc_Szt;
  88.     @FXML
  89.     private TextField TextField_Cena;
  90.     @FXML
  91.     private Button Przycisk_Delete;
  92.    
  93.    
  94.    
  95.    
  96.     @Override
  97.     public void initialize(URL url, ResourceBundle rb) {
  98.        
  99.         Kolumna_Nazwa.setCellValueFactory(new PropertyValueFactory<>("Nazwa_Przedmiotu"));
  100.         Kolumna_Rodzaj.setCellValueFactory(new PropertyValueFactory<>("Rodzaj_Przedmiotu"));
  101.         Kolumna_Ilość_Sztuk.setCellValueFactory(new PropertyValueFactory<>("Ilosc"));
  102.         Kolumna_Cena.setCellValueFactory(new PropertyValueFactory<>("Cena_Przedmiotu"));
  103.        
  104.         try {
  105.             updateList();
  106.         } catch (ClassNotFoundException ex) {
  107.             Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
  108.         } catch (SQLException ex) {
  109.             Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
  110.         }
  111.        
  112.        
  113.        
  114.         FilteredList<DB_VIEW> filteredData = new FilteredList<>(obList, b -> true);
  115.        
  116.         TextField_Wyszukiwanie.textProperty().addListener((observable, oldValue, newValue) -> {
  117.             filteredData.setPredicate(DB_VIEW -> {
  118.                                
  119.                 if (newValue == null || newValue.isEmpty()) {
  120.                     return true;
  121.                 }
  122.                
  123.                 String lowerCaseFilter = newValue.toLowerCase();
  124.                
  125.                 if (DB_VIEW.getNazwa_Przedmiotu().toLowerCase().contains(lowerCaseFilter)) {
  126.                     return true;
  127.                 } else if (DB_VIEW.getRodzaj_Przedmiotu().toLowerCase().contains(lowerCaseFilter)) {
  128.                     return true;
  129.                 }
  130.                                 else if (String.valueOf(DB_VIEW.getCena_Przedmiotu()).toLowerCase().contains(lowerCaseFilter)){
  131.                      return true;
  132.                                 }
  133.                 else if (String.valueOf(DB_VIEW.getIlosc()).toLowerCase().contains(lowerCaseFilter))
  134.                      return true;
  135.                 else  
  136.                         return false;
  137.             });
  138.         });
  139.        
  140.         SortedList<DB_VIEW> sortedData = new SortedList<>(filteredData);
  141.         sortedData.comparatorProperty().bind(TableView.comparatorProperty());
  142.         TableView.setItems(sortedData);
  143.                
  144.  
  145.     }    
  146.  
  147.     @FXML
  148.     private void Idz_do_Menu(ActionEvent event) {
  149.         SceneManager.renderScene("Menu");
  150.     }
  151.    
  152.     @FXML
  153.     private void Usuń_Dane(ActionEvent event) throws ClassNotFoundException, SQLException {
  154.        
  155.              try{
  156.             int ID = obList.size();  
  157.             String Nazwa=TextField_Nazwa.getText();
  158.             String Rodzaj=TextField_Rodzaj.getText();
  159.             String Ilosc=TextField_Ilosc_Szt.getText();
  160.             String Cena=TextField_Cena.getText();
  161.            
  162. //            String ZabezpieczeniaT = "[A-Za-z%](?!^\\d+$)^.+$";
  163. //            String ZabezpieczeniaI = "[0-9\\.\\,]";
  164. //            
  165. //            if(!Nazwa.matches(ZabezpieczeniaT)||!Rodzaj.matches(ZabezpieczeniaT)){
  166. //                System.out.println("Wprowadzaono złe dane!");
  167. //                return ;
  168. //            }
  169. //            if(!Ilosc.matches(ZabezpieczeniaI)||!Cena.matches(ZabezpieczeniaI)){
  170. //                System.out.println("Wprowadzaono złe dane!");
  171. //                return ;
  172. //            }
  173.            
  174.            
  175.             String sqlA = "DELETE FROM Magazyn WHERE Nazwa_Przedmiotu=? AND Rodzaj_Przedmiotu=? AND Ilosc=? AND Cena_Przedmiotu=?";
  176.  
  177.            
  178.             try{
  179.                 Connection connection = DB_Access.connect();
  180.    
  181.             PreparedStatement STMTA = connection.prepareStatement(sqlA); {
  182.            
  183.                 Double.parseDouble(Ilosc);
  184.                 Double.parseDouble(Cena);
  185.                
  186.                 STMTA.setString(1, Nazwa);
  187.                 STMTA.setString(2, Rodzaj);
  188.                 STMTA.setString(3, Ilosc);
  189.                 STMTA.setString(4, Cena);
  190.                
  191.                
  192.             STMTA.executeUpdate();
  193.                 System.out.println("Usunięto poprawnie przedmiot! ");
  194.             refreshScene();
  195.         }
  196.            
  197.            
  198.            
  199.             try {
  200.             updateList();
  201.         } catch (ClassNotFoundException ex) {
  202.             Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
  203.         } catch (SQLException ex) {
  204.             Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
  205.         }
  206.      
  207.             }
  208.             catch(SQLException e) {
  209.             System.out.println(e.getMessage());
  210.         }
  211.         }
  212.         catch (ClassNotFoundException ex) {
  213.             Logger.getLogger(ProfilController.class.getName()).log(Level.SEVERE, null, ex);
  214.         }
  215.     }
  216.  
  217.     @FXML
  218.     private void Dodaj_Dane(ActionEvent event) throws ClassNotFoundException, SQLException {
  219.            
  220.        try{
  221.            
  222.             int N_id = obList.size()+1;
  223.             String NowaNazwa=TextField_Nazwa.getText();
  224.             String NowyRodzaj=TextField_Rodzaj.getText();
  225.             String NowaIlosc=TextField_Ilosc_Szt.getText();
  226.             String NowaCena=TextField_Cena.getText();
  227.            
  228. //            String ZabezpieczeniaT = "[A-Za-z%](?!^\\d+$)^.+$";
  229. //            String ZabezpieczeniaI = "[0-9\\.\\,]";
  230. //            
  231. //            if(!NowaNazwa.matches(ZabezpieczeniaT)||!NowyRodzaj.matches(ZabezpieczeniaT)){
  232. //                System.out.println("Wprowadzaono złe dane!");
  233. //                return ;
  234. //            }
  235. //            if(!NowaIlosc.matches(ZabezpieczeniaI)||!NowaCena.matches(ZabezpieczeniaI)){
  236. //                System.out.println("Wprowadzaono złe dane!");
  237. //                return ;
  238. //            }
  239.            
  240.             String sql = "INSERT INTO Magazyn(ID_Przedmiotu, Nazwa_Przedmiotu,Rodzaj_Przedmiotu,Ilosc,Cena_Przedmiotu) VALUES(?,?,?,?,?)";
  241.            
  242.             if(!obList.isEmpty()){
  243.                
  244.                 obList.clear();
  245.             }
  246.             try{
  247.                 Connection connection = DB_Access.connect();
  248.                
  249.             PreparedStatement pstmt = connection.prepareStatement(sql); {
  250.            
  251.                 Double.parseDouble(NowaIlosc);
  252.                
  253.             pstmt.setInt(1, N_id);
  254.             pstmt.setString(2, NowaNazwa);
  255.             pstmt.setString(3, NowyRodzaj);
  256.             pstmt.setString(4, NowaIlosc);
  257.             pstmt.setString(5, NowaCena);
  258.  
  259.             pstmt.executeUpdate();
  260.                 System.out.println("Dodano poprawnie przedmiot! ");
  261.                 refreshScene();
  262.         }
  263.             try {
  264.             updateList();
  265.         } catch (ClassNotFoundException ex) {
  266.             Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
  267.         } catch (SQLException ex) {
  268.             Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
  269.         }
  270.      
  271.             }
  272.             catch(SQLException e) {
  273.             System.out.println(e.getMessage());
  274.         }
  275.  
  276.         }
  277.         catch (ClassNotFoundException ex) {
  278.             Logger.getLogger(ProfilController.class.getName()).log(Level.SEVERE, null, ex);
  279.         }
  280.     }
  281.  
  282.     @FXML
  283.     private void Aktualizuj_Dane(ActionEvent event) {
  284.        
  285.         try{
  286.            
  287.             String NazwaUpdate=TextField_Nazwa.getText();
  288.             String RodzajUpdate=TextField_Rodzaj.getText();
  289.             String IloscUpdate=TextField_Ilosc_Szt.getText();
  290.             String CenaUpdate=TextField_Cena.getText();
  291.            
  292. //            String ZabezpieczeniaT = "[A-Za-z%](?!^\\d+$)^.+$";
  293. //            String ZabezpieczeniaI = "[0-9\\.\\,]";
  294. //            
  295. //            if(!NazwaUpdate.matches(ZabezpieczeniaT)||!RodzajUpdate.matches(ZabezpieczeniaT)){
  296. //                System.out.println("Wprowadzaono złe dane!");
  297. //                return ;
  298. //            }
  299. //            if(!IloscUpdate.matches(ZabezpieczeniaI)||!CenaUpdate.matches(ZabezpieczeniaI)){
  300. //                System.out.println("Wprowadzaono złe dane!");
  301. //                return ;
  302. //            }
  303.            
  304.             String sql = "UPDATE Magazyn SET Nazwa_Przedmiotu=?, Rodzaj_Przedmiotu=?, Ilosc=?, Cena_Przedmiotu=? WHERE ID_Przedmiotu=?;";
  305.            
  306.             try{
  307.                 Connection connection = DB_Access.connect();
  308.                
  309.             PreparedStatement pstmtU = connection.prepareStatement(sql); {
  310.                  
  311.                 pstmtU.setString(1, NazwaUpdate);
  312.                 pstmtU.setString(2, RodzajUpdate);
  313.                 pstmtU.setString(3, IloscUpdate);
  314.                 pstmtU.setString(4, CenaUpdate);
  315.                 pstmtU.setString(5, String.valueOf(ID_Przedmiotu));
  316.  
  317.             pstmtU.executeUpdate();
  318.                 System.out.println("Zaktualizowano poprawnie przedmiot! ");
  319.                 refreshScene();
  320.                
  321.                 ID_Przedmiotu = -1;
  322.                 TextField_Nazwa.setText("");
  323.                 TextField_Rodzaj.setText("");
  324.                 TextField_Ilosc_Szt.setText("");
  325.                 TextField_Cena.setText("");
  326.                
  327.                
  328.         }
  329.             try {
  330.             updateList();
  331.         } catch (ClassNotFoundException ex) {
  332.             Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
  333.         } catch (SQLException ex) {
  334.             Logger.getLogger(SklepController.class.getName()).log(Level.SEVERE, null, ex);
  335.         }
  336.      
  337.             }
  338.             catch(SQLException e) {
  339.             System.out.println(e.getMessage());
  340.         }
  341.         }
  342.         catch (ClassNotFoundException ex) {
  343.             Logger.getLogger(ProfilController.class.getName()).log(Level.SEVERE, null, ex);
  344.         }  
  345.     }    
  346.    
  347.     @FXML
  348.     private void TableView_Klikniece_Myszką(MouseEvent event) {
  349.        
  350.        
  351.         DB_VIEW Dane = TableView.getSelectionModel().getSelectedItem();
  352.         ID_Przedmiotu = TableView.getSelectionModel().getSelectedItem().getID_Przedmiotu();
  353.         TextField_Nazwa.setText(Dane.getNazwa_Przedmiotu());
  354.         TextField_Rodzaj.setText(Dane.getRodzaj_Przedmiotu());
  355.         TextField_Ilosc_Szt.setText(String.valueOf(Dane.getIlosc()));
  356.         TextField_Cena.setText(String.valueOf(Dane.getCena_Przedmiotu()));
  357.        
  358.     }  
  359.    
  360.    
  361.    
  362.        
  363.        
  364.    
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement