grhey

EZTECH CheckOutSystem

Jan 8th, 2023 (edited)
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.48 KB | None | 0 0
  1. import java.awt.*; // imports classes in the awt package
  2. import java.awt.event.*; // imports classes in the awt.event package
  3. import javax.swing.*; // imports classes in the swing package
  4. import java.util.*; // imports classes in the java.util package
  5.  
  6. public class InventoryCheckoutSystem extends JFrame { // class that extends JFrame
  7. // Declare constants
  8. public static final int WIDTH = 400; // constant that defines the width of the window
  9. public static final int HEIGHT = 500; // constant that defines the height of the window
  10.  
  11. // Declare instance variables
  12. private HashMap<String, Product> inventory; // map that maps serial codes to products
  13. private ArrayList<Product> cart; // list of products in the cart
  14. private JLabel serialCodeLabel; // label for serial code text field
  15. private JTextField serialCodeField; // text field for entering serial code
  16. private JLabel quantityLabel; // label for quantity text field
  17. private JTextField quantityField; // text field for entering quantity
  18. private JButton addButton; // button for adding product to cart
  19. private JButton checkoutButton; // button for checking out
  20. private JLabel totalLabel; // label for total cost
  21. private JLabel totalField; // field for displaying total cost
  22. private JLabel Products; // field for displaying change due
  23. private JLabel changeLabel; // label for change due
  24. private JLabel changeField; // field for displaying change due
  25. private JLabel amountPaidField; // field for displaying amount paid
  26. private JScrollPane scrollPane;   //field for diplaying scrollbar
  27. private JTextArea ProductArea;  // text area for displaying contents of cart
  28.  
  29.  
  30.  public InventoryCheckoutSystem() {
  31. // Set the title and size of the window
  32. setTitle("EZTECH Checkout System"); // sets the title of the window
  33. setSize(WIDTH, HEIGHT); // sets the size of the window
  34. setVisible(true); // makes the window visible
  35.  
  36.  
  37. // Set the layout to a grid layout
  38. setLayout(new GridLayout(6, 10)); // sets the layout to a grid layout with 6 rows and 10 columns
  39. // Initialize the inventory and cart
  40. inventory = new HashMap<>(); // initializes the inventory as a hash map mapping serial codes to products
  41. cart = new ArrayList<>(); // initializes the cart as an array list of products
  42.  
  43. // Add products to the inventory
  44. inventory.put("28547", new Product("28547", "IdeaPad Slim 1i (14'', Gen 7)", 19000.00)); // adds a product with serial code "A1" to the inventory
  45. inventory.put("74575", new Product("74575", "POCO X3 Pro", 14435.00));
  46. inventory.put("53653", new Product("53653", "iPhone 12 Pro", 46599.00));
  47. inventory.put("65783", new Product("65783", "Redragon Magic Wand K587 RGB TKL", 2099.00));
  48. inventory.put("78655", new Product("78655", "Oppo Reno6", 26795.00));
  49. inventory.put("94754", new Product("94754", "VIVO Y15A", 6999.00));
  50. inventory.put("66764", new Product("66764", "XIAOMI Pad 5", 20999.00));
  51. inventory.put("D4", new Product("D5", "Product D", 40.00));
  52.  
  53. // Initialize the GUI components
  54. serialCodeLabel = new JLabel("Enter Product serial code: "); // label for serial code text field
  55. serialCodeField = new JTextField(20); // text field for entering serial code
  56. quantityLabel = new JLabel("Enter quantity: "); // label for quantity text field
  57. quantityField = new JTextField(20); // text field for entering quantity
  58. addButton = new JButton("Add to cart"); // button for adding product to cart
  59. totalLabel = new JLabel("Total: "); // label for total cost
  60. addButton = new JButton("Add to cart"); // button for adding product to cart
  61.  
  62. // Initialize the GUI components
  63. Products = new JLabel("Your Cart: "); // field for displaying the contents of the cart
  64. totalField = new JLabel(); // field for displaying the total cost of the products in the cart
  65. checkoutButton = new JButton("Checkout"); // button for checking out
  66. ProductArea = new JTextArea();  // create a new JTextArea object and assign it to the "ProductArea" instance variable
  67. ProductArea.setEditable(false); // set the "ProductArea" to be non-editable
  68. scrollPane = new JScrollPane(ProductArea);  // create a new JScrollPane object, which will wrap the "ProductArea" JTextArea, and assign it to the "scrollPane" instance variable
  69. changeLabel = new JLabel("Change: "); // label for change due
  70. changeField = new JLabel(); // field for displaying the change due
  71. amountPaidField = new JLabel(); // field for displaying the amount paid by the user
  72.  
  73.  
  74.    // Add the GUI components to the window
  75.  
  76. add(serialCodeLabel); // label for the serial code text field
  77. add(serialCodeField); // text field for entering the serial code of the product
  78. add(quantityLabel); // label for the quantity text field
  79. add(quantityField); // text field for entering the quantity of the product
  80. add(addButton); // button for adding the product to the cart
  81. add(checkoutButton); // button for checking out
  82. add(totalLabel); // label for the total cost
  83. add(totalField); // field for displaying the total cost of the products in the cart
  84. add(Products); // label for the contents of the cart
  85. add(scrollPane);// text area for displaying the contents of the cart
  86. scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  87. add(changeLabel); // label for the change due
  88. add(changeField); // field for displaying the change due
  89.  
  90. // Add action listeners to the buttons
  91. addButton.addActionListener(new AddButtonListener()); // adds an action listener to the "Add to cart" button
  92. checkoutButton.addActionListener(new CheckoutButtonListener()); // adds an action listener to the "Checkout" button
  93.  }
  94.  
  95.  private class AddButtonListener implements ActionListener {
  96. public void actionPerformed(ActionEvent e) {
  97. // Get the serial code and quantity from the text fields
  98. String serialCode = serialCodeField.getText().trim();
  99. int quantity;
  100. try {
  101. quantity = Integer.parseInt(quantityField.getText().trim());
  102. } catch (NumberFormatException exception) {
  103. JOptionPane.showMessageDialog(null, "Invalid quantity.");
  104. return;
  105. }
  106.  // Get the product with the specified serial code from the inventory
  107.   Product product = inventory.get(serialCode);
  108.  
  109.   // If the product exists, add it to the cart the specified number of times
  110.   if (product != null) {
  111.     for (int i = 0; i < quantity; i++) {
  112.       cart.add(product);
  113.     }
  114.     // Update the contents of the cart text area
  115.   ProductArea.setText(getCartString());
  116.   } else {
  117.     // If the product does not exist, show an error message
  118.     JOptionPane.showMessageDialog(null, "Product Not Found!");
  119.   }
  120.  
  121.   // Clear the serial code and quantity text fields
  122.   serialCodeField.setText("");
  123.   quantityField.setText("");
  124. }
  125. }
  126.   // Inner class for the action listener of the "Checkout" button
  127.   private class CheckoutButtonListener implements ActionListener {
  128.     public void actionPerformed(ActionEvent e) {
  129.       // If the cart is empty, show an error message
  130.       if (cart.isEmpty()) {
  131.         JOptionPane.showMessageDialog(null, "Your cart is empty.");
  132.       } else {
  133.         // Calculate the total cost of the products in the cart
  134.         double total = getTotal();
  135.         // Format the total cost as a string with two decimal places
  136.         String totalString = String.format("%.2f", total);
  137.         // Display the total cost
  138.         totalField.setText(totalString);
  139.  
  140.         // Show a dialog to get the amount paid from the user
  141.         String amountPaidString = JOptionPane.showInputDialog(null, "Enter amount paid:");
  142.         // Convert the input string to a double
  143.        double amountPaid = Double.parseDouble(amountPaidString);
  144.         amountPaidField.setText(String.valueOf(amountPaid));
  145.        if (amountPaid < total) {
  146.         // Show an error message if the amount paid is less than the total
  147.         JOptionPane.showMessageDialog(null, "Amount paid is less than the total.(Please click Checkout Again)");
  148.       } else {
  149.         // Calculate the change due
  150.         double change = amountPaid - total;
  151.         // Format the change due as a string with two decimal places
  152.         String changeString  = String.format("%.2f", change);
  153.         // Display the change due
  154.         changeField.setText(String.valueOf(changeString));
  155.  
  156.         // Show a message dialog with the receipt
  157.         JOptionPane.showMessageDialog(null, getReceiptString());
  158.  
  159.         // Clear the cart and update the cart text area
  160.         cart.clear();
  161.         ProductArea.setText(getCartString());
  162.         // Clear the total and change due fields
  163.         totalField.setText("");
  164.         changeField.setText("");
  165.       }
  166.       }
  167.     }
  168.   }
  169.  
  170.  
  171.  
  172.      private double getTotal() {
  173.         // Declare variable to store total
  174.         double total = 0;
  175.         // Loop through the products in the cart
  176.         for (Product p : cart) {
  177.         // Add the price of the current product to the total
  178.         total += p.getPrice();
  179.         }
  180.         // Return the total
  181.         return total;
  182.         }
  183.  
  184.  
  185.         // Get a string representation of the contents of the cart
  186.         private String getCartString() {
  187.         String s = "";
  188.         // Iterate through the cart and add each unique product to the string
  189.         for (Product p : cart) {
  190.         // Only add the product if it doesn't already exist in the string
  191.         if (!s.contains(p.toString())) {
  192.         // Find the number of times the product occurs in the cart
  193.         int quantity = Collections.frequency(cart, p);
  194.         // Add the product and its quantity to the string
  195.         s += quantity + " x " + p + "\n";
  196.         }
  197.         }
  198.         return s;
  199.         }
  200.  
  201.        
  202.         private String getReceiptString() {
  203.           // Generate a random tracking number for the receipt
  204.           Random random = new Random();
  205.           int trackingNumber = random.nextInt();
  206.           // Initialize the string with the tracking number
  207.           String s = "Tracking number: " + trackingNumber + "\n";
  208.           // Loop through the products in the cart
  209.           for (Product p : cart) {
  210.             // If the product's name is not already in the string, add the product and its quantity and price
  211.             if (!s.contains(p.getName())) {
  212.               int quantity = Collections.frequency(cart, p);
  213.               s += quantity + " x " + p.getName() + ": " + p.getPrice() + "\n";
  214.             }
  215.           }
  216.           // Add the total, amount paid, and change due to the string
  217.           s += "Total: " + getTotal() + "\n";
  218.           s += "Amount paid: " + amountPaidField.getText() + "\n";
  219.           s += "Change: " + changeField.getText();
  220.           // Add a thank you message
  221.           s+="\nThank you for Shoping with us";
  222.           // Return the string
  223.           return s;
  224.         }
  225.  
  226.             // Main method
  227.            public static void main(String[] args) {
  228.            InventoryCheckoutSystem window = new InventoryCheckoutSystem();
  229.            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  230.            window.setVisible(true);
  231.            }
  232.            }
  233.  
  234.            // Product class representing a product with a serial code, name, and price
  235.             class Product {
  236.             private String serialCode; // variable to hold the product serial code
  237.             private String name; // variable to hold the product name
  238.             private double price; // variable to hold the product price
  239.  
  240.             // constructor method to create a new product instance with a serial code, name, and price
  241.             public Product(String serialCode, String name, double price) {
  242.                 this.serialCode = serialCode; // assign the serial code passed in to the serialCode variable
  243.                 this.name = name; // assign the name passed in to the name variable
  244.                 this.price = price; // assign the price passed in to the price variable
  245.             }
  246.  
  247.             // method to get the serial code of the product
  248.             public String getSerialCode() {
  249.                 return serialCode; // return the serial code of the product
  250.             }
  251.  
  252.             // method to get the name of the product
  253.             public String getName() {
  254.                 return name; // return the name of the product
  255.             }
  256.  
  257.             // method to get the price of the product
  258.             public double getPrice() {
  259.                 return price; // return the price of the product
  260.             }
  261.  
  262.             // method to return a string representation of the product in the format: "serialCode: name (price)"
  263.             public String toString() {
  264.                 return serialCode + ": " + name + " (" + price + ")"; // concatenate the serial code, name, and price with a colon, space, and parentheses to create the string
  265.             }
  266.         }
Advertisement
Add Comment
Please, Sign In to add comment