Advertisement
Guest User

admin

a guest
Jan 26th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.73 KB | None | 0 0
  1. package application;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5.  
  6. import data.ProductIO;
  7. import data.UserIO;
  8. import employees.Admin;
  9. import employees.Cashier;
  10. import employees.User;
  11. import javafx.collections.FXCollections;
  12. import javafx.collections.ObservableList;
  13. import javafx.event.ActionEvent;
  14. import javafx.event.EventHandler;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Alert;
  17. import javafx.scene.control.Alert.AlertType;
  18. import javafx.scene.control.ButtonType;
  19. import javafx.scene.control.Label;
  20. import javafx.scene.control.Menu;
  21. import javafx.scene.control.MenuBar;
  22. import javafx.scene.control.ScrollPane;
  23. import javafx.scene.control.TableColumn;
  24. import javafx.scene.control.TableView;
  25. import javafx.scene.control.ToolBar;
  26. import javafx.scene.control.cell.PropertyValueFactory;
  27. import javafx.scene.image.Image;
  28. import javafx.scene.image.ImageView;
  29. import javafx.scene.input.MouseEvent;
  30. import javafx.scene.layout.BorderPane;
  31. import javafx.scene.layout.HBox;
  32. import javafx.scene.layout.VBox;
  33. import javafx.stage.Stage;
  34. import products.Product;
  35. import resources.ResourceManager;
  36. import util.FlatButton;
  37. import util.Notification;
  38. import util.NotificationManager;
  39. import util.SharedElements;
  40.  
  41. public class AdminStage {
  42.    
  43.     private ObservableList<User> users = FXCollections.observableArrayList();
  44.     private ObservableList<Product> products = FXCollections.observableArrayList();
  45.     private TableView userData;
  46.     private TableView productData;
  47.    
  48.     public void view(Stage previousStage, Admin adm) {
  49.         UserIO uio = new UserIO();
  50.         ProductIO pio = new ProductIO();
  51.         NotificationManager nm = new NotificationManager();
  52.        
  53.         //Setting up the layout
  54.         Stage adminStage = new Stage();
  55.         adminStage.getIcons().add(SharedElements.getIcon().getImage());
  56.         ScrollPane mainPane = new ScrollPane();
  57.         BorderPane mainWindow = new BorderPane();
  58.         mainWindow.setCenter(mainPane);
  59.         ToolBar topBar = new ToolBar();
  60.         topBar.setPrefHeight(85);
  61.         topBar.setStyle("-fx-background-color: #074F76");
  62.        
  63.         HBox usersButtonTbar = new HBox(20);
  64.         ToolBar usersbottomBar = new ToolBar();
  65.         usersbottomBar.setStyle("-fx-background-color: #074F76");
  66.         usersbottomBar.getItems().add(usersButtonTbar);
  67.        
  68.         //Setting up the button's images for top toolbar
  69.         //ImageView userImg = new ImageView(new Image("resources" + File.separator + "man.png"));
  70.         ImageView userImg = new ImageView(new Image(ResourceManager.manloc.toString()));
  71.         userImg.setFitHeight(50);
  72.         userImg.setFitWidth(50);
  73.         userImg.setPreserveRatio(true);
  74.        
  75.         //ImageView productsImg = new ImageView(new Image("resources" + File.separator + "cart.png"));
  76.         ImageView productsImg = new ImageView(new Image(ResourceManager.cartloc.toString()));
  77.         productsImg.setFitHeight(50);
  78.         productsImg.setFitWidth(50);
  79.         productsImg.setPreserveRatio(true);
  80.        
  81.         //ImageView incomeImg = new ImageView(new Image("resources" + File.separator + "money.png"));
  82.         ImageView incomeImg = new ImageView(new Image(ResourceManager.moneyloc.toString()));
  83.         incomeImg.setFitHeight(50);
  84.         incomeImg.setFitWidth(50);
  85.         incomeImg.setPreserveRatio(true);
  86.        
  87.         //ImageView notifIV = new ImageView(new Image("resources" + File.separator + "notification.png"));
  88.         ImageView notifIV = new ImageView(new Image(ResourceManager.notificationloc.toString()));
  89.         notifIV.setFitHeight(50);
  90.         notifIV.setFitWidth(50);
  91.         notifIV.setPreserveRatio(true);
  92.        
  93.         //ImageView logoutIV = new ImageView(new Image("resources" + File.separator + "logout.png"));
  94.         ImageView logoutIV = new ImageView(new Image(ResourceManager.logoutloc.toString()));
  95.         logoutIV.setFitHeight(50);
  96.         logoutIV.setFitWidth(50);
  97.         logoutIV.setPreserveRatio(true);
  98.        
  99.        
  100.         //Setting up the buttons for User View
  101.         FlatButton usersButton = new FlatButton("Users", userImg);
  102.         usersButton.setPrefSize(120, 85);
  103.         FlatButton productsButton = new FlatButton("Products", productsImg);
  104.         productsButton.setPrefSize(120, 85);
  105.         FlatButton incomeButton = new FlatButton("Income", incomeImg);
  106.         incomeButton.setPrefSize(120, 85);
  107.         FlatButton notificationButton = new FlatButton("Notifications", notifIV);
  108.         notificationButton.setPrefSize(120, 85);
  109.         FlatButton logOutButton = new FlatButton("Log Out", logoutIV);
  110.         logOutButton.setPrefSize(120, 85);
  111.        
  112.         FlatButton addUserButton = new FlatButton("Add User");
  113.         FlatButton editUserButton = new FlatButton("Edit User");
  114.         FlatButton removeUserButton = new FlatButton("Remove User");
  115.         FlatButton statsUserButton = new FlatButton("View Bills");
  116.        
  117.        
  118.         //Setting up buttons for Product View
  119.         FlatButton addProductButton = new FlatButton("Add Product");
  120.         FlatButton editProductButton = new FlatButton("Edit Product");
  121.         FlatButton removeProductButton = new FlatButton("Remove Product");
  122.         ToolBar productsTBar = new ToolBar();
  123.         productsTBar.setStyle("-fx-background-color: #074F76");
  124.         productsTBar.getItems().addAll(addProductButton, editProductButton, removeProductButton);
  125.  
  126.        
  127.         userData = viewUsers(uio);
  128.         productData = viewProducts(pio);
  129.        
  130.        
  131.         //Adding top toolbar button's functions
  132.         usersButton.setOnAction(new EventHandler<ActionEvent>() {
  133.             @Override
  134.             public void handle(ActionEvent arg0) {
  135.                 mainWindow.setBottom(usersbottomBar);
  136.                 refresh(uio);
  137.                 mainPane.setContent(userData);
  138.             }
  139.         });
  140.        
  141.         productsButton.setOnAction(new EventHandler<ActionEvent>() {
  142.             @Override
  143.             public void handle(ActionEvent arg0) {
  144.                 mainWindow.setBottom(productsTBar);
  145.                 refresh(pio);
  146.                 mainPane.setContent(productData);
  147.             }
  148.         });
  149.        
  150.         notificationButton.setOnAction(new EventHandler<ActionEvent>() {
  151.             @Override
  152.             public void handle(ActionEvent arg0) {
  153.                 SharedElements.notificationView(adm, nm);
  154.             }
  155.         });
  156.        
  157.         incomeButton.setOnAction(new EventHandler<ActionEvent>() {
  158.             @Override
  159.             public void handle(ActionEvent arg0) {
  160.                 SharedElements.salesView();
  161.             }
  162.         });
  163.        
  164.         logOutButton.setOnAction(new EventHandler<ActionEvent>() {
  165.             @Override
  166.             public void handle(ActionEvent arg0) {
  167.                 adminStage.close();
  168.                 LoginStage lgs = new LoginStage();
  169.                 lgs.view(adminStage, uio);
  170.             }
  171.         });
  172.        
  173.         //Adding functions to User related buttons
  174.         addUserButton.setOnAction(new EventHandler<ActionEvent>() {
  175.             @Override
  176.             public void handle(ActionEvent arg0) {
  177.                 SharedElements.addUserView(adminStage, uio);
  178.                 refresh(uio);
  179.                 mainPane.setContent(userData);
  180.             }
  181.         });
  182.        
  183.         editUserButton.setOnAction(new EventHandler<ActionEvent>() {
  184.             @Override
  185.             public void handle(ActionEvent arg0) {
  186.                 try {
  187.                     SharedElements.editUserView(adminStage, (User) userData.getSelectionModel().getSelectedItem(), uio);
  188.                     refresh(uio);
  189.                     mainPane.setContent(userData);
  190.                 } catch (NullPointerException ex) {
  191.                     Alert al = new Alert(AlertType.ERROR, "No user selected", ButtonType.OK);
  192.                     al.showAndWait();
  193.                 }
  194.             }
  195.         });
  196.        
  197.         statsUserButton.setOnAction(new EventHandler<ActionEvent>() {
  198.             @Override
  199.             public void handle(ActionEvent arg0) {
  200.                 try {
  201.                     User u = (User) userData.getSelectionModel().getSelectedItem();
  202.                     if(u instanceof Cashier) {
  203.                         SharedElements.viewStatistics((Cashier) u);
  204.                     }else {
  205.                         Alert al = new Alert(AlertType.WARNING, "No data is available for that user type", ButtonType.OK);
  206.                         al.showAndWait();
  207.                     }
  208.                 } catch (NullPointerException ex) {
  209.                     Alert al = new Alert(AlertType.ERROR, "No user selected", ButtonType.OK);
  210.                     al.showAndWait();
  211.                 }
  212.             }
  213.         });
  214.        
  215.         removeUserButton.setOnAction(new EventHandler<ActionEvent>() {
  216.             @Override
  217.             public void handle(ActionEvent arg0) {
  218.                 try {
  219.                     User u = (User) userData.getSelectionModel().getSelectedItem();
  220.                     if(u instanceof Admin && uio.getAdminsCount() <= 1) {
  221.                         Alert al = new Alert(AlertType.ERROR, "Cannot delete the only admin", ButtonType.OK);
  222.                         al.show();
  223.                         return;
  224.                     }
  225.                     uio.removeUser(u);
  226.                     refresh(uio);
  227.                     mainPane.setContent(userData);
  228.                 } catch (NullPointerException ex) {
  229.                     Alert al = new Alert(AlertType.ERROR, "No user selected", ButtonType.OK);
  230.                     al.show();
  231.                 }
  232.             }
  233.         });
  234.        
  235.        
  236.         //Adding functions to products related buttons
  237.         addProductButton.setOnAction(new EventHandler<ActionEvent>() {
  238.             @Override
  239.             public void handle(ActionEvent arg0) {
  240.                 SharedElements.addProductView(adminStage, pio);
  241.                 refresh(pio);
  242.                 mainPane.setContent(productData);
  243.             }
  244.         });
  245.        
  246.         editProductButton.setOnAction(new EventHandler<ActionEvent>() {
  247.             @Override
  248.             public void handle(ActionEvent arg0) {
  249.                 try {
  250.                     Product p = (Product) productData.getSelectionModel().getSelectedItem();
  251.                     SharedElements.editProductView(previousStage, pio, p);
  252.                     refresh(pio);
  253.                     mainPane.setContent(productData);
  254.                 } catch (NullPointerException ex) {
  255.                     Alert al = new Alert(AlertType.ERROR, "No product selected", ButtonType.OK);
  256.                     al.show();
  257.                 }
  258.             }
  259.         });
  260.        
  261.         removeProductButton.setOnAction(new EventHandler<ActionEvent>() {
  262.             @Override
  263.             public void handle(ActionEvent arg0) {
  264.                 Product p = (Product) productData.getSelectionModel().getSelectedItem();
  265.                 pio.removeProduct(p);
  266.                 pio.update();
  267.                 refresh(pio);
  268.                 mainPane.setContent(productData);
  269.             }
  270.         });
  271.        
  272.         MenuBar menu = new MenuBar();
  273.         Menu logOut = new Menu();
  274.         Label logOutLabel = new Label("Log Out");
  275.         logOutLabel.setOnMouseClicked(new EventHandler<MouseEvent>() {
  276.             @Override
  277.             public void handle(MouseEvent event) {
  278.                 adminStage.close();
  279.                 LoginStage lgs = new LoginStage();
  280.                 lgs.view(previousStage, uio);
  281.             }
  282.         });
  283.         logOut.setGraphic(logOutLabel);
  284.         menu.getMenus().add(logOut);
  285.        
  286.         topBar.getItems().addAll(usersButton, productsButton, incomeButton, notificationButton, logOutButton);
  287.         VBox top = new VBox(menu, topBar);
  288.         usersButtonTbar.getChildren().addAll(addUserButton, editUserButton, removeUserButton, statsUserButton);
  289.         mainWindow.setTop(top);
  290.         Scene adminScene = new Scene(mainWindow, 1024, 576);
  291.         adminScene.getStylesheets().add("style.css");
  292.         adminStage.setTitle("Admin Window ( " + adm.getName() + " )");
  293.         adminStage.setScene(adminScene);
  294.         mainWindow.requestFocus();
  295.         adminStage.show();
  296.        
  297.         ArrayList<Notification> toRemove = new ArrayList<Notification>();
  298.         for(Notification n : nm.getNotifications()) {
  299.             if(n.getReciever().equals("Administrator")) {
  300.                 n.show(nm, toRemove);
  301.             }
  302.         }
  303.         for(Notification n : toRemove) {
  304.             nm.removeNotification(n);
  305.             nm.update();
  306.         }
  307.     }
  308.    
  309.     private TableView viewUsers(UserIO uio) {
  310.        
  311.         TableView usersTable = new TableView();
  312.         TableColumn<User, Integer> column1 = new TableColumn<>("Id");
  313.         column1.setCellValueFactory(new PropertyValueFactory<>("id"));
  314.        
  315.         TableColumn<User, String> column2 = new TableColumn<>("User Type");
  316.         column2.setCellValueFactory(new PropertyValueFactory<>("usertype"));
  317.        
  318.         TableColumn<User, String> column3 = new TableColumn<>("First Name");
  319.         column3.setCellValueFactory(new PropertyValueFactory<>("name"));
  320.        
  321.         TableColumn<User, String> column4 = new TableColumn<>("Last Name");
  322.         column4.setCellValueFactory(new PropertyValueFactory<>("surname"));
  323.        
  324.         TableColumn<User, String> column5 = new TableColumn<>("Username");
  325.         column5.setCellValueFactory(new PropertyValueFactory<>("username"));
  326.        
  327.         TableColumn<User, String> column6 = new TableColumn<>("Password");
  328.         column6.setCellValueFactory(new PropertyValueFactory<>("password"));
  329.        
  330.         TableColumn<User, String> column7 = new TableColumn<>("Birthday");
  331.         column7.setCellValueFactory(new PropertyValueFactory<>("birthday"));
  332.        
  333.         TableColumn<User, String> column8 = new TableColumn<>("Salary");
  334.         column8.setCellValueFactory(new PropertyValueFactory<>("salary"));
  335.        
  336.         usersTable.setItems(users);
  337.        
  338.         usersTable.getColumns().addAll(column1, column2, column3, column4, column5, column6, column7, column8);
  339.         usersTable.setPlaceholder(new Label("No user data to display"));
  340.         usersTable.setPrefSize(1600, 1200);
  341.        
  342.         return usersTable;
  343.        
  344.     }
  345.    
  346.     private TableView viewProducts(ProductIO uio) {
  347.    
  348.         TableView productsTable = new TableView();
  349.         TableColumn<Product, String> column1 = new TableColumn<>("Name");
  350.         column1.setCellValueFactory(new PropertyValueFactory<>("name"));
  351.        
  352.         TableColumn<Product, String> column2 = new TableColumn<>("Supplier");
  353.         column2.setCellValueFactory(new PropertyValueFactory<>("supplier"));
  354.        
  355.         TableColumn<Product, Integer> column3 = new TableColumn<>("Quantity");
  356.         column3.setCellValueFactory(new PropertyValueFactory<>("quantity"));
  357.        
  358.         TableColumn<Product, Integer> column4 = new TableColumn<>("Product Price");
  359.         column4.setCellValueFactory(new PropertyValueFactory<>("buyingprice"));
  360.        
  361.         TableColumn<Product, Integer> column5 = new TableColumn<>("Selling Price");
  362.         column5.setCellValueFactory(new PropertyValueFactory<>("price"));
  363.        
  364.         TableColumn<Product, Integer> column6 = new TableColumn<>("Barcode");
  365.         column6.setCellValueFactory(new PropertyValueFactory<>("barcode"));
  366.        
  367.         TableColumn<Product, String> column7 = new TableColumn<>("Expire Date");
  368.         column7.setCellValueFactory(new PropertyValueFactory<>("expireDate"));
  369.        
  370.         productsTable.setItems(products);
  371.        
  372.         productsTable.getColumns().addAll(column1, column2, column3, column4, column5, column6, column7);
  373.         productsTable.setPlaceholder(new Label("No products data to display"));
  374.         productsTable.setPrefSize(1600, 1200);
  375.        
  376.         return productsTable;
  377.        
  378.     }
  379.    
  380.     private void refresh(UserIO uio) {
  381.         users.clear();
  382.         for(User u : uio.getUsers()) {
  383.             users.add(u);
  384.         }
  385.         userData = viewUsers(uio);
  386.     }
  387.    
  388.     private void refresh(ProductIO pio) {
  389.         products.clear();
  390.         for(Product p : pio.getProducts()) {
  391.             products.add(p);
  392.         }
  393.         productData = viewProducts(pio);
  394.     }
  395.    
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement