Advertisement
Gaudenz

Garments

May 11th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.50 KB | None | 0 0
  1. package bluejayDB;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. import java.util.List;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JPanel;
  15. import javax.swing.JPasswordField;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.JSpinner;
  18. import javax.swing.JTextArea;
  19. import javax.swing.JTextField;
  20. import javax.swing.SpinnerNumberModel;
  21. import javax.swing.SwingUtilities;
  22.  
  23. class Garment {
  24.     private String name;
  25.     private double priceUSD;
  26.     private String[] sizes;
  27.     private int quantity; // Added to track inventory
  28.     private String checkpointStatus; // Added to track checkpoint status
  29.  
  30.     public Garment(String name, double priceUSD, String[] sizes) {
  31.         this.name = name;
  32.         this.priceUSD = priceUSD;
  33.         this.sizes = sizes;
  34.         this.quantity = 10; // Default quantity for each garment
  35.         this.checkpointStatus = ""; // Default checkpoint status is empty
  36.     }
  37.  
  38.     public String getName() {
  39.         return name;
  40.     }
  41.  
  42.     public double getPriceUSD() {
  43.         return priceUSD;
  44.     }
  45.  
  46.     public String[] getSizes() {
  47.         return sizes;
  48.     }
  49.  
  50.     public int getQuantity() {
  51.         return quantity;
  52.     }
  53.  
  54.     public void setQuantity(int quantity) {
  55.         this.quantity = quantity;
  56.     }
  57.  
  58.     public void setPriceUSD(double priceUSD) {
  59.         this.priceUSD = priceUSD;
  60.     }
  61.  
  62.     public String getCheckpointStatus() {
  63.         return checkpointStatus;
  64.     }
  65.  
  66.     public void setCheckpointStatus(String checkpointStatus) {
  67.         this.checkpointStatus = checkpointStatus;
  68.     }
  69. }
  70.  
  71. class OrderItem {
  72.     private Garment garment;
  73.     private String size;
  74.     private int quantity;
  75.     private double totalPrice;
  76.  
  77.     public OrderItem(Garment garment, String size, int quantity, double totalPrice) {
  78.         this.garment = garment;
  79.         this.size = size;
  80.         this.quantity = quantity;
  81.         this.totalPrice = totalPrice;
  82.     }
  83.  
  84.     public Garment getGarment() {
  85.         return garment;
  86.     }
  87.  
  88.     public String getSize() {
  89.         return size;
  90.     }
  91.  
  92.     public int getQuantity() {
  93.         return quantity;
  94.     }
  95.  
  96.     public double getTotalPrice() {
  97.         return totalPrice;
  98.     }
  99.  
  100.     public void setQuantity(int quantity) {
  101.         this.quantity = quantity;
  102.     }
  103. }
  104.  
  105. public class GarmentsProductionSystem extends JFrame {
  106.     private static final String CUSTOMER_USERNAME = "customer";
  107.     private static final String CUSTOMER_PASSWORD = "password";
  108.     private static final String ADMIN_USERNAME = "admin";
  109.     private static final String ADMIN_PASSWORD = "adminpassword";
  110.     private static final double EXCHANGE_RATE = 50.0; // Exchange rate: 1 USD = 50 PHP
  111.  
  112.     private static final Object[][] INITIAL_GARMENTS = {
  113.             { "Tshirt", 7.0, new String[] { "S", "M", "L" } },
  114.             { "Skirt", 13.0, new String[] { "S", "M", "L" } },
  115.             { "Jacket", 35.0, new String[] { "S", "M", "L" } },
  116.             { "Pants", 20.0, new String[] { "S", "M", "L" } },
  117.             { "Dress", 45.0, new String[] { "S", "M", "L" } },
  118.             { "Jeans", 35.0, new String[] { "S", "M", "L" } },
  119.             { "Blouse", 20.0, new String[] { "S", "M", "L" } },
  120.             { "Coat", 11.0, new String[] { "S", "M", "L" } },
  121.             { "Suit", 20.0, new String[] { "S", "M", "L" } },
  122.             { "Gold Rmore", 100.0, new String[] { "S", "M", "L" } }
  123.     };
  124.  
  125.     private JTextField usernameField;
  126.     private JPasswordField passwordField;
  127.     private JTextArea cartTextArea;
  128.     private JLabel priceCounterLabel;
  129.     private double totalPrice;
  130.  
  131.     private JPanel loginPanel;
  132.     private JPanel customerPanel;
  133.     private JPanel adminPanel;
  134.     private JPanel adminControlPanel;
  135.  
  136.     private final List<Garment> garments;
  137.     private final List<Garment> inventory;
  138.     private final List<OrderItem> orderItems;
  139.  
  140.     public GarmentsProductionSystem() {
  141.         setTitle("Garments Production System");
  142.         setSize(800, 600);
  143.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  144.         setLayout(new BorderLayout());
  145.  
  146.         garments = new ArrayList<>();
  147.         inventory = new ArrayList<>();
  148.         orderItems = new ArrayList<>();
  149.         for (Object[] garmentInfo : INITIAL_GARMENTS) {
  150.             String name = (String) garmentInfo[0];
  151.             double price = (double) garmentInfo[1];
  152.             String[] sizes = (String[]) garmentInfo[2];
  153.             garments.add(new Garment(name, price, sizes));
  154.             inventory.add(new Garment(name, price, sizes));
  155.         }
  156.  
  157.         createLoginWindow();
  158.     }
  159.  
  160.     private void createLoginWindow() {
  161.         loginPanel = new JPanel();
  162.         loginPanel.setLayout(new GridLayout(3, 2));
  163.  
  164.         JLabel usernameLabel = new JLabel("Username:");
  165.         usernameField = new JTextField();
  166.         JLabel passwordLabel = new JLabel("Password:");
  167.         passwordField = new JPasswordField();
  168.         JButton loginButton = new JButton("Login");
  169.  
  170.         loginPanel.add(usernameLabel);
  171.         loginPanel.add(usernameField);
  172.         loginPanel.add(passwordLabel);
  173.         loginPanel.add(passwordField);
  174.         loginPanel.add(new JLabel());
  175.         loginPanel.add(loginButton);
  176.  
  177.         add(loginPanel, BorderLayout.NORTH);
  178.  
  179.         loginButton.addActionListener(this::login);
  180.     }
  181.  
  182.     private void login(ActionEvent e) {
  183.         String username = usernameField.getText();
  184.         String password = String.valueOf(passwordField.getPassword());
  185.  
  186.         if (username.isEmpty() || password.isEmpty()) {
  187.             JOptionPane.showMessageDialog(null, "Please enter both username and password.");
  188.             return;
  189.         }
  190.  
  191.         if (username.equals(CUSTOMER_USERNAME) && password.equals(CUSTOMER_PASSWORD)) {
  192.             JOptionPane.showMessageDialog(null, "Login successful as Customer. You are now logged in.");
  193.             createCustomerWindow();
  194.         } else if (username.equals(ADMIN_USERNAME) && password.equals(ADMIN_PASSWORD)) {
  195.             JOptionPane.showMessageDialog(null, "Login successful as Admin. You are now logged in.");
  196.             createAdminWindow();
  197.         } else {
  198.             JOptionPane.showMessageDialog(null, "Invalid username or password. Access denied.");
  199.         }
  200.     }
  201.  
  202.     private void createCustomerWindow() {
  203.         customerPanel = new JPanel();
  204.         customerPanel.setLayout(new BorderLayout());
  205.  
  206.         cartTextArea = new JTextArea();
  207.         cartTextArea.setEditable(false);
  208.         JScrollPane scrollPane = new JScrollPane(cartTextArea);
  209.         customerPanel.add(scrollPane, BorderLayout.CENTER);
  210.  
  211.         JPanel buttonPanel = new JPanel(new GridLayout(garments.size() + 1, 1));
  212.         for (Garment garment : garments) {
  213.             JButton orderButton = new JButton("Order " + garment.getName() + " (Php"
  214.                     + String.format("%.2f", garment.getPriceUSD() * EXCHANGE_RATE) + ")");
  215.             buttonPanel.add(orderButton);
  216.             orderButton.addActionListener(ev -> {
  217.                 String size = (String) JOptionPane.showInputDialog(null, "Select size for " + garment.getName(),
  218.                         "Size Selection", JOptionPane.QUESTION_MESSAGE, null, garment.getSizes(),
  219.                         garment.getSizes()[0]);
  220.                 if (size != null) {
  221.                     JPanel quantityPanel = new JPanel(new GridLayout(2, 1));
  222.                     JLabel quantityLabel = new JLabel("Available Quantity: " + garment.getQuantity());
  223.                     quantityPanel.add(quantityLabel);
  224.  
  225.                     // Ensure the spinner respects the available quantity
  226.                     int availableQuantity = garment.getQuantity();
  227.                     int spinnerMin = availableQuantity > 0 ? 1 : 0;
  228.                     int spinnerValue = availableQuantity > 0 ? 1 : 0;
  229.                     int spinnerMax = availableQuantity > 0 ? availableQuantity : 0;
  230.  
  231.                     SpinnerNumberModel model = new SpinnerNumberModel(spinnerValue, spinnerMin, spinnerMax, 1);
  232.                     JSpinner quantitySpinner = new JSpinner(model);
  233.                     quantityPanel.add(quantitySpinner);
  234.  
  235.                     int result = JOptionPane.showConfirmDialog(null, quantityPanel,
  236.                             "Enter quantity for " + garment.getName() + " (Size: " + size + "):",
  237.                             JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
  238.  
  239.                     if (result == JOptionPane.OK_OPTION) {
  240.                         int orderedQuantity = (Integer) quantitySpinner.getValue();
  241.  
  242.                         double totalPriceForItem = orderedQuantity * garment.getPriceUSD() * EXCHANGE_RATE;
  243.                         JOptionPane.showMessageDialog(null, "You ordered " + orderedQuantity + " " + garment.getName()
  244.                                 + " (Size: " + size + "). Price: Php" + String.format("%.2f", totalPriceForItem));
  245.                         orderItems.add(new OrderItem(garment, size, orderedQuantity, totalPriceForItem));
  246.                         updateCartTextArea();
  247.                         totalPrice += totalPriceForItem;
  248.                         updatePriceCounter();
  249.                         garment.setQuantity(garment.getQuantity() - orderedQuantity); // Update inventory
  250.                     }
  251.                 }
  252.             });
  253.         }
  254.  
  255.         JButton viewCartButton = new JButton("View Cart");
  256.         viewCartButton.addActionListener(ev -> viewCart());
  257.         buttonPanel.add(viewCartButton);
  258.  
  259.         JButton logoutButton = new JButton("Logout");
  260.         logoutButton.addActionListener(ev -> {
  261.             loginPanel.setVisible(true);
  262.             customerPanel.setVisible(false);
  263.             cartTextArea.setText("");
  264.             totalPrice = 0.0;
  265.             if (priceCounterLabel != null) {
  266.                 priceCounterLabel.setText("Total Price: Php0.00");
  267.             }
  268.         });
  269.         buttonPanel.add(logoutButton);
  270.  
  271.         priceCounterLabel = new JLabel("Total Price: Php0.00");
  272.         customerPanel.add(priceCounterLabel, BorderLayout.SOUTH);
  273.  
  274.         customerPanel.add(buttonPanel, BorderLayout.EAST);
  275.  
  276.         add(customerPanel, BorderLayout.CENTER);
  277.         loginPanel.setVisible(false);
  278.         customerPanel.setVisible(true);
  279.     }
  280.  
  281.     private void removeItemFromCart(int index) {
  282.         if (index >= 0 && index < orderItems.size()) {
  283.             OrderItem item = orderItems.get(index);
  284.             Garment garment = item.getGarment();
  285.             int quantityToAddBack = item.getQuantity();
  286.  
  287.             // Update the garment quantity in the inventory
  288.             garment.setQuantity(garment.getQuantity() + quantityToAddBack);
  289.  
  290.             // Remove the item from the cart
  291.             orderItems.remove(index);
  292.             updateCartTextArea(); // Update the text area after removing the item
  293.             totalPrice -= item.getTotalPrice(); // Adjust the total price
  294.             updatePriceCounter(); // Update the price counter display
  295.             JOptionPane.showMessageDialog(null, "Item removed successfully.");
  296.         } else {
  297.             JOptionPane.showMessageDialog(null, "Invalid item index.");
  298.         }
  299.     }
  300.  
  301.     private void viewCart() {
  302.         if (!orderItems.isEmpty()) {
  303.             StringBuilder cartInfo = new StringBuilder();
  304.             for (int i = 0; i < orderItems.size(); i++) {
  305.                 OrderItem item = orderItems.get(i);
  306.                 cartInfo.append(i + 1).append(". ").append(item.getGarment().getName())
  307.                         .append(" (Size: ").append(item.getSize())
  308.                         .append(", Quantity: ").append(item.getQuantity())
  309.                         .append(", Total Price: Php").append(String.format("%.2f", item.getTotalPrice()))
  310.                         .append(")\n");
  311.             }
  312.             JOptionPane.showMessageDialog(null, cartInfo.toString(), "Cart", JOptionPane.INFORMATION_MESSAGE);
  313.  
  314.             String input = JOptionPane.showInputDialog("Enter the number of the item to remove, or 'cancel' to exit:");
  315.             if (input == null || input.equalsIgnoreCase("cancel")) {
  316.                 return;
  317.             }
  318.             try {
  319.                 int itemNumber = Integer.parseInt(input) - 1; // Convert to zero-based index
  320.                 removeItemFromCart(itemNumber);
  321.             } catch (NumberFormatException e) {
  322.                 JOptionPane.showMessageDialog(null, "Invalid input. Please enter a number.");
  323.             }
  324.         } else {
  325.             JOptionPane.showMessageDialog(null, "Your cart is empty.", "Cart", JOptionPane.INFORMATION_MESSAGE);
  326.         }
  327.     }
  328.  
  329.     private void updateCartTextArea() {
  330.         cartTextArea.setText(""); // Clear the existing text
  331.         for (OrderItem item : orderItems) {
  332.             cartTextArea.append(item.getGarment().getName() + " (Size: " + item.getSize() + "). Price: Php" +
  333.                     String.format("%.2f", item.getTotalPrice()) + " (Quantity: " + item.getQuantity() + ")\n");
  334.         }
  335.     }
  336.  
  337.     private void updatePriceCounter() {
  338.         if (priceCounterLabel != null) {
  339.             priceCounterLabel.setText("Total Price: Php" + String.format("%.2f", totalPrice));
  340.         }
  341.     }
  342.  
  343.     private void createAdminWindow() {
  344.         adminPanel = new JPanel();
  345.         adminPanel.setLayout(new BorderLayout());
  346.  
  347.         adminControlPanel = new JPanel();
  348.         adminControlPanel.setLayout(new GridLayout(5, 1));
  349.  
  350.         JButton addButton = new JButton("Add Product");
  351.         JButton removeButton = new JButton("Remove Product");
  352.         JButton updateButton = new JButton("Update Product");
  353.         JButton checkpointButton = new JButton("Mark Checkpoint");
  354.         JButton viewAuditTrailButton = new JButton("View Audit Trail");
  355.         JButton logoutButton = new JButton("Logout");
  356.  
  357.         addButton.addActionListener(ev -> addProduct());
  358.         removeButton.addActionListener(ev -> removeProduct());
  359.         updateButton.addActionListener(ev -> updateProduct());
  360.         checkpointButton.addActionListener(ev -> markCheckpoint());
  361.         viewAuditTrailButton.addActionListener(ev -> viewAuditTrail());
  362.         logoutButton.addActionListener(ev -> {
  363.             loginPanel.setVisible(true);
  364.             adminPanel.setVisible(false);
  365.         });
  366.  
  367.         adminControlPanel.add(addButton);
  368.         adminControlPanel.add(removeButton);
  369.         adminControlPanel.add(updateButton);
  370.         adminControlPanel.add(checkpointButton);
  371.         adminControlPanel.add(viewAuditTrailButton);
  372.         adminControlPanel.add(logoutButton);
  373.  
  374.         adminPanel.add(adminControlPanel, BorderLayout.CENTER);
  375.  
  376.         add(adminPanel, BorderLayout.CENTER);
  377.         loginPanel.setVisible(false);
  378.         adminPanel.setVisible(true);
  379.     }
  380.  
  381.     private void addProduct() {
  382.         String productName = JOptionPane.showInputDialog("Enter product name:");
  383.         if (productName == null || productName.trim().isEmpty()) {
  384.             JOptionPane.showMessageDialog(null, "Product name cannot be empty.");
  385.             return;
  386.         }
  387.         double priceUSD;
  388.         try {
  389.             priceUSD = Double.parseDouble(JOptionPane.showInputDialog("Enter product price (USD):"));
  390.             if (priceUSD <= 0) {
  391.                 JOptionPane.showMessageDialog(null, "Invalid price. Please enter a positive number.");
  392.                 return;
  393.             }
  394.         } catch (NumberFormatException e) {
  395.             JOptionPane.showMessageDialog(null, "Invalid price format. Please enter a valid number.");
  396.             return;
  397.         }
  398.         inventory.add(new Garment(productName, priceUSD, new String[] { "S", "M", "L" }));
  399.         JOptionPane.showMessageDialog(null, "Product added successfully.");
  400.  
  401.         // Update garments list in customer panel
  402.         garments.add(new Garment(productName, priceUSD, new String[] { "S", "M", "L" }));
  403.     }
  404.  
  405.     private void removeProduct() {
  406.         String productName = JOptionPane.showInputDialog("Enter name of the product to remove:");
  407.         if (productName == null || productName.trim().isEmpty()) {
  408.             JOptionPane.showMessageDialog(null, "Product name cannot be empty.");
  409.             return;
  410.         }
  411.         boolean found = false;
  412.         Iterator<Garment> iterator = inventory.iterator();
  413.         while (iterator.hasNext()) {
  414.             Garment garment = iterator.next();
  415.             if (garment.getName().equals(productName)) {
  416.                 iterator.remove(); // Safely remove the garment from inventory
  417.                 found = true;
  418.                 break;
  419.             }
  420.         }
  421.         if (!found) {
  422.             JOptionPane.showMessageDialog(null, "Product not found.");
  423.             return;
  424.         }
  425.  
  426.         // Update garments list in customer panel
  427.         iterator = garments.iterator();
  428.         while (iterator.hasNext()) {
  429.             Garment garment = iterator.next();
  430.             if (garment.getName().equals(productName)) {
  431.                 iterator.remove(); // Safely remove the garment from garments list
  432.             }
  433.         }
  434.  
  435.         // Update the cart text area to reflect the removal
  436.         updateCartTextArea();
  437.         JOptionPane.showMessageDialog(null, "Product removed successfully.");
  438.     }
  439.  
  440.     private void updateProduct() {
  441.         String productName = JOptionPane.showInputDialog("Enter name of the product to update:");
  442.         if (productName == null || productName.trim().isEmpty()) {
  443.             JOptionPane.showMessageDialog(null, "Product name cannot be empty.");
  444.             return;
  445.         }
  446.         Garment foundGarment = null;
  447.         for (Garment garment : inventory) {
  448.             if (garment.getName().equals(productName)) {
  449.                 foundGarment = garment;
  450.                 break;
  451.             }
  452.         }
  453.         if (foundGarment == null) {
  454.             JOptionPane.showMessageDialog(null, "Product not found.");
  455.             return;
  456.         }
  457.         double newPriceUSD;
  458.         try {
  459.             newPriceUSD = Double
  460.                     .parseDouble(JOptionPane.showInputDialog("Enter new price (USD) for " + productName + ":"));
  461.             if (newPriceUSD <= 0) {
  462.                 JOptionPane.showMessageDialog(null, "Invalid price. Please enter a positive number.");
  463.                 return;
  464.             }
  465.         } catch (NumberFormatException e) {
  466.             JOptionPane.showMessageDialog(null, "Invalid price format. Please enter a valid number.");
  467.             return;
  468.         }
  469.         foundGarment.setPriceUSD(newPriceUSD);
  470.         JOptionPane.showMessageDialog(null, "Product updated successfully.");
  471.  
  472.         // Update garments list in customer panel
  473.         for (Garment garment : garments) {
  474.             if (garment.getName().equals(productName)) {
  475.                 garment.setPriceUSD(newPriceUSD);
  476.                 break;
  477.             }
  478.         }
  479.     }
  480.  
  481.     private void markCheckpoint() {
  482.         String productName = JOptionPane.showInputDialog("Enter name of the garment to mark checkpoint:");
  483.         if (productName == null || productName.trim().isEmpty()) {
  484.             JOptionPane.showMessageDialog(null, "Product name cannot be empty.");
  485.             return;
  486.         }
  487.         boolean found = false;
  488.         for (Garment garment : inventory) {
  489.             if (garment.getName().equals(productName)) {
  490.                 garment.setCheckpointStatus("Checked");
  491.                 JOptionPane.showMessageDialog(null, "Checkpoint marked for " + productName);
  492.                 found = true;
  493.                 break;
  494.             }
  495.         }
  496.         if (!found) {
  497.             JOptionPane.showMessageDialog(null, "Product not found.");
  498.         }
  499.     }
  500.  
  501.     private void viewAuditTrail() {
  502.         StringBuilder auditTrail = new StringBuilder();
  503.         // Iterate through the order items and append audit information
  504.         for (OrderItem item : orderItems) {
  505.             auditTrail.append("Ordered: ").append(item.getGarment().getName())
  506.                     .append(", Size: ").append(item.getSize())
  507.                     .append(", Quantity: ").append(item.getQuantity())
  508.                     .append(", Total Price: Php").append(String.format("%.2f", item.getTotalPrice())).append("\n");
  509.         }
  510.         // Show the audit trail in a dialog
  511.         JOptionPane.showMessageDialog(null, auditTrail.toString(), "Audit Trail", JOptionPane.INFORMATION_MESSAGE);
  512.     }
  513.  
  514.     public static void main(String[] args) {
  515.         SwingUtilities.invokeLater(() -> {
  516.             GarmentsProductionSystem system = new GarmentsProductionSystem();
  517.             system.setVisible(true);
  518.         });
  519.     }
  520. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement