Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*; // imports classes in the awt package
- import java.awt.event.*; // imports classes in the awt.event package
- import javax.swing.*; // imports classes in the swing package
- import java.util.*; // imports classes in the java.util package
- public class InventoryCheckoutSystem extends JFrame { // class that extends JFrame
- // Declare constants
- public static final int WIDTH = 400; // constant that defines the width of the window
- public static final int HEIGHT = 500; // constant that defines the height of the window
- // Declare instance variables
- private HashMap<String, Product> inventory; // map that maps serial codes to products
- private ArrayList<Product> cart; // list of products in the cart
- private JLabel serialCodeLabel; // label for serial code text field
- private JTextField serialCodeField; // text field for entering serial code
- private JLabel quantityLabel; // label for quantity text field
- private JTextField quantityField; // text field for entering quantity
- private JButton addButton; // button for adding product to cart
- private JButton checkoutButton; // button for checking out
- private JLabel totalLabel; // label for total cost
- private JLabel totalField; // field for displaying total cost
- private JLabel Products; // field for displaying change due
- private JLabel changeLabel; // label for change due
- private JLabel changeField; // field for displaying change due
- private JLabel amountPaidField; // field for displaying amount paid
- private JScrollPane scrollPane; //field for diplaying scrollbar
- private JTextArea ProductArea; // text area for displaying contents of cart
- public InventoryCheckoutSystem() {
- // Set the title and size of the window
- setTitle("EZTECH Checkout System"); // sets the title of the window
- setSize(WIDTH, HEIGHT); // sets the size of the window
- setVisible(true); // makes the window visible
- // Set the layout to a grid layout
- setLayout(new GridLayout(6, 10)); // sets the layout to a grid layout with 6 rows and 10 columns
- // Initialize the inventory and cart
- inventory = new HashMap<>(); // initializes the inventory as a hash map mapping serial codes to products
- cart = new ArrayList<>(); // initializes the cart as an array list of products
- // Add products to the inventory
- inventory.put("28547", new Product("28547", "IdeaPad Slim 1i (14'', Gen 7)", 19000.00)); // adds a product with serial code "A1" to the inventory
- inventory.put("74575", new Product("74575", "POCO X3 Pro", 14435.00));
- inventory.put("53653", new Product("53653", "iPhone 12 Pro", 46599.00));
- inventory.put("65783", new Product("65783", "Redragon Magic Wand K587 RGB TKL", 2099.00));
- inventory.put("78655", new Product("78655", "Oppo Reno6", 26795.00));
- inventory.put("94754", new Product("94754", "VIVO Y15A", 6999.00));
- inventory.put("66764", new Product("66764", "XIAOMI Pad 5", 20999.00));
- inventory.put("D4", new Product("D5", "Product D", 40.00));
- // Initialize the GUI components
- serialCodeLabel = new JLabel("Enter Product serial code: "); // label for serial code text field
- serialCodeField = new JTextField(20); // text field for entering serial code
- quantityLabel = new JLabel("Enter quantity: "); // label for quantity text field
- quantityField = new JTextField(20); // text field for entering quantity
- addButton = new JButton("Add to cart"); // button for adding product to cart
- totalLabel = new JLabel("Total: "); // label for total cost
- addButton = new JButton("Add to cart"); // button for adding product to cart
- // Initialize the GUI components
- Products = new JLabel("Your Cart: "); // field for displaying the contents of the cart
- totalField = new JLabel(); // field for displaying the total cost of the products in the cart
- checkoutButton = new JButton("Checkout"); // button for checking out
- ProductArea = new JTextArea(); // create a new JTextArea object and assign it to the "ProductArea" instance variable
- ProductArea.setEditable(false); // set the "ProductArea" to be non-editable
- scrollPane = new JScrollPane(ProductArea); // create a new JScrollPane object, which will wrap the "ProductArea" JTextArea, and assign it to the "scrollPane" instance variable
- changeLabel = new JLabel("Change: "); // label for change due
- changeField = new JLabel(); // field for displaying the change due
- amountPaidField = new JLabel(); // field for displaying the amount paid by the user
- // Add the GUI components to the window
- add(serialCodeLabel); // label for the serial code text field
- add(serialCodeField); // text field for entering the serial code of the product
- add(quantityLabel); // label for the quantity text field
- add(quantityField); // text field for entering the quantity of the product
- add(addButton); // button for adding the product to the cart
- add(checkoutButton); // button for checking out
- add(totalLabel); // label for the total cost
- add(totalField); // field for displaying the total cost of the products in the cart
- add(Products); // label for the contents of the cart
- add(scrollPane);// text area for displaying the contents of the cart
- scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
- add(changeLabel); // label for the change due
- add(changeField); // field for displaying the change due
- // Add action listeners to the buttons
- addButton.addActionListener(new AddButtonListener()); // adds an action listener to the "Add to cart" button
- checkoutButton.addActionListener(new CheckoutButtonListener()); // adds an action listener to the "Checkout" button
- }
- private class AddButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- // Get the serial code and quantity from the text fields
- String serialCode = serialCodeField.getText().trim();
- int quantity;
- try {
- quantity = Integer.parseInt(quantityField.getText().trim());
- } catch (NumberFormatException exception) {
- JOptionPane.showMessageDialog(null, "Invalid quantity.");
- return;
- }
- // Get the product with the specified serial code from the inventory
- Product product = inventory.get(serialCode);
- // If the product exists, add it to the cart the specified number of times
- if (product != null) {
- for (int i = 0; i < quantity; i++) {
- cart.add(product);
- }
- // Update the contents of the cart text area
- ProductArea.setText(getCartString());
- } else {
- // If the product does not exist, show an error message
- JOptionPane.showMessageDialog(null, "Product Not Found!");
- }
- // Clear the serial code and quantity text fields
- serialCodeField.setText("");
- quantityField.setText("");
- }
- }
- // Inner class for the action listener of the "Checkout" button
- private class CheckoutButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- // If the cart is empty, show an error message
- if (cart.isEmpty()) {
- JOptionPane.showMessageDialog(null, "Your cart is empty.");
- } else {
- // Calculate the total cost of the products in the cart
- double total = getTotal();
- // Format the total cost as a string with two decimal places
- String totalString = String.format("%.2f", total);
- // Display the total cost
- totalField.setText(totalString);
- // Show a dialog to get the amount paid from the user
- String amountPaidString = JOptionPane.showInputDialog(null, "Enter amount paid:");
- // Convert the input string to a double
- double amountPaid = Double.parseDouble(amountPaidString);
- amountPaidField.setText(String.valueOf(amountPaid));
- if (amountPaid < total) {
- // Show an error message if the amount paid is less than the total
- JOptionPane.showMessageDialog(null, "Amount paid is less than the total.(Please click Checkout Again)");
- } else {
- // Calculate the change due
- double change = amountPaid - total;
- // Format the change due as a string with two decimal places
- String changeString = String.format("%.2f", change);
- // Display the change due
- changeField.setText(String.valueOf(changeString));
- // Show a message dialog with the receipt
- JOptionPane.showMessageDialog(null, getReceiptString());
- // Clear the cart and update the cart text area
- cart.clear();
- ProductArea.setText(getCartString());
- // Clear the total and change due fields
- totalField.setText("");
- changeField.setText("");
- }
- }
- }
- }
- private double getTotal() {
- // Declare variable to store total
- double total = 0;
- // Loop through the products in the cart
- for (Product p : cart) {
- // Add the price of the current product to the total
- total += p.getPrice();
- }
- // Return the total
- return total;
- }
- // Get a string representation of the contents of the cart
- private String getCartString() {
- String s = "";
- // Iterate through the cart and add each unique product to the string
- for (Product p : cart) {
- // Only add the product if it doesn't already exist in the string
- if (!s.contains(p.toString())) {
- // Find the number of times the product occurs in the cart
- int quantity = Collections.frequency(cart, p);
- // Add the product and its quantity to the string
- s += quantity + " x " + p + "\n";
- }
- }
- return s;
- }
- private String getReceiptString() {
- // Generate a random tracking number for the receipt
- Random random = new Random();
- int trackingNumber = random.nextInt();
- // Initialize the string with the tracking number
- String s = "Tracking number: " + trackingNumber + "\n";
- // Loop through the products in the cart
- for (Product p : cart) {
- // If the product's name is not already in the string, add the product and its quantity and price
- if (!s.contains(p.getName())) {
- int quantity = Collections.frequency(cart, p);
- s += quantity + " x " + p.getName() + ": " + p.getPrice() + "\n";
- }
- }
- // Add the total, amount paid, and change due to the string
- s += "Total: " + getTotal() + "\n";
- s += "Amount paid: " + amountPaidField.getText() + "\n";
- s += "Change: " + changeField.getText();
- // Add a thank you message
- s+="\nThank you for Shoping with us";
- // Return the string
- return s;
- }
- // Main method
- public static void main(String[] args) {
- InventoryCheckoutSystem window = new InventoryCheckoutSystem();
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setVisible(true);
- }
- }
- // Product class representing a product with a serial code, name, and price
- class Product {
- private String serialCode; // variable to hold the product serial code
- private String name; // variable to hold the product name
- private double price; // variable to hold the product price
- // constructor method to create a new product instance with a serial code, name, and price
- public Product(String serialCode, String name, double price) {
- this.serialCode = serialCode; // assign the serial code passed in to the serialCode variable
- this.name = name; // assign the name passed in to the name variable
- this.price = price; // assign the price passed in to the price variable
- }
- // method to get the serial code of the product
- public String getSerialCode() {
- return serialCode; // return the serial code of the product
- }
- // method to get the name of the product
- public String getName() {
- return name; // return the name of the product
- }
- // method to get the price of the product
- public double getPrice() {
- return price; // return the price of the product
- }
- // method to return a string representation of the product in the format: "serialCode: name (price)"
- public String toString() {
- return serialCode + ": " + name + " (" + price + ")"; // concatenate the serial code, name, and price with a colon, space, and parentheses to create the string
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment