Advertisement
Guest User

Cart

a guest
Jun 7th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.88 KB | None | 0 0
  1. package application;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8.  
  9. import com.mysql.jdbc.PreparedStatement;
  10.  
  11. import javafx.event.ActionEvent;
  12. import javafx.event.EventHandler;
  13. import javafx.geometry.Insets;
  14. import javafx.scene.Scene;
  15. import javafx.scene.control.Button;
  16. import javafx.scene.layout.GridPane;
  17. import javafx.scene.text.Text;
  18. import javafx.stage.Stage;
  19.  
  20. public class Cart extends Stage {
  21.     public Cart(Stage prev , String userName) {
  22.         Stage current = this;
  23.         java.util.List<Button> list = new ArrayList<Button>();
  24.         GridPane gridPane = new GridPane();
  25.         int counter =0;
  26.         try {
  27.             ResultSet rs;
  28.             Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "ahmed666" );
  29.             PreparedStatement p = (PreparedStatement) con.prepareStatement("select * from CART where USER_NAME='"+userName+"'");
  30.             rs = p.executeQuery();
  31.             while(rs.next()) {
  32.                    list.add(new Button("Purchase"));
  33.                    list.get(counter).setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  34.                    Text ISBN = new Text(Integer.toString(rs.getInt("ISBN")));
  35.                    Text copies = new Text(Integer.toString(rs.getInt("COPIES")));
  36.                    ISBN.setStyle("-fx-font: normal italic 15px 'serif' ");
  37.                    copies.setStyle("-fx-font: normal italic 15px 'serif' ");
  38.                    gridPane.add(ISBN,0,counter+1);
  39.                    gridPane.add(copies,1,counter+1);
  40.                    gridPane.add(list.get(counter),(400/30) ,counter+1);
  41.                    
  42.                    
  43.                    // Purchasing handling
  44.                    Button b;
  45.                    b =  list.get(counter);
  46.                    
  47.                    list.get(counter).setOnAction(new EventHandler<ActionEvent>() {
  48.                         @Override
  49.                         public void handle(ActionEvent event) {
  50.                             // connect to db
  51.                               try {
  52.                                   String c = copies.getText();
  53.                                   String i = ISBN.getText();
  54.                                   Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "ahmed666" );
  55.                                   PreparedStatement p = (PreparedStatement) con.prepareStatement("update BOOK_STORE set BOOK_COPIES = BOOK_COPIES - "+c+
  56.                                         " where ISBN = "+i);
  57.                                   p.executeUpdate();
  58.                                   p = (PreparedStatement) con.prepareStatement("delete from CART where ISBN = "+i+" and USER_NAME = '"+userName+"'");
  59.                                   p.executeUpdate();
  60.                                   b.setDisable(true);
  61.                                  
  62.                               }
  63.                               catch (SQLException e) {
  64.                                   // TODO Auto-generated catch block
  65.                                   e.printStackTrace();
  66.                               }
  67.                            
  68.                            
  69.                            
  70.                         }
  71.                     });
  72.                    
  73.                    counter++;
  74.             }
  75.              
  76.         }
  77.         catch (SQLException e) {
  78.             // TODO Auto-generated catch block
  79.             e.printStackTrace();
  80.         }
  81.        
  82.        
  83.         //back button
  84.         Button back = new Button("Back");
  85.         back.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  86.         gridPane.add(back, 0, 400/30);
  87.         back.setOnAction(new EventHandler<ActionEvent>() {
  88.             @Override
  89.             public void handle(ActionEvent event) {
  90.                 current.close();
  91.                 prev.show();
  92.             }
  93.         });
  94.        
  95.         //Setting size for the pane  
  96.         gridPane.setMinSize(600, 400);
  97.            
  98.         //Setting the padding  
  99.         gridPane.setPadding(new Insets(10, 10, 10, 10));
  100.          
  101.         //Setting the vertical and horizontal gaps between the columns
  102.         gridPane.setVgap(30);
  103.         gridPane.setHgap(30);
  104.        
  105.         if(counter == 0 ) {
  106.             Text noRes = new Text("Cart is Empty");
  107.             gridPane.add(noRes,600/120 , 400/60 );
  108.             noRes.setStyle("-fx-font: normal bold 20px 'serif' ");
  109.         }
  110.         else {
  111.             Text ISBN = new Text("ISBN");
  112.             Text copies = new Text("COPIES");
  113.             Button purchaseAll = new Button("Purchase All");
  114.            
  115.             // Add Nodes to GridPane
  116.             gridPane.add(ISBN,0,0);
  117.             gridPane.add(copies,1,0);
  118.             gridPane.add(purchaseAll, 4 , 400/30);
  119.            
  120.             // set styling
  121.             ISBN.setStyle("-fx-font: normal bold 15px 'serif' ");
  122.             copies.setStyle("-fx-font: normal bold 15px 'serif' ");
  123.             purchaseAll.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
  124.            
  125.            
  126.            
  127.             purchaseAll.setOnAction(new EventHandler<ActionEvent>() {
  128.                 @Override
  129.                 public void handle(ActionEvent event) {
  130.                     try {
  131.                           Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "ahmed666" );
  132.                           PreparedStatement p = (PreparedStatement) con.prepareStatement("update BOOK_STORE set BOOK_COPIES = BOOK_COPIES - ( select COPIES"
  133.                                 + " from CART where BOOK_STORE.ISBN = ISBN and USER_NAME ='"+userName+"') where ISBN IN ( select ISBN from CART );");
  134.                           System.out.println("update BOOK_STORE set BOOK_COPIES = BOOK_COPIES - ( select COPIES"
  135.                                     + " from CART where BOOK_STORE.ISBN = ISBN and USER_NAME ='"+userName+"')  where ISBN IN ( select ISBN from CART ); ");
  136.                           p.executeUpdate();
  137.                           p = (PreparedStatement) con.prepareStatement("delete from CART where USER_NAME = '"+userName+"'");
  138.                           p.executeUpdate();
  139.                           purchaseAll.setDisable(true);
  140.                           for(int i =0 ; i< list.size() ;i++) {
  141.                               list.get(i).setDisable(true);
  142.                           }
  143.                          
  144.                       }
  145.                       catch (SQLException e) {
  146.                           // TODO Auto-generated catch block
  147.                           e.printStackTrace();
  148.                       }
  149.                 }
  150.             });
  151.  
  152.            
  153.         }
  154.         Scene scene = new Scene(gridPane);
  155.         setScene(scene);
  156.        
  157.        
  158.     }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement