Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.application.Application;
- import javafx.collections.FXCollections;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.layout.*;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- import java.sql.SQLException;
- public class Main extends Application {
- private Stage stage;
- private BorderPane borderPane = new BorderPane();
- private InvoiceDAO invoiceDAO = new InvoiceDAO();
- private ProductDAO productDAO = new ProductDAO();
- private CustomerDAO customerDAO = new CustomerDAO();
- private CategoryDAO categoryDAO = new CategoryDAO();
- private AddressDAO addressDAO = new AddressDAO();
- private ListView<Invoice> invoices = new ListView<>();
- private ListView<Customer> customers = new ListView<>();
- private ListView<Product> products = new ListView<>();
- private ListView<Category> categories = new ListView<>();
- public static void main(String[] args) throws SQLException {
- StatementReader sr = new StatementReader();
- launch(args);
- }
- @Override
- public void start(Stage primaryStage) {
- stage = primaryStage;
- createBorderPane();
- }
- /**
- * Metoden createBorderPane() lager brukergrensersnittet og alle elementene som skal i den.
- */
- public void createBorderPane() {
- borderPane.setPadding(new Insets(0, 5, 5, 5));
- ToolBar toolBar = new ToolBar();
- borderPane.setTop(toolBar);
- //Lister
- invoices.setItems(FXCollections.observableArrayList(invoiceDAO.getAllInvoices()));
- customers.setItems(FXCollections.observableArrayList(customerDAO.getAllCustomers()));
- products.setItems(FXCollections.observableArrayList(productDAO.getAllProducts()));
- categories.setItems(FXCollections.observableArrayList(categoryDAO.getAllCategories()));
- //Invoice
- invoices.getSelectionModel().selectFirst();
- invoices.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) ->
- borderPane.setCenter(setInvoice(newValue.getID())));
- HBox invoiceBody = setInvoice(invoices.getSelectionModel().getSelectedItem().getID());
- VBox insertInvoice = setNewInvoice(invoiceBody);
- HBox iButtons = new HBox();
- Button iAdd = new Button("new Invoice");
- Button iEdit = new Button("edit Invoice");
- iAdd.setOnAction(event -> borderPane.setCenter(insertInvoice));
- iEdit.setOnAction(event -> borderPane.setCenter(editInvoice(invoices.getSelectionModel().getSelectedItem(), invoiceBody)));
- iButtons.getChildren().setAll(iAdd, iEdit);
- borderPane.setCenter(invoiceBody);
- borderPane.setLeft(invoices);
- borderPane.setBottom(iButtons);
- //Customer
- customers.getSelectionModel().selectFirst();
- customers.getSelectionModel().selectedItemProperty().addListener(((v, oldValue, newValue) ->
- borderPane.setCenter(setCustomer(newValue.getID()))));
- HBox customerBody = setCustomer(customers.getSelectionModel().getSelectedItem().getID());
- customerBody.setPadding(new Insets(5));
- HBox cButtons = new HBox();
- Button cAdd = new Button("new Customer");
- Button cEdit = new Button("edit Customer");
- cAdd.setOnAction(event -> borderPane.setCenter(setNewCustomer(customerBody)));
- cEdit.setOnAction(event -> borderPane.setCenter(editCustomer(customers.getSelectionModel().getSelectedItem(), customerBody)));
- cButtons.getChildren().setAll(cAdd, cEdit);
- //Products
- products.getSelectionModel().selectFirst();
- products.getSelectionModel().selectedItemProperty().addListener(((v, oldValue, newValue) ->
- borderPane.setCenter(setProduct(newValue.getID()))));
- HBox productBody = setProduct(products.getSelectionModel().getSelectedItem().getID());
- HBox pButtons = new HBox();
- Button pAdd = new Button("new Product");
- Button pEdit = new Button("edit Product");
- pAdd.setOnAction(event -> borderPane.setCenter(setNewProduct(productBody)));
- pEdit.setOnAction(event -> borderPane.setCenter(editProduct(products.getSelectionModel().getSelectedItem(), invoiceBody)));
- pButtons.getChildren().setAll(pAdd, pEdit);
- //Categories
- categories.getSelectionModel().selectFirst();
- categories.getSelectionModel().selectedItemProperty().addListener(((v, oldValue, newValue) ->
- borderPane.setCenter(setCategory(newValue.getID()))));
- HBox categoryBody = setCategory(categories.getSelectionModel().getSelectedItem().getID());
- HBox catButtons = new HBox();
- Button catAdd = new Button("new Category");
- Button catEdit = new Button("edit Category");
- catAdd.setOnAction(event -> borderPane.setCenter(setNewCategory(categoryBody)));
- catEdit.setOnAction(event -> borderPane.setCenter(editCategory(categories.getSelectionModel().getSelectedItem(), categoryBody)));
- catButtons.getChildren().setAll(catAdd, catEdit);
- //Deloppgave 5
- ListView<Customer> ibycList = new ListView<>();
- ibycList.setItems(FXCollections.observableArrayList(customerDAO.getAllCustomers()));
- ibycList.getSelectionModel().selectFirst();
- ibycList.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) ->
- borderPane.setCenter(invoicesByCustomers(newValue.getID())));
- ListView<Customer> pbycList = new ListView<>();
- pbycList.setItems(FXCollections.observableArrayList(customerDAO.getAllCustomers()));
- pbycList.getSelectionModel().selectFirst();
- pbycList.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) ->
- borderPane.setCenter(productByCustomers(newValue.getID())));
- //Toolbar buttons
- Button invoiceButton = new Button("Invoice");
- invoiceButton.setOnAction(event -> { borderPane.setCenter(invoiceBody);
- borderPane.setLeft(invoices);
- borderPane.setBottom(iButtons);});
- Button customerButton = new Button("Customer");
- customerButton.setOnAction(event -> { borderPane.setCenter(customerBody);
- borderPane.setLeft(customers);
- borderPane.setBottom(cButtons);});
- Button productButton = new Button("Product");
- productButton.setOnAction(event -> { borderPane.setCenter(productBody);
- borderPane.setLeft(products);
- borderPane.setBottom(pButtons);});
- Button categoryButton = new Button("Category");
- categoryButton.setOnAction(event -> { borderPane.setCenter(categoryBody);
- borderPane.setLeft(categories);
- borderPane.setBottom(catButtons);});
- Button iByCButton = new Button("Invoices by Customer");
- iByCButton.setOnAction(event -> { borderPane.setCenter(invoicesByCustomers(
- pbycList.getSelectionModel().getSelectedItem().getID()));
- borderPane.setLeft(ibycList);
- borderPane.setBottom(null);});
- Button cByPButton = new Button("Products by Customer");
- cByPButton.setOnAction(event -> { borderPane.setCenter(productByCustomers(
- pbycList.getSelectionModel().getSelectedItem().getID()));
- borderPane.setLeft(pbycList);
- borderPane.setBottom(null);});
- toolBar.getItems().addAll(invoiceButton, customerButton, productButton, categoryButton, iByCButton, cByPButton);
- //Insets
- invoiceBody.setPadding(new Insets(10));
- customerBody.setPadding(new Insets(10));
- productBody.setPadding(new Insets(10));
- categoryBody.setPadding(new Insets(10));
- iButtons.setPadding(new Insets(5, 10, 5, 10));
- cButtons.setPadding(new Insets(5, 10, 5, 10));
- pButtons.setPadding(new Insets(5, 10, 5, 10));
- catButtons.setPadding(new Insets(5, 10, 5, 10));
- toolBar.setPadding(new Insets(5, 10, 5, 10));
- //Stage
- stage.setScene(new Scene(borderPane, 850, 510));
- stage.show();
- }
- /**
- * Metoden setInvoice() returnerer en HBox som skal brukes som centre i borderPane.
- *
- * @param id
- * @return HBox med invoicen med riktig ID
- */
- public HBox setInvoice(int id) {
- Invoice invoice = invoiceDAO.getInvoice(id);
- Customer customer = customerDAO.getCustomer(invoice.getCustomer());
- String products = "";
- double sum = 0;
- for(Product p : invoice.getProducts()){
- products += '"' + p.getName() + '"' + "\n";
- sum += p.getPrice();
- }
- VBox lVbox = new VBox(new Text( "Invoice id:" + "\n" +
- "Date:" + "\n" +
- "Customer:" + "\n" +
- "tlf:" + "\n" +
- "Price:"+ "\n" +
- "Products:"));
- VBox rVbox = new VBox(new Text( invoice.getID() + "\n" +
- invoice.getDate() + "\n" +
- customer.getName() + "\n" +
- customer.getPhonenumber() + "\n" +
- sum + "kr" + "\n" +
- products));
- rVbox.setPadding(new Insets(0, 0, 0, 10));
- HBox iHbox = new HBox();
- iHbox.getChildren().addAll(lVbox, rVbox);
- iHbox.setPadding(new Insets(0, 5, 0, 5));
- return iHbox;
- }
- /**
- * Metoden setNewInvoice() returnerer en VBox som lar deg danne nye Invoice-objekter
- * Denne skal brukes som center i borderPane
- *
- * @param invoiceBody HBox-en vi går tilbake til når vi trykker "back"-knappen
- * @return VBox
- */
- public VBox setNewInvoice(HBox invoiceBody){
- TextField iCustomer = new TextField();
- TextField iDate = new TextField();
- TextField prod = new TextField();
- HBox customerBox = new HBox(new Label("Customer ID: " + "\t"), iCustomer);
- HBox dateBox = new HBox(new Label("Date: " + "\t" + "\t"), iDate, new Label(" dd.mm.yyyy"));
- HBox prodBox = new HBox(new Label("Product ID: " + "\t"), prod, new Label(" enter each ID separated by only a #-symbol."));
- Button insert = new Button("insert");
- Alert alert = new Alert(Alert.AlertType.ERROR);
- insert.setOnAction(event -> {
- String[] products = prod.getText().split("#");
- boolean allProductsExist = true;
- for(String p : products) {
- int id = Integer.parseInt(p.trim());
- if (productDAO.getProduct(id) == null) {
- allProductsExist = false;
- }
- }
- if(iCustomer.getText().isEmpty() || iDate.getText().isEmpty() || prod.getText().isEmpty()){
- alert.setContentText("All fields must be filled");
- alert.showAndWait();
- } else if (customerDAO.getCustomer(Integer.parseInt(iCustomer.getText())) == null){
- alert.setContentText("Customer not found");
- alert.showAndWait();
- } else if (iDate.getText().length() < 10){
- alert.setContentText("Invalid date (dd.mm.yyyy)");
- alert.showAndWait();
- } else if(!allProductsExist){
- alert.setContentText("Product not found");
- alert.showAndWait();
- } else{
- Invoice invoice = new Invoice(Integer.parseInt(iCustomer.getText()), iDate.getText());
- for(String p : products) {
- int id = Integer.parseInt(p.trim());
- invoice.getProducts().add(productDAO.getProduct(id));
- }
- int invoiceID = invoiceDAO.insertInvoice(invoice);
- invoice.setID(invoiceID);
- invoices.getItems().add(invoice);
- iCustomer.clear();
- iDate.clear();
- prod.clear();
- }});
- Button back = new Button("back");
- back.setOnAction(event -> borderPane.setCenter(invoiceBody));
- VBox ibox = new VBox();
- ibox.getChildren().setAll(customerBox, dateBox, prodBox, insert, back);
- return ibox;
- }
- /**
- * Metoden editInvoice() lar deg redigere ett Invoice-objekt. Disse endringene skjer også i databasen.
- *
- * @param invoice kunden vi vil endre
- * @param invoiceBody bodien vi vil returnere til når vi trykker "back"-knappen
- * @return VBox som lar deg redigere ett Customer-objekt
- */
- public VBox editInvoice(Invoice invoice, HBox invoiceBody) {
- TextField iCustomer = new TextField(invoice.getCustomer() + "");
- TextField iDate = new TextField(invoice.getDate());
- HBox customerBox = new HBox(new Label("Edit Customer by ID: " + "\t"), iCustomer);
- HBox dateBox = new HBox(new Label("Edit Date: " + "\t" + "\t" + "\t"),iDate);
- Alert alert = new Alert(Alert.AlertType.ERROR);
- Button update = new Button("update");
- update.setOnAction(event -> {
- if(iCustomer.getText().isEmpty() || iDate.getText().isEmpty()){
- alert.setContentText("All fields must be filled");
- alert.showAndWait();
- } else if (iDate.getText().length() < 10){
- alert.setContentText("Invalid date (dd.mm.yyyy)");
- alert.showAndWait();
- } else if (customerDAO.getCustomer(Integer.parseInt(iCustomer.getText())) == null){
- alert.setContentText("Customer not found");
- alert.showAndWait();
- } else {
- invoice.setCustomer(Integer.parseInt(iCustomer.getText()));
- invoice.setDate(iDate.getText());
- invoiceDAO.updateInvoice(invoice);
- borderPane.setCenter(setInvoice(invoice.getID()));
- }});
- Button back = new Button("back");
- back.setOnAction(event -> borderPane.setCenter(invoiceBody));
- HBox iNavigation = new HBox();
- iNavigation.getChildren().addAll(update, back);
- VBox iBox = new VBox();
- iBox.getChildren().addAll(customerBox, dateBox, iNavigation);
- return iBox;
- }
- /**
- * Metoden setCustomer() returnerer en HBox som skal brukes som centre i borderPane.
- * Address blir også hentet ut her.
- *
- * @param id
- * @return HBox med customer med riktig ID
- */
- public HBox setCustomer(int id) {
- Customer customer = customerDAO.getCustomer(id);
- VBox lVbox = new VBox(new Text( "Customer ID: " + "\n" +
- "Customer name: " + "\n" +
- "Phone number: " + "\n" +
- "Address: " + "\n" +
- "Billing account: "));
- VBox rVbox = new VBox(new Text( customer.getID() + "\n" +
- customer.getName() + "\n" +
- customer.getPhonenumber() + "\n" +
- customer.getAddress().getAddress() + "\n" +
- customer.getBillingAccount() + "\n"));
- HBox cHbox = new HBox();
- cHbox.getChildren().addAll(lVbox, rVbox);
- cHbox.setPadding(new Insets(0, 5, 0, 5));
- return cHbox;
- }
- /**
- * Metoden setNewCustomer() returnerer en VBox som lar deg danne nye Customer-objekter
- * Denne skal brukes som center i borderPane.
- * Her kan du også lage nye Address-objekter.
- *
- * @param customerBody HBox-en vi går tilbake til når vi trykker "back"-knappen
- * @return VBox
- */
- public VBox setNewCustomer(HBox customerBody){
- TextField cName = new TextField();
- TextField cNumber = new TextField();
- TextField cAccount = new TextField();
- TextField cAdrStreet = new TextField();
- TextField cAdrNumber = new TextField();
- TextField cAdrCode = new TextField();
- TextField cAdrTown = new TextField();
- HBox nameBox = new HBox(new Label("Customer name: " + "\t"), cName);
- HBox numberBox = new HBox(new Label("Phone number: " + "\t"), cNumber);
- HBox accountBox = new HBox(new Label("Billing Account: " + "\t"), cAccount);
- HBox adrStreetBox = new HBox(new Label("Street name: " + "\t" + "\t"), cAdrStreet);
- HBox adrNumberBox = new HBox(new Label("Street number: " + "\t"), cAdrNumber);
- HBox adrCodeBox = new HBox(new Label("Area code: " + "\t" + "\t"), cAdrCode);
- HBox adrTownBox = new HBox(new Label("town: " + "\t" + "\t" + "\t"), cAdrTown);
- Button insert = new Button("insert");
- Alert alert = new Alert(Alert.AlertType.ERROR);
- alert.setHeaderText(null);
- insert.setOnAction(event -> {
- if(cName.getText().isEmpty() || cNumber.getText().isEmpty() || cAccount.getText().isEmpty()
- || cAdrNumber.getText().isEmpty() || cAdrStreet.getText().isEmpty() || cAdrCode.getText().isEmpty()
- || cAdrTown.getText().isEmpty()) {
- alert.setContentText("All fields must be filled");
- alert.showAndWait();
- } else {
- Address address = new Address(cAdrNumber.getText(), cAdrStreet.getText(), cAdrCode.getText(), cAdrTown.getText());
- int addressID = addressDAO.insertAddress(address);
- address.setID(addressID);
- Customer customer = new Customer(cName.getText(), cNumber.getText(), cAccount.getText(), addressID);
- int customerID = customerDAO.insertCustomer(customer);
- customer.setID(customerID);
- customers.getItems().add(customer);
- cName.clear();
- cNumber.clear();
- cAccount.clear();
- cAdrStreet.clear();
- cAdrNumber.clear();
- cAdrCode.clear();
- cAdrTown.clear();
- }});
- Button back = new Button("back");
- back.setOnAction(event -> borderPane.setCenter(customerBody));
- VBox ibox = new VBox();
- ibox.getChildren().setAll(nameBox, numberBox, accountBox, adrStreetBox, adrNumberBox, adrCodeBox, adrTownBox, insert, back);
- ibox.setPadding(new Insets(10));
- return ibox;
- }
- /**
- * Metoden editCustomer() lar deg redigere ett Customer-objekt. Disse endringene skjer også i databasen.
- * Her kan du også redigere Address-objektet som tilhører Customer-objektet.
- *
- * @param customer kunden vi vil endre
- * @param customerBody bodien vi vil returnere til når vi trykker "back"-knappen
- * @return VBox som lar deg redigere ett Customer-objekt
- */
- private VBox editCustomer(Customer customer, HBox customerBody) {
- Address address = customer.getAddress();
- TextField cName = new TextField(customer.getName());
- TextField cNumber = new TextField(customer.getPhonenumber());
- TextField cAccount = new TextField(customer.getBillingAccount());
- TextField cAdrStreet = new TextField(address.getStreet());
- TextField cAdrNumber = new TextField(address.getNumber());
- TextField cAdrCode = new TextField(address.getPostalCode());
- TextField cAdrTown = new TextField(address.getTown());
- HBox nameBox = new HBox(new Label("Edit Customer name: " + "\t"), cName);
- HBox numberBox = new HBox(new Label("Edit Phone number: " + "\t"), cNumber);
- HBox accountBox = new HBox(new Label("Edit Billing Account: " + "\t"), cAccount);
- HBox adrStreetBox = new HBox(new Label("Edit Street name: " + "\t" + "\t"), cAdrStreet);
- HBox adrNumberBox = new HBox(new Label("Edit Street number: " + "\t"), cAdrNumber);
- HBox adrCodeBox = new HBox(new Label("Edit Area code: " + "\t" + "\t"), cAdrCode);
- HBox adrTownBox = new HBox(new Label("Edit town: " + "\t" + "\t" + "\t"), cAdrTown);
- Alert alert = new Alert(Alert.AlertType.ERROR);
- Button update = new Button("update");
- update.setOnAction(event -> {
- if(cName.getText().isEmpty() || cNumber.getText().isEmpty() || cAccount.getText().isEmpty()
- || cAdrStreet.getText().isEmpty() || cAdrNumber.getText().isEmpty() || cAdrCode.getText().isEmpty()
- || cAdrTown.getText().isEmpty()) {
- alert.setContentText("All fields must be filled");
- alert.showAndWait();
- } else {
- address.setStreet(cAdrStreet.getText());
- address.setNumber(cAdrNumber.getText());
- address.setPostalCode(cAdrCode.getText());
- address.setTown(cAdrTown.getText());
- customer.setName(cName.getText());
- customer.setPhonenumber(cNumber.getText());
- customer.setBillingAccount(cAccount.getText());
- customer.setAddress(address);
- customerDAO.updateCustomer(customer);
- addressDAO.updateAddress(address);
- borderPane.setCenter(setCustomer(customer.getID()));
- }});
- Button back = new Button("back");
- back.setOnAction(event -> borderPane.setCenter(customerBody));
- HBox cNavigation = new HBox();
- cNavigation.getChildren().addAll(update, back);
- VBox cBox = new VBox();
- cBox.getChildren().addAll(nameBox, numberBox, accountBox, adrStreetBox, adrNumberBox, adrCodeBox, adrTownBox, cNavigation);
- cBox.setPadding(new Insets(10));
- return cBox;
- }
- /**
- * Metoden setProduct() returnerer en HBox som skal brukes som centre i borderPane.
- *
- * @param id
- * @return HBox med product med riktig ID
- */
- public HBox setProduct(int id){
- Product product = productDAO.getProduct(id);
- Category category = categoryDAO.getCategory(product.getCategory());
- VBox lVbox = new VBox(new Text( "Product ID: " + "\n" +
- "Product category: " + "\n" +
- "Product Name: " + "\n" +
- "Product Description: " + "\n" +
- "Price: "));
- VBox rVbox = new VBox(new Text( product.getID() + "\n" +
- category.getName() + "\n" +
- product.getName() + "\n" +
- product.getDescription() + "\n" +
- product.getPrice() + "kr"));
- HBox pHbox = new HBox();
- pHbox.getChildren().addAll(lVbox, rVbox);
- pHbox.setPadding(new Insets(10));
- return pHbox;
- }
- /**
- * Metoden setNewProduct() returnerer en VBox som lar deg danne nye Product-objekter
- * Denne skal brukes som center i borderPane
- *
- * @param productBody HBox-en vi går tilbake til når vi trykker "back"-knappen
- * @return VBox
- */
- private VBox setNewProduct(HBox productBody) {
- TextField pName = new TextField();
- TextField pDesc = new TextField();
- TextField pPrice = new TextField();
- TextField pCategory = new TextField();
- HBox nameBox = new HBox(new Label("Product Name: " + "\t" + "\t"), pName);
- HBox descBox = new HBox(new Label("Product Description: " + "\t"), pDesc);
- HBox priceBox = new HBox(new Label("Product Price: " + "\t" + "\t"), pPrice);
- HBox categoryBox = new HBox(new Label("Category ID: " + "\t" + "\t" + "\t"), pCategory);
- Button insert = new Button("insert");
- Alert alert = new Alert(Alert.AlertType.ERROR);
- insert.setOnAction(event -> {
- if(pName.getText().isEmpty() || pDesc.getText().isEmpty() || pPrice.getText().isEmpty() || pCategory.getText().isEmpty()){
- alert.setContentText("All fields must be filled");
- alert.showAndWait();
- } else if(Integer.parseInt(pPrice.getText()) < 0) {
- alert.setContentText("Price must be greater than zero");
- alert.showAndWait();
- } else if(categoryDAO.getCategory(Integer.parseInt(pCategory.getText())) == null) {
- alert.setContentText("Category not found");
- alert.showAndWait();
- } else {
- Product product = new Product(pName.getText(), pDesc.getText(), Integer.parseInt(pPrice.getText()), Integer.parseInt(pCategory.getText()));
- int productID = productDAO.insertProduct(product);
- product.setID(productID);
- products.getItems().add(product);
- pName.clear();
- pDesc.clear();
- pPrice.clear();
- pCategory.clear();
- }});
- Button back = new Button("back");
- back.setOnAction(event -> borderPane.setCenter(productBody));
- HBox pNavigation = new HBox();
- pNavigation.getChildren().addAll(insert, back);
- VBox pbox = new VBox();
- pbox.getChildren().setAll(nameBox, descBox, priceBox, categoryBox, pNavigation);
- pbox.setPadding(new Insets(10));
- return pbox;
- }
- /**
- * Metoden editProduct() lar deg redigere ett Product-objekt. Disse endringene skjer også i databasen.
- *
- * @param product kunden vi vil endre
- * @param productBody bodien vi vil returnere til når vi trykker "back"-knappen
- * @return VBox som lar deg redigere ett Customer-objekt
- */
- private VBox editProduct(Product product, HBox productBody) {
- TextField pName = new TextField(product.getName());
- TextField pDesc = new TextField(product.getDescription());
- TextField pPrice = new TextField(product.getPrice() + "");
- TextField pCat = new TextField(product.getCategory() + "");
- HBox nameBox = new HBox(new Label("Edit Product name: " + "\t"), pName);
- HBox descBox = new HBox(new Label("Edit description " + "\t" + "\t"),pDesc);
- HBox priceBox = new HBox(new Label("Edit price " + "\t" + "\t" + "\t"),pPrice);
- HBox catBox = new HBox(new Label("Edit Category by ID: " + "\t"),pCat);
- Alert alert = new Alert(Alert.AlertType.ERROR);
- Button update = new Button("update");
- update.setOnAction(event -> {
- if(pName.getText().isEmpty() || pDesc.getText().isEmpty()
- || pPrice.getText().isEmpty() || pCat.getText().isEmpty()){
- alert.setContentText("All fields must be filled");
- alert.showAndWait();
- } else if (categoryDAO.getCategory(Integer.parseInt(pCat.getText())) == null){
- alert.setContentText("Category not found");
- alert.showAndWait();
- } else if (Double.parseDouble(pPrice.getText()) <= 0){
- alert.setContentText("Price must be greater than 0");
- alert.showAndWait();
- } else {
- product.setName(pName.getText());
- product.setDescription(pDesc.getText());
- product.setPrice(Double.parseDouble(pPrice.getText()));
- product.setCategory(Integer.parseInt(pCat.getText()));
- productDAO.updateProduct(product);
- borderPane.setCenter(setProduct(product.getID()));
- }});
- Button back = new Button("back");
- back.setOnAction(event -> borderPane.setCenter(productBody));
- HBox pNavigation = new HBox();
- pNavigation.getChildren().addAll(update, back);
- VBox pBox = new VBox();
- pBox.getChildren().addAll(nameBox, descBox, priceBox, catBox, pNavigation);
- pBox.setPadding(new Insets(10));
- return pBox;
- }
- /**
- * Metoden setCategory() returnerer en HBox som skal brukes som centre i borderPane.
- * Denne metoden er del av deloppgave 5.
- *
- * @param id
- * @return HBox med Category med riktig id, og en liste over produkter med den Category-en
- */ public HBox setCategory(int id){
- ListView<Product> categoryProducts = new ListView<>();
- Category category = categoryDAO.getCategory(id);
- for(Product p : products.getItems()){
- if(p.getCategory() == category.getID()) {
- categoryProducts.getItems().add(p);
- }
- }
- VBox cVbox = new VBox();
- cVbox.getChildren().addAll(new Label(category.getName()), categoryProducts);
- HBox cHbox = new HBox();
- cHbox.getChildren().addAll(cVbox);
- cHbox.setPadding(new Insets(10));
- return cHbox;
- }
- /**
- * Metoden setNewCategory() returnerer en VBox som lar deg danne nye Category-objekter
- * Denne skal brukes som center i borderPane
- *
- * @param categoryBody HBox-en vi går tilbake til når vi rykker "back"-knappen
- * @return VBox
- */
- public VBox setNewCategory(HBox categoryBody) {
- TextField catName = new TextField();
- HBox nameBox = new HBox(new Label("Category name: " + "\t"), catName);
- Button insert = new Button("insert");
- Alert alert = new Alert(Alert.AlertType.ERROR);
- insert.setOnAction(event -> {
- if(catName.getText().isEmpty()){
- alert.setContentText("All field must be filled");
- alert.showAndWait();
- } else {
- Category category = new Category(catName.getText());
- int categoryID = categoryDAO.insertCategory(category);
- category.setID(categoryID);
- System.out.println("new Category: " + category.getName());
- categories.getItems().add(category);
- catName.clear();
- }});
- Button back = new Button("back");
- back.setOnAction(event -> borderPane.setCenter(categoryBody));
- HBox catNavigation = new HBox();
- catNavigation.getChildren().addAll(insert, back);
- VBox catBox = new VBox();
- catBox.getChildren().addAll(nameBox, catNavigation);
- catBox.setPadding(new Insets(10));
- return catBox;
- }
- /**
- * Metoden editCategory() lar deg redigere ett Category-objekt. Disse endringene skjer også i databasen.
- *
- * @param category kunden vi vil endre
- * @param categoryBody bodien vi vil returnere til når vi trykker "back"-knappen
- * @return VBox som lar deg redigere ett Customer-objekt
- */
- private VBox editCategory(Category category, HBox categoryBody) {
- TextField catName = new TextField(category.getName());
- HBox nameBox = new HBox(new Label("Edit Category name: " + "\t"), catName);
- Alert alert = new Alert(Alert.AlertType.ERROR);
- Button update = new Button("update");
- update.setOnAction(event -> {
- if(catName.getText().isEmpty()){
- alert.setContentText("All fields must be filled");
- alert.showAndWait();
- } else {
- category.setName(catName.getText());
- categoryDAO.updateCategory(category);
- borderPane.setCenter(setCategory(category.getID()));
- }});
- Button back = new Button("back");
- back.setOnAction(event -> borderPane.setCenter(categoryBody));
- HBox catNavigation = new HBox();
- catNavigation.getChildren().addAll(update, back);
- VBox cBox = new VBox();
- cBox.getChildren().addAll(nameBox, catNavigation);
- cBox.setPadding(new Insets(10));
- return cBox;
- }
- /**
- * Metoden invoicesByCustomers() returnerer en HBox som skal brukes som centre i borderPane.
- * Denne metoden er del av deloppgave 5
- *
- * @param id
- * @return HBox som viser invoices som er knyttet til Customer med riktig id
- */
- public HBox invoicesByCustomers(int id){
- ListView<Invoice> customerInvoices = new ListView<>();
- Customer customer = customerDAO.getCustomer(id);
- for(Invoice i : invoices.getItems()){
- if(i.getCustomer() == customer.getID()){
- customerInvoices.getItems().add(i);
- }
- }
- VBox ibcVbox = new VBox();
- ibcVbox.getChildren().addAll(new Label(customer.getName()), customerInvoices);
- HBox ibcHbox = new HBox();
- ibcHbox.getChildren().addAll(ibcVbox);
- ibcHbox.setPadding(new Insets(10));
- return ibcHbox;
- }
- /**
- * Metoden productByCUstomers() returnerer en HBox som skal brukes som centre i borderPane.
- * Denne metoden er del av deloppgave 5
- *
- * @param id
- * @return HBox som viser Products som er knyttet til Customer med riktig id
- */
- public HBox productByCustomers(int id){
- ListView<Product> customerProducts = new ListView<>();
- Customer customer = customerDAO.getCustomer(id);
- for(Invoice i : invoices.getItems()){
- if(i.getCustomer() == customer.getID()){
- for(Product p : invoiceDAO.getInvoice(i.getID()).getProducts()){
- customerProducts.getItems().add(p);
- }
- }
- }
- VBox pbcVbox = new VBox();
- pbcVbox.getChildren().addAll(new Label(customer.getName()), customerProducts);
- HBox pbcHbox = new HBox();
- pbcHbox.getChildren().addAll(pbcVbox);
- pbcHbox.setPadding(new Insets(10));
- return pbcHbox;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement