Advertisement
Guest User

local picture

a guest
Apr 4th, 2020
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 33.12 KB | None | 0 0
  1. package nl.tudelft.oopp.demo.controllers;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.math.BigDecimal;
  7. import javafx.event.ActionEvent;
  8. import javafx.fxml.FXML;
  9. import javafx.fxml.FXMLLoader;
  10. import javafx.scene.Node;
  11. import javafx.scene.Parent;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Alert;
  14. import javafx.scene.control.MenuButton;
  15. import javafx.scene.control.Spinner;
  16. import javafx.scene.control.SpinnerValueFactory;
  17. import javafx.scene.control.TextField;
  18. import javafx.scene.image.Image;
  19. import javafx.scene.image.ImageView;
  20. import javafx.scene.text.Text;
  21. import javafx.stage.Stage;
  22. import nl.tudelft.oopp.demo.communication.ServerCommunicationMerchandiseStore;
  23. import nl.tudelft.oopp.demo.usersession.UserSession;
  24.  
  25.  
  26. public class ManageMerchandiseController {
  27.  
  28.     @FXML private MenuButton adminManageButton;
  29.     @FXML private TextField productName;
  30.     @FXML private TextField imageUrl;
  31.     @FXML private Text welcomeText;
  32.     @FXML private Spinner<Double> merchandisePrice;
  33.     @FXML private ImageView imageUrlPreview;
  34.  
  35.     /**
  36.      * The initialize method appends the welcome text depending on the name of the
  37.      * user and initializes the price spinner and the image URL preview.
  38.      */
  39.     public void initialize() {
  40.         welcomeText.setText("Welcome " + UserSession.getInstance().getFullName());
  41.  
  42.         SpinnerValueFactory<Double> capacityValueFactory = new SpinnerValueFactory
  43.                 .DoubleSpinnerValueFactory(0.00, Double.MAX_VALUE, 0.00, 0.10);
  44.         this.merchandisePrice.setValueFactory(capacityValueFactory);
  45.         merchandisePrice.setEditable(true);
  46.  
  47.         try {
  48.             imageUrlPreview.setImage(new Image(new FileInputStream(
  49.                     "client/src/main/resources/tud.jpg")));
  50.         } catch (FileNotFoundException e) {
  51.             e.printStackTrace();
  52.         }
  53.     }
  54.  
  55.     /**When the add button is pressed, the method performs checks on the input field. If
  56.      * everything conforms, the merchandise item is sent to the server.
  57.      *
  58.      */
  59.     public void addButtonClicked() {
  60.         Alert alert = new Alert(Alert.AlertType.INFORMATION);
  61.         if (productName.getText().isEmpty() || productName.getText().length() == 0) {
  62.             alert.setHeaderText(null);
  63.             alert.setContentText("Please enter the name of the product in the given field.");
  64.             alert.showAndWait();
  65.             return;
  66.         }
  67.         if (BigDecimal.valueOf(merchandisePrice.getValue()).scale() > 2) {
  68.             alert.setHeaderText(null);
  69.             alert.setContentText("Please only enter maximum of 2 decimal points in price");
  70.             alert.showAndWait();
  71.             return;
  72.         }
  73.         String nameOfMerchandise = productName.getText().replace(" ", "%20");
  74.         if (!ServerCommunicationMerchandiseStore.checkName(nameOfMerchandise)) {
  75.             alert.setHeaderText(null);
  76.             alert.setContentText("A product with the entered name already exists");
  77.             alert.showAndWait();
  78.             return;
  79.         }
  80.  
  81.         String productNameString = productName.getText().replace(" ", "%20");
  82.         String imageUrlString = imageUrl.getText().toString().replace(" ", "%20");
  83.         ServerCommunicationMerchandiseStore.addMerchandise(merchandisePrice.getValue(),
  84.                 productNameString, imageUrlString);
  85.         alert.setHeaderText(null);
  86.         alert.setContentText("Merchandise item has been added successfully!");
  87.         alert.showAndWait();
  88.  
  89.         productName.setText("");
  90.         imageUrl.setText("");
  91.         merchandisePrice.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(
  92.                 0.00, Double.MAX_VALUE, 0.00, 0.10));
  93.     }
  94.  
  95.     /**Display the image corresponding to the URL provided by the user.
  96.      *
  97.      * @param event which receives the I/O event.
  98.      */
  99.     public void previewImageClicked(ActionEvent event) {
  100.         Alert alert = new Alert(Alert.AlertType.INFORMATION);
  101.         if (imageUrl.getText().isEmpty() || imageUrl.getText().length() == 0) {
  102.             alert.setHeaderText(null);
  103.             alert.setContentText("Please enter the URL of the image to preview it.");
  104.             alert.showAndWait();
  105.             return;
  106.         }
  107.         try {
  108.             imageUrlPreview.setImage(new Image(imageUrl.getText().toString()));
  109.         } catch (IllegalArgumentException e) {
  110.             try {
  111.                 imageUrlPreview.setImage(new Image(new FileInputStream(
  112.                         "client/src/main/resources/tud.jpg")));
  113.             } catch (FileNotFoundException ex) {
  114.                 ex.printStackTrace();
  115.             }
  116.  
  117.             alert.setHeaderText(null);
  118.             alert.setContentText("The URL you entered is not valid");
  119.             alert.showAndWait();
  120.             return;
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * Changes scene to the loginScreen when the button is clicked. The user-session gets cleared.
  126.      * @param event which receives the I/O event.
  127.      * @throws IOException when load is unsuccessful.
  128.      */
  129.     public void logOutClicked(ActionEvent event) throws IOException {
  130.         UserSession.getInstance().cleanUserSession();
  131.         Parent parent = FXMLLoader.load(getClass().getResource("/loginScreen.fxml"));
  132.         Scene newScene = new Scene(parent);
  133.  
  134.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  135.         window.setScene(newScene);
  136.         window.show();
  137.     }
  138.  
  139.     /**
  140.      * Changes scene to the manage buildings page once the respective button has been clicked.
  141.      * @throws IOException when load is unsuccessful.
  142.      */
  143.     public void manageBuildingClicked() throws IOException {
  144.         Parent parent = FXMLLoader.load(getClass().getResource("/manageBuildings.fxml"));
  145.         Scene newScene = new Scene(parent);
  146.  
  147.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  148.         window.setScene(newScene);
  149.         window.show();
  150.     }
  151.  
  152.     /**
  153.      * Changes scene to the manage rooms page once the respective button has been clicked.
  154.      * @throws IOException when load is unsuccessful.
  155.      */
  156.     public void manageRoomsClicked() throws IOException {
  157.         Parent parent = FXMLLoader.load(getClass().getResource("/manageRooms.fxml"));
  158.         Scene newScene = new Scene(parent);
  159.  
  160.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  161.         window.setScene(newScene);
  162.         window.show();
  163.     }
  164.  
  165.  
  166.     /**
  167.      * Changes scene to the create admin page once the respective button has been clicked.
  168.      * @throws IOException when load is unsuccessful.
  169.      */
  170.     public void createAdminClicked() throws IOException {
  171.         Parent parent = FXMLLoader.load(getClass().getResource("/createAdmin.fxml"));
  172.         Scene newScene = new Scene(parent);
  173.  
  174.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  175.         window.setScene(newScene);
  176.         window.show();
  177.     }
  178.  
  179.     /**
  180.      * Changes scene to the manage food items page once the respective button has been clicked.
  181.      * @throws IOException when load is unsuccessful.
  182.      */
  183.     public void manageFoodClicked() throws IOException {
  184.         Parent parent = FXMLLoader.load(getClass().getResource("/manageFoodItems.fxml"));
  185.         Scene newScene = new Scene(parent);
  186.  
  187.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  188.         window.setScene(newScene);
  189.         window.show();
  190.     }
  191.  
  192.     /**
  193.      * Changes scene to the home page once the respective button has been clicked.
  194.      * @param event which receives the I/O event.
  195.      * @throws IOException when load is unsuccessful.
  196.      */
  197.     public void homeButtonClicked(ActionEvent event) throws IOException {
  198.         Parent parent = FXMLLoader.load(getClass().getResource("/homeScreen.fxml"));
  199.         Scene newScene = new Scene(parent);
  200.  
  201.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  202.         window.setScene(newScene);
  203.         window.show();
  204.     }
  205.  
  206.     /**
  207.      * Changes scene to the reserve a room page once the respective button has been clicked.
  208.      * @param event which receives the I/O event.
  209.      * @throws IOException when load is unsuccessful.
  210.      */
  211.     public void reserveRoomClicked(ActionEvent event) throws IOException {
  212.         Parent parent = FXMLLoader.load(getClass().getResource("/roomReservation.fxml"));
  213.         Scene newScene = new Scene(parent);
  214.  
  215.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  216.         window.setScene(newScene);
  217.         window.show();
  218.     }
  219.  
  220.     /**
  221.      * Changes scene to the food order page once the respective button has been clicked.
  222.      * @param event which receives the I/O event.
  223.      * @throws IOException when load is unsuccessful.
  224.      */
  225.     public void foodOrderClicked(ActionEvent event) throws IOException {
  226.         Parent parent = FXMLLoader.load(getClass().getResource("/foodOrder.fxml"));
  227.         Scene newScene = new Scene(parent);
  228.  
  229.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  230.         window.setScene(newScene);
  231.         window.show();
  232.     }
  233.  
  234.     /**
  235.      * Changes scene to the rent a bike page once the respective button has been clicked.
  236.      * @param event which receives the I/O event.
  237.      * @throws IOException when load is unsuccessful.
  238.      */
  239.     public void bikeReservationClicked(ActionEvent event) throws IOException {
  240.         Parent parent = FXMLLoader.load(getClass().getResource("/bikeReservation.fxml"));
  241.         Scene newScene = new Scene(parent);
  242.  
  243.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  244.         window.setScene(newScene);
  245.         window.show();
  246.     }
  247.  
  248.     /**
  249.      * Changes scene to the FAQ page once the respective button has been clicked.
  250.      * @param event which receives the I/O event.
  251.      * @throws IOException when load is unsuccessful.
  252.      */
  253.     public void faqClicked(ActionEvent event) throws IOException {
  254.         Parent parent = FXMLLoader.load(getClass().getResource("/FAQ.fxml"));
  255.         Scene newScene = new Scene(parent);
  256.  
  257.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  258.         window.setScene(newScene);
  259.         window.show();
  260.     }
  261.  
  262.     /**
  263.      * Changes scene to the merchandise store page once the respective button has been clicked.
  264.      *
  265.      * @param event which receives the I/O event.
  266.      * @throws IOException when load is unsuccessful.
  267.      */
  268.     public void merchandiseStoreClicked(ActionEvent event) throws IOException {
  269.         Parent parent = FXMLLoader.load(getClass().getResource("/MerchandiseStore.fxml"));
  270.         Scene newScene = new Scene(parent);
  271.  
  272.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  273.         window.setScene(newScene);
  274.         window.show();
  275.     }
  276.  
  277.     /**
  278.      * Changes scene to the delete food item page once the respective button has been clicked.
  279.      * @param event which receives the I/O event.
  280.      * @throws IOException when load is unsuccessful.
  281.      */
  282.     public void deleteFoodClicked(ActionEvent event) throws IOException {
  283.         Parent parent = FXMLLoader.load(getClass().getResource("/deleteFoodItem.fxml"));
  284.         Scene newScene = new Scene(parent);
  285.  
  286.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  287.         window.setScene(newScene);
  288.         window.show();
  289.     }
  290.  
  291.     /**
  292.      * Changes scene to the manage merchandise page once the respective button has been clicked.
  293.      * @param event which receives the I/O event.
  294.      * @throws IOException when load is unsuccessful.
  295.      */
  296.     public void manageMerchandiseClicked(ActionEvent event) throws IOException {
  297.         Parent parent = FXMLLoader.load(getClass().getResource("/manageMerchandise.fxml"));
  298.         Scene newScene = new Scene(parent);
  299.  
  300.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  301.         window.setScene(newScene);
  302.         window.show();
  303.     }
  304.  
  305.     /**
  306.      * Changes scene to the delete merchandise page once the respective button has been clicked.
  307.      * @param event which receives the I/O event.
  308.      * @throws IOException when load is unsuccessful.
  309.      */
  310.     public void deleteMerchandiseClicked(ActionEvent event) throws IOException {
  311.         Parent parent = FXMLLoader.load(getClass().getResource("/deleteMerchandise.fxml"));
  312.         Scene newScene = new Scene(parent);
  313.  
  314.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  315.         window.setScene(newScene);
  316.         window.show();
  317.     }
  318.  
  319.  
  320.  
  321. }
  322.  
  323.  
  324.  
  325.  
  326.  
  327. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  328. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  329. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  330. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  331. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  332. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  333. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  334. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  335.  
  336.      
  337.  
  338.  
  339.  
  340. package nl.tudelft.oopp.demo.controllers;
  341.  
  342. import com.google.gson.Gson;
  343.  
  344. import java.io.FileInputStream;
  345. import java.io.FileNotFoundException;
  346. import java.io.IOException;
  347. import java.text.DecimalFormat;
  348. import java.time.LocalDate;
  349. import java.time.format.DateTimeFormatter;
  350. import java.util.ArrayList;
  351. import java.util.Collections;
  352. import java.util.regex.Matcher;
  353. import java.util.regex.Pattern;
  354.  
  355. import javafx.collections.FXCollections;
  356. import javafx.collections.ObservableList;
  357.  
  358. import javafx.event.ActionEvent;
  359. import javafx.fxml.FXML;
  360. import javafx.fxml.FXMLLoader;
  361. import javafx.scene.Node;
  362. import javafx.scene.Parent;
  363. import javafx.scene.Scene;
  364. import javafx.scene.control.Alert;
  365. import javafx.scene.control.ChoiceBox;
  366. import javafx.scene.control.Label;
  367. import javafx.scene.control.ListCell;
  368. import javafx.scene.control.ListView;
  369. import javafx.scene.control.MenuButton;
  370. import javafx.scene.image.Image;
  371. import javafx.scene.image.ImageView;
  372. import javafx.scene.layout.Region;
  373. import javafx.scene.text.Text;
  374. import javafx.stage.Stage;
  375.  
  376. import nl.tudelft.oopp.demo.communication.ServerCommunicationMerchandiseStore;
  377. import nl.tudelft.oopp.demo.entities.Merchandise;
  378. import nl.tudelft.oopp.demo.usersession.UserSession;
  379.  
  380. public class MerchandiseStoreController {
  381.  
  382.     @FXML
  383.     private MenuButton adminManageButton;
  384.     @FXML
  385.     private ListView listViewMerchandise;
  386.     @FXML
  387.     private ListView listViewBasket;
  388.     @FXML
  389.     private Label totalPriceDisplay;
  390.     @FXML
  391.     private Text welcomeText;
  392.     @FXML
  393.     private ChoiceBox selectSortBy;
  394.     @FXML
  395.     private ArrayList<String> sortByList;
  396.     private ArrayList<Merchandise> merchandiseBasket;
  397.     private ArrayList<Merchandise> merchandiseOrderedList;
  398.     private double totalPrice = 0.00;
  399.  
  400.     /**Constructor for the MerchandiseStoreController class.
  401.      *
  402.      */
  403.     public MerchandiseStoreController() {
  404.         this.sortByList = new ArrayList<>();
  405.         this.merchandiseOrderedList = new ArrayList<>();
  406.         this.merchandiseBasket = new ArrayList<>();
  407.         this.totalPriceDisplay = new Label();
  408.     }
  409.  
  410.     /**
  411.      * Initialize method gets executed before the page has been loaded. It retrieves and loads
  412.      * the names of the merchandise items.
  413.      */
  414.     public void initialize() {
  415.         if (!UserSession.getInstance().getRole().equals("Admin")) {
  416.             adminManageButton.setDisable(true);
  417.             adminManageButton.setVisible(false);
  418.         }
  419.         welcomeText.setText("Welcome " + UserSession.getInstance().getFullName());
  420.  
  421.         ObservableList<String> tempList = FXCollections.observableArrayList();
  422.         String regex = "\\{([^}]+)}";
  423.         Pattern pattern = Pattern.compile(regex);
  424.         Matcher matcher = pattern.matcher(ServerCommunicationMerchandiseStore
  425.                 .getMerchandiseOrderByName());
  426.         Gson gson = new Gson();
  427.         while (matcher.find()) {
  428.             tempList.add(matcher.group());
  429.         }
  430.  
  431.         ObservableList<String> res = FXCollections.observableArrayList();
  432.         for (String jsonBuilding : tempList) {
  433.             Merchandise merchandise = gson.fromJson(jsonBuilding, Merchandise.class);
  434.             merchandiseOrderedList.add(merchandise);
  435.             res.add(merchandise.toString());
  436.         }
  437.  
  438.         listViewMerchandise.setMinHeight(60);
  439.         listViewMerchandise.setItems(res);
  440.  
  441.         listViewMerchandise.setCellFactory(param -> new ListCell<String>() {
  442.             @Override
  443.             public void updateItem(String text, boolean empty) {
  444.                 super.updateItem(text, empty);
  445.                 if (empty) {
  446.                     setText(null);
  447.                     setGraphic(null);
  448.                     return;
  449.                 }
  450.                 ImageView picture = new ImageView();
  451.                 picture.setSmooth(true);
  452.                 picture.setPreserveRatio(true);
  453.                 picture.resize(75, 75);
  454.                 picture.setFitHeight(75);
  455.                 picture.setFitWidth(75);
  456.                 Merchandise merchandise = Merchandise
  457.                         .findMerchandiseInList(merchandiseOrderedList, text);
  458.                 setText(text);
  459.                 if (merchandise.hasImage()) {
  460.                     try {
  461.                         picture.setImage(new Image(merchandise.getImage()));
  462.                         setGraphic(picture);
  463.                     } catch (IllegalArgumentException e) {
  464.                         try {
  465.                             picture.setImage(new Image(new FileInputStream(
  466.                                     "client/src/main/resources/tud.jpg")));
  467.                             setGraphic(picture);
  468.                         } catch (FileNotFoundException e2) {
  469.                             setGraphic(null);
  470.                         }
  471.                     }
  472.                 }
  473.             }
  474.         });
  475.  
  476.     }
  477.  
  478.     /**Sorts the list of merchandise items according to the selected sorting
  479.      * parameter.
  480.      *
  481.      * @param actionEvent which receives the I/O event.
  482.      * @throws IOException when load is unsuccessful.
  483.      */
  484.     public void sortBySelected(ActionEvent actionEvent) throws IOException {
  485.         ObservableList<String> tempList = FXCollections.observableArrayList();
  486.         String regex = "\\{([^}]+)}";
  487.         Pattern pattern1 = Pattern.compile(regex);
  488.         String selectedSortBy = selectSortBy.getValue().toString();
  489.         Matcher matcher;
  490.         if (selectedSortBy.equals("Name")) {
  491.             matcher = pattern1.matcher(ServerCommunicationMerchandiseStore
  492.                     .getMerchandiseOrderByName());
  493.         } else if (selectedSortBy.equals("Price (asc)")) {
  494.             matcher = pattern1.matcher(ServerCommunicationMerchandiseStore
  495.                     .getMerchandiseOrderByPriceLowToHigh());
  496.         } else if (selectedSortBy.equals("Price (desc)")) {
  497.             matcher = pattern1.matcher(ServerCommunicationMerchandiseStore
  498.                     .getMerchandiseOrderByPriceHighToLow());
  499.         } else {
  500.             throw new IllegalArgumentException("Sort by parameter error");
  501.         }
  502.  
  503.         Gson gson = new Gson();
  504.         while (matcher.find()) {
  505.             tempList.add(matcher.group());
  506.         }
  507.  
  508.         ObservableList<String> res = FXCollections.observableArrayList();
  509.         merchandiseOrderedList.clear();
  510.         for (String jsonBuilding : tempList) {
  511.             Merchandise merchandise = gson.fromJson(jsonBuilding, Merchandise.class);
  512.             merchandiseOrderedList.add(merchandise);
  513.             res.add(merchandise.toString());
  514.         }
  515.  
  516.         listViewMerchandise.setItems(res);
  517.     }
  518.  
  519.     /**When the add button us clicked, the selected merchandise item is added to the basket.
  520.      *
  521.      * @param actionEvent which receives the I/O event.
  522.      * @throws IOException when load is unsuccessful.
  523.      */
  524.     public void addMerchandiseClicked(ActionEvent actionEvent) throws IOException {
  525.         if (listViewMerchandise.getSelectionModel().getSelectedItems() != null) {
  526.             Merchandise merchandise = Merchandise.findMerchandiseInList(merchandiseOrderedList,
  527.                     (String) listViewMerchandise.getSelectionModel().getSelectedItems().get(0));
  528.             merchandiseBasket.add(merchandise);
  529.  
  530.             updateBasketDisplay(merchandiseBasket, listViewBasket);
  531.             updateTotalPrice(merchandiseBasket);
  532.         } else {
  533.             Alert alert = new Alert(Alert.AlertType.INFORMATION);
  534.             alert.setHeaderText(null);
  535.             alert.setContentText("Please select an item to add to your basket");
  536.             alert.showAndWait();
  537.         }
  538.     }
  539.  
  540.     /**When the delete button us clicked, the selected merchandise item is removed from the basket.
  541.      *
  542.      * @param actionEvent which receives the I/O event.
  543.      * @throws IOException when load is unsuccessful.
  544.      */
  545.     public void deleteMerchandiseClicked(ActionEvent actionEvent) throws IOException {
  546.         if (listViewMerchandise.getSelectionModel().getSelectedItems() != null) {
  547.             Merchandise merchandise = Merchandise.findMerchandiseInList(merchandiseOrderedList,
  548.                     (String) listViewMerchandise.getSelectionModel().getSelectedItems().get(0));
  549.             merchandiseBasket.remove(merchandise);
  550.  
  551.             updateBasketDisplay(merchandiseBasket, listViewBasket);
  552.             updateTotalPrice(merchandiseBasket);
  553.         } else {
  554.             Alert alert = new Alert(Alert.AlertType.INFORMATION);
  555.             alert.setHeaderText(null);
  556.             alert.setContentText("Please select an item to delete from your basket");
  557.             alert.showAndWait();
  558.         }
  559.     }
  560.  
  561.     /**Updates the total price displayed on screen by recalculating the
  562.      * total cost of all merchandise items in the basket.
  563.      *
  564.      * @param merchandiseBasket ArrayList containing the merchandise items inside of the basket
  565.      */
  566.     public void updateTotalPrice(ArrayList<Merchandise> merchandiseBasket) {
  567.         DecimalFormat decimalFormat = new DecimalFormat("0.00");
  568.         totalPrice = 0;
  569.         for (Merchandise m : this.merchandiseBasket) {
  570.             totalPrice = totalPrice + m.getPrice();
  571.         }
  572.         totalPriceDisplay.setText(decimalFormat.format(totalPrice) + " euro");
  573.     }
  574.  
  575.     /**Updates the merchandise items displayed on screen.This method is called after
  576.      * a merchandise item has benn added/deleted from the basket.
  577.      *
  578.      * @param merchandiseBasket ArrayList containing the merchandise items inside of the basket
  579.      * @param listViewBasket ArrayList containing the merchandise items inside of the basket
  580.      *                       displayed on screen
  581.      */
  582.     public void updateBasketDisplay(ArrayList<Merchandise> merchandiseBasket,
  583.                                     ListView listViewBasket) {
  584.         ObservableList<String> res = FXCollections.observableArrayList();
  585.         ArrayList<Merchandise> occurrences = new ArrayList<>();
  586.         for (Merchandise m : this.merchandiseBasket) {
  587.             if (!occurrences.contains(m)) {
  588.                 res.remove(m);
  589.                 res.add(m.toStringQuantity(Collections.frequency(this.merchandiseBasket, m)));
  590.             }
  591.  
  592.             occurrences.add(m);
  593.         }
  594.         this.listViewBasket.setItems(res);
  595.     }
  596.  
  597.     /**When the "place order" button is pressed, this method first perform checks to verify
  598.      * that all fields are provided. Then, merchandiseOrder objects are generated according
  599.      * to the merchandise items in the basket.
  600.      *
  601.      * @param actionEvent event which receives the I/O event.
  602.      * @throws IOException when load is unsuccessful.
  603.      */
  604.     public void placeOrder(ActionEvent actionEvent) throws IOException {
  605.         Alert alert = new Alert(Alert.AlertType.INFORMATION);
  606.         if (merchandiseBasket.isEmpty()) {
  607.             alert.setHeaderText(null);
  608.             alert.setContentText("Please add items to your basket before placing your order");
  609.             alert.showAndWait();
  610.             return;
  611.         }
  612.  
  613.         saveOrders(merchandiseBasket);
  614.  
  615.         Parent parent = FXMLLoader.load(getClass().getResource("/MerchandiseStore.fxml"));
  616.         Scene newScene = new Scene(parent);
  617.  
  618.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  619.         window.setScene(newScene);
  620.         window.show();
  621.     }
  622.  
  623.     /**Performs checks on the current time and date and then generates merchandise orders
  624.      * with the merchandise items in the basket.
  625.      *
  626.      * @param merchandiseBasket ArrayList containing the merchandise items inside of the basket
  627.      */
  628.     public void saveOrders(ArrayList<Merchandise> merchandiseBasket) {
  629.         LocalDate orderDate = LocalDate.now();
  630.         DateTimeFormatter  dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  631.         String date = dateFormat.format(orderDate);
  632.  
  633.         ArrayList<Merchandise> occurrences = new ArrayList<>();
  634.         for (Merchandise m : merchandiseBasket) {
  635.             if (!occurrences.contains(m)) {
  636.                 ServerCommunicationMerchandiseStore.addMerchandiseOrder(
  637.                         m.getId(),  UserSession.getInstance().getId(),
  638.                         Collections.frequency(merchandiseBasket, m),
  639.                         date);
  640.                 occurrences.add(m);
  641.             }
  642.         }
  643.  
  644.         Alert alert = new Alert(Alert.AlertType.INFORMATION);
  645.         alert.setHeaderText(null);
  646.         alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
  647.         alert.setContentText("Your order has been placed successfully. The TU Delft "
  648.                 + "gift shop will soon contact you to verify the availability of your "
  649.                 + "ordered products and arrange a pick-up date");
  650.         alert.showAndWait();
  651.     }
  652.  
  653.     /**
  654.      * Changes scene to the loginScreen when the button is clicked. The user-session gets cleared.
  655.      *
  656.      * @param event which receives the I/O event.
  657.      * @throws IOException when load is unsuccessful.
  658.      */
  659.     public void logOutClicked(ActionEvent event) throws IOException {
  660.         UserSession.getInstance().cleanUserSession();
  661.         Parent parent = FXMLLoader.load(getClass().getResource("/loginScreen.fxml"));
  662.         Scene newScene = new Scene(parent);
  663.  
  664.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  665.         window.setScene(newScene);
  666.         window.show();
  667.     }
  668.  
  669.     /**
  670.      * Changes scene to the manage buildings page once the respective button has been clicked.
  671.      *
  672.      * @throws IOException when load is unsuccessful.
  673.      */
  674.     public void manageBuildingClicked() throws IOException {
  675.         Parent parent = FXMLLoader.load(getClass().getResource("/manageBuildings.fxml"));
  676.         Scene newScene = new Scene(parent);
  677.  
  678.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  679.         window.setScene(newScene);
  680.         window.show();
  681.     }
  682.  
  683.     /**
  684.      * Changes scene to the manage rooms page once the respective button has been clicked.
  685.      *
  686.      * @throws IOException when load is unsuccessful.
  687.      */
  688.     public void manageRoomsClicked() throws IOException {
  689.         Parent parent = FXMLLoader.load(getClass().getResource("/manageRooms.fxml"));
  690.         Scene newScene = new Scene(parent);
  691.  
  692.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  693.         window.setScene(newScene);
  694.         window.show();
  695.     }
  696.  
  697.  
  698.     /**
  699.      * Changes scene to the create admin page once the respective button has been clicked.
  700.      *
  701.      * @throws IOException when load is unsuccessful.
  702.      */
  703.     public void createAdminClicked() throws IOException {
  704.         Parent parent = FXMLLoader.load(getClass().getResource("/createAdmin.fxml"));
  705.         Scene newScene = new Scene(parent);
  706.  
  707.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  708.         window.setScene(newScene);
  709.         window.show();
  710.     }
  711.  
  712.     /**
  713.      * Changes scene to the manage food items page once the respective button has been clicked.
  714.      *
  715.      * @throws IOException when load is unsuccessful.
  716.      */
  717.     public void manageFoodClicked() throws IOException {
  718.         Parent parent = FXMLLoader.load(getClass().getResource("/manageFoodItems.fxml"));
  719.         Scene newScene = new Scene(parent);
  720.  
  721.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  722.         window.setScene(newScene);
  723.         window.show();
  724.     }
  725.  
  726.     /**
  727.      * Changes scene to the home page once the respective button has been clicked.
  728.      *
  729.      * @param event which receives the I/O event.
  730.      * @throws IOException when load is unsuccessful.
  731.      */
  732.     public void homeButtonClicked(ActionEvent event) throws IOException {
  733.         Parent parent = FXMLLoader.load(getClass().getResource("/homeScreen.fxml"));
  734.         Scene newScene = new Scene(parent);
  735.  
  736.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  737.         window.setScene(newScene);
  738.         window.show();
  739.     }
  740.  
  741.     /**
  742.      * Changes scene to the reserve a room page once the respective button has been clicked.
  743.      *
  744.      * @param event which receives the I/O event.
  745.      * @throws IOException when load is unsuccessful.
  746.      */
  747.     public void reserveRoomClicked(ActionEvent event) throws IOException {
  748.         Parent parent = FXMLLoader.load(getClass().getResource("/roomReservation.fxml"));
  749.         Scene newScene = new Scene(parent);
  750.  
  751.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  752.         window.setScene(newScene);
  753.         window.show();
  754.     }
  755.  
  756.     /**
  757.      * Changes scene to the food order page once the respective button has been clicked.
  758.      *
  759.      * @param event which receives the I/O event.
  760.      * @throws IOException when load is unsuccessful.
  761.      */
  762.     public void foodOrderClicked(ActionEvent event) throws IOException {
  763.         Parent parent = FXMLLoader.load(getClass().getResource("/foodOrder.fxml"));
  764.         Scene newScene = new Scene(parent);
  765.  
  766.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  767.         window.setScene(newScene);
  768.         window.show();
  769.     }
  770.  
  771.     /**
  772.      * Changes scene to the rent a bike page once the respective button has been clicked.
  773.      *
  774.      * @param event which receives the I/O event.
  775.      * @throws IOException when load is unsuccessful.
  776.      */
  777.     public void bikeReservationClicked(ActionEvent event) throws IOException {
  778.         Parent parent = FXMLLoader.load(getClass().getResource("/bikeReservation.fxml"));
  779.         Scene newScene = new Scene(parent);
  780.  
  781.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  782.         window.setScene(newScene);
  783.         window.show();
  784.     }
  785.  
  786.     /**
  787.      * Changes scene to the FAQ page once the respective button has been clicked.
  788.      *
  789.      * @param event which receives the I/O event.
  790.      * @throws IOException when load is unsuccessful.
  791.      */
  792.     public void faqClicked(ActionEvent event) throws IOException {
  793.         Parent parent = FXMLLoader.load(getClass().getResource("/FAQ.fxml"));
  794.         Scene newScene = new Scene(parent);
  795.  
  796.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  797.         window.setScene(newScene);
  798.         window.show();
  799.     }
  800.  
  801.     /**
  802.      * Changes scene to the merchandise store page once the respective button has been clicked.
  803.      *
  804.      * @param event which receives the I/O event.
  805.      * @throws IOException when load is unsuccessful.
  806.      */
  807.     public void merchandiseStoreClicked(ActionEvent event) throws IOException {
  808.         Parent parent = FXMLLoader.load(getClass().getResource("/MerchandiseStore.fxml"));
  809.         Scene newScene = new Scene(parent);
  810.  
  811.         Stage window = (Stage) (((Node) event.getSource()).getScene().getWindow());
  812.         window.setScene(newScene);
  813.         window.show();
  814.     }
  815.  
  816.     /**
  817.      * Changes scene to the manage merchandise page once the respective button has been clicked.
  818.      * @param event which receives the I/O event.
  819.      * @throws IOException when load is unsuccessful.
  820.      */
  821.     public void manageMerchandiseClicked(ActionEvent event) throws IOException {
  822.         Parent parent = FXMLLoader.load(getClass().getResource("/manageMerchandise.fxml"));
  823.         Scene newScene = new Scene(parent);
  824.  
  825.         Stage window = (Stage) adminManageButton.getScene().getWindow();
  826.         window.setScene(newScene);
  827.         window.show();
  828.     }
  829.  
  830.  
  831. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement