Advertisement
Guest User

DB Book Store

a guest
Apr 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.38 KB | None | 0 0
  1.  
  2. package lab4;
  3.  
  4. import java.util.*;
  5. import java.io.*;
  6. import javafx.event.*;
  7. import javafx.application.*;
  8. import javafx.collections.FXCollections;
  9. import javafx.collections.ObservableList;
  10. import javafx.scene.Scene;
  11. import javafx.scene.control.*;
  12. import javafx.scene.image.*;
  13. import javafx.scene.input.MouseEvent;
  14. import javafx.scene.layout.*;
  15. import javafx.scene.paint.Color;
  16. import javafx.stage.Stage;
  17. import java.sql.*;
  18. import javafx.geometry.Insets;
  19. import javafx.geometry.Pos;
  20.  
  21. public class newBookShoppingCart extends Application{
  22.     private static ObservableList<String> bookNamesList;
  23.     private static ArrayList<Double> bookPrices;
  24.     private static ArrayList<String> bookNames;
  25.     private static ListView<String> listView;
  26.     private static ImageView image;
  27.     private static Button cartButton;
  28.     private static BorderPane bPane;
  29.     private static HBox hbox;
  30.     private static GridPane grid;
  31.    
  32.     private static final Image taoProg = new Image("https://images-na.ssl-"
  33.             + "images-amazon.com/images/I/510Q4YKK9ML._SX276_BO1,204,"
  34.             + "203,200_.jpg");
  35.     private static final Image unixProg = new Image("https://images-na.ssl-"
  36.             + "images-amazon.com/images/I/51ach2pIk3L._SX404_BO1,204,203,"
  37.             + "200_.jpg");
  38.     private static final Image cProg = new Image("http://www.prenhall.com/"
  39.             + "covergif/0131103628.jpg");
  40.     private static final Image realHask = new Image("https://covers.oreilly"
  41.             + "static.com/images/9780596514983/lrg.jpg");
  42.     private static final Image hackersDelight = new Image("https://images-na."
  43.             + "ssl-images-amazon.com/images/I/41nhhmV8cQL._SX317_BO1,204,203,"
  44.             + "200_.jpg");
  45.    
  46.     private void DBConnect() throws ClassNotFoundException, SQLException,
  47.             IOException {
  48.         bookNames = new ArrayList<>();
  49.         bookPrices = new ArrayList<>();
  50.        
  51.         try {
  52.             // register the Oracle JDBC drivers
  53.             DriverManager.registerDriver(
  54.                 new oracle.jdbc.OracleDriver()
  55.             );
  56.             System.out.println("Driver loaded");
  57.            
  58.             // Establish a connection to the remote database
  59.             Connection connection = DriverManager.getConnection(
  60.             // IP address of the Oracle1 server, port 1521, SID name
  61.                 "**WITHELD:1521:orcl",
  62.                 "WITHELD",    // A generic Oracle account
  63.                 "WITHELD"
  64.             );
  65.             System.out.println("Database connected");
  66.            
  67.             // Create a statement
  68.             Statement stmt = connection.createStatement();
  69.            
  70.             ResultSet rst = stmt.executeQuery
  71.                     ("SELECT BOOK,BOOK_PRICE\n"
  72.                     + "FROM JAVA_BOOK_STORE");
  73.             while (rst.next()) {
  74.                 bookNames.add(rst.getString(1));
  75.                 bookPrices.add(rst.getDouble(2));
  76.             }
  77.             bookNamesList = FXCollections.observableArrayList(bookNames);
  78.         }
  79.         catch (SQLException ex) {
  80.         }
  81.     }
  82.     private void ListView() {
  83.         listView = new ListView<>(bookNamesList);
  84.         listView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
  85.     }
  86.     private void ListViewFunction() {
  87.         image = new ImageView();
  88.        
  89.        
  90.         listView.setOnMouseClicked((MouseEvent arg0) -> {
  91.             switch (listView.getSelectionModel().getSelectedIndex()) {
  92.                 case 0:
  93.                     image.setImage(taoProg);
  94.                     break;
  95.                 case 1:
  96.                     image.setImage(unixProg);
  97.                     break;
  98.                 case 2:
  99.                     image.setImage(cProg);
  100.                     break;
  101.                 case 3:
  102.                     image.setImage(realHask);
  103.                     break;
  104.                 case 4:
  105.                     image.setImage(hackersDelight);
  106.                     break;
  107.             }
  108.         });
  109.     }
  110.     private void MenuBar(Stage stage, BorderPane root) {
  111.         MenuBar menuBar = new MenuBar();
  112.         menuBar.prefWidthProperty().bind(stage.widthProperty());
  113.         root.setTop(menuBar);
  114.        
  115.         // File menu item
  116.         Menu fileMenu = new Menu("File");
  117.         MenuItem exitMenuItem = new MenuItem("Exit");
  118.         ExitMenuFunction(exitMenuItem);
  119.         fileMenu.getItems().addAll(exitMenuItem);
  120.        
  121.         // Help menu item
  122.         Menu helpMenu = new Menu("Help");
  123.         MenuItem aboutMenuItem = new MenuItem("About");
  124.         AboutMenuFunction(aboutMenuItem, stage);
  125.         helpMenu.getItems().addAll(aboutMenuItem);
  126.        
  127.         // Menu bar
  128.         menuBar.getMenus().addAll(fileMenu, helpMenu);
  129.     }
  130.     private void AboutMenuFunction(MenuItem aboutMenuItem, Stage stage) {
  131.         aboutMenuItem.setOnAction((ActionEvent event) -> {
  132.             String url = "https://content.mycutegraphics.com/graphics/book/"
  133.                     + "stack-of-books-clipart-book-clip-art.png";
  134.            
  135.             ImageView img = new ImageView(new Image(url));
  136.             img.setFitHeight(100);
  137.             img.setFitWidth(100);
  138.             Label label = new Label("Book Store\nv0.0.1\n"
  139.                     + "_________________________________________________\n\n"
  140.                     + "An application that allows you to purchase books\n"
  141.                     + "from the book store", img);
  142.            
  143.             StackPane secondaryLayout = new StackPane();
  144.             secondaryLayout.getChildren().add(label);
  145.             Scene secondScene = new Scene(secondaryLayout,500,125);
  146.            
  147.             // New Window
  148.             Stage newWindow = new Stage();
  149.             newWindow.setTitle("About");
  150.             newWindow.setScene(secondScene);
  151.            
  152.             // Set position
  153.             newWindow.setX(stage.getX() + 200);
  154.             newWindow.setY(stage.getY() + 100);
  155.                    
  156.            
  157.             newWindow.show();
  158.         });
  159.     }
  160.     private void ExitMenuFunction(MenuItem exitMenuItem) {
  161.         exitMenuItem.setOnAction(actionEvent -> Platform.exit());
  162.     }
  163.     private void Layout() {
  164.         bPane = new BorderPane();
  165.         hbox = new HBox();
  166.        
  167.         hbox.getChildren().addAll(listView, grid);
  168.        
  169.         bPane.setLeft(hbox);
  170.     }
  171.     private void GridPaneAdd() {
  172.         image.setFitWidth(150);
  173.         image.setPreserveRatio(true);
  174.         image.setCache(true);
  175.        
  176.         cartButton = new Button("Add To Cart");
  177.        
  178.         grid = new GridPane();
  179.        
  180.         grid.setHgap(20);
  181.         grid.setVgap(20);
  182.         grid.setPadding(new Insets(0,30,0,30));
  183.        
  184.         grid.add(image, 2, 1);
  185.         grid.add(cartButton, 2, 4);
  186.     }
  187.     @Override
  188.     public void start(Stage stage) throws IOException, ClassNotFoundException,
  189.             SQLException {
  190.         stage.setTitle("Book Store");
  191.         stage.setWidth(550);
  192.         stage.setHeight(500);
  193.        
  194.         DBConnect();
  195.         ListView();
  196.         ListViewFunction();
  197.         GridPaneAdd();
  198.        
  199.         Layout();
  200.        
  201.         MenuBar(stage,bPane);
  202.        
  203.         Scene scene = new Scene(bPane, 300, 250, Color.WHITE);
  204.         stage.setScene(scene);
  205.         stage.show();
  206.     }
  207.     public static void main(String[] args) throws ClassNotFoundException,
  208.             SQLException {
  209.         launch(args);
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement