Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab4;
- import java.util.*;
- import java.io.*;
- import javafx.event.*;
- import javafx.application.*;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.image.*;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.*;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- import java.sql.*;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- public class newBookShoppingCart extends Application{
- private static ObservableList<String> bookNamesList;
- private static ArrayList<Double> bookPrices;
- private static ArrayList<String> bookNames;
- private static ListView<String> listView;
- private static ImageView image;
- private static Button cartButton;
- private static BorderPane bPane;
- private static HBox hbox;
- private static GridPane grid;
- private static final Image taoProg = new Image("https://images-na.ssl-"
- + "images-amazon.com/images/I/510Q4YKK9ML._SX276_BO1,204,"
- + "203,200_.jpg");
- private static final Image unixProg = new Image("https://images-na.ssl-"
- + "images-amazon.com/images/I/51ach2pIk3L._SX404_BO1,204,203,"
- + "200_.jpg");
- private static final Image cProg = new Image("http://www.prenhall.com/"
- + "covergif/0131103628.jpg");
- private static final Image realHask = new Image("https://covers.oreilly"
- + "static.com/images/9780596514983/lrg.jpg");
- private static final Image hackersDelight = new Image("https://images-na."
- + "ssl-images-amazon.com/images/I/41nhhmV8cQL._SX317_BO1,204,203,"
- + "200_.jpg");
- private void DBConnect() throws ClassNotFoundException, SQLException,
- IOException {
- bookNames = new ArrayList<>();
- bookPrices = new ArrayList<>();
- try {
- // register the Oracle JDBC drivers
- DriverManager.registerDriver(
- new oracle.jdbc.OracleDriver()
- );
- System.out.println("Driver loaded");
- // Establish a connection to the remote database
- Connection connection = DriverManager.getConnection(
- // IP address of the Oracle1 server, port 1521, SID name
- "**WITHELD:1521:orcl",
- "WITHELD", // A generic Oracle account
- "WITHELD"
- );
- System.out.println("Database connected");
- // Create a statement
- Statement stmt = connection.createStatement();
- ResultSet rst = stmt.executeQuery
- ("SELECT BOOK,BOOK_PRICE\n"
- + "FROM JAVA_BOOK_STORE");
- while (rst.next()) {
- bookNames.add(rst.getString(1));
- bookPrices.add(rst.getDouble(2));
- }
- bookNamesList = FXCollections.observableArrayList(bookNames);
- }
- catch (SQLException ex) {
- }
- }
- private void ListView() {
- listView = new ListView<>(bookNamesList);
- listView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
- }
- private void ListViewFunction() {
- image = new ImageView();
- listView.setOnMouseClicked((MouseEvent arg0) -> {
- switch (listView.getSelectionModel().getSelectedIndex()) {
- case 0:
- image.setImage(taoProg);
- break;
- case 1:
- image.setImage(unixProg);
- break;
- case 2:
- image.setImage(cProg);
- break;
- case 3:
- image.setImage(realHask);
- break;
- case 4:
- image.setImage(hackersDelight);
- break;
- }
- });
- }
- private void MenuBar(Stage stage, BorderPane root) {
- MenuBar menuBar = new MenuBar();
- menuBar.prefWidthProperty().bind(stage.widthProperty());
- root.setTop(menuBar);
- // File menu item
- Menu fileMenu = new Menu("File");
- MenuItem exitMenuItem = new MenuItem("Exit");
- ExitMenuFunction(exitMenuItem);
- fileMenu.getItems().addAll(exitMenuItem);
- // Help menu item
- Menu helpMenu = new Menu("Help");
- MenuItem aboutMenuItem = new MenuItem("About");
- AboutMenuFunction(aboutMenuItem, stage);
- helpMenu.getItems().addAll(aboutMenuItem);
- // Menu bar
- menuBar.getMenus().addAll(fileMenu, helpMenu);
- }
- private void AboutMenuFunction(MenuItem aboutMenuItem, Stage stage) {
- aboutMenuItem.setOnAction((ActionEvent event) -> {
- String url = "https://content.mycutegraphics.com/graphics/book/"
- + "stack-of-books-clipart-book-clip-art.png";
- ImageView img = new ImageView(new Image(url));
- img.setFitHeight(100);
- img.setFitWidth(100);
- Label label = new Label("Book Store\nv0.0.1\n"
- + "_________________________________________________\n\n"
- + "An application that allows you to purchase books\n"
- + "from the book store", img);
- StackPane secondaryLayout = new StackPane();
- secondaryLayout.getChildren().add(label);
- Scene secondScene = new Scene(secondaryLayout,500,125);
- // New Window
- Stage newWindow = new Stage();
- newWindow.setTitle("About");
- newWindow.setScene(secondScene);
- // Set position
- newWindow.setX(stage.getX() + 200);
- newWindow.setY(stage.getY() + 100);
- newWindow.show();
- });
- }
- private void ExitMenuFunction(MenuItem exitMenuItem) {
- exitMenuItem.setOnAction(actionEvent -> Platform.exit());
- }
- private void Layout() {
- bPane = new BorderPane();
- hbox = new HBox();
- hbox.getChildren().addAll(listView, grid);
- bPane.setLeft(hbox);
- }
- private void GridPaneAdd() {
- image.setFitWidth(150);
- image.setPreserveRatio(true);
- image.setCache(true);
- cartButton = new Button("Add To Cart");
- grid = new GridPane();
- grid.setHgap(20);
- grid.setVgap(20);
- grid.setPadding(new Insets(0,30,0,30));
- grid.add(image, 2, 1);
- grid.add(cartButton, 2, 4);
- }
- @Override
- public void start(Stage stage) throws IOException, ClassNotFoundException,
- SQLException {
- stage.setTitle("Book Store");
- stage.setWidth(550);
- stage.setHeight(500);
- DBConnect();
- ListView();
- ListViewFunction();
- GridPaneAdd();
- Layout();
- MenuBar(stage,bPane);
- Scene scene = new Scene(bPane, 300, 250, Color.WHITE);
- stage.setScene(scene);
- stage.show();
- }
- public static void main(String[] args) throws ClassNotFoundException,
- SQLException {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement