0% found this document useful (0 votes)
29 views

Oop Final Project

This document contains code for a point of sale system written in Java. It includes classes for Products with name and price attributes, a ShoppingCart class to hold items, and a PointOfSaleGUI class with a GUI to display and interact with the shopping cart. The GUI allows adding products to the cart, displays the cart contents, calculates a running total, and supports checkout to clear the cart.

Uploaded by

sheikhanas371
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Oop Final Project

This document contains code for a point of sale system written in Java. It includes classes for Products with name and price attributes, a ShoppingCart class to hold items, and a PointOfSaleGUI class with a GUI to display and interact with the shopping cart. The GUI allows adding products to the cart, displays the cart contents, calculates a running total, and supports checkout to clear the cart.

Uploaded by

sheikhanas371
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Submitted by: Huma Shafique

(FSD-SP-163)
Eeman Asghar
(FSD-sp-161)
Awais Jameel
(Fsd-sp-184)

Department: Software Engineering

Semester: BSSE-2nd

Subject: OOP

Submitted to: Mr. Muhammad Usman


Point of sale system

Code:
package com.example.Product;
import javax.swing.*;
import java.awt.*;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

public class Product {

private String name;


private double price;

public Product(String name, double price) {


this.name = name;
this.price = price;
}

public String getName() {


return name;
}

public double getPrice() {


return price;
}
}

class ShoppingCart {
private List<Product> items;

public ShoppingCart() {
items = new ArrayList<>();
}

public void addItem(Product product) {


items.add(product);
}

public double calculateTotal() {


return items.stream().mapToDouble(Product::getPrice).sum();
}

public List<Product> getItems() {


return items;
}
}

class PointOfSaleGUI extends JFrame {


private ShoppingCart shoppingCart;

private JTextArea cartTextArea;


private JTextField productNameField;
private JFormattedTextField productPriceField;
private JLabel totalLabel;

public PointOfSaleGUI() {
shoppingCart = new ShoppingCart();

setTitle("Point of Sale System");


setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

initializeUI();

setVisible(true);
}

private void initializeUI() {


JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.setBackground(new Color(240, 240, 240));

JPanel inputPanel = new JPanel(new GridBagLayout());


GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.insets = new Insets(10, 10, 10, 10);

JLabel titleLabel = new JLabel("Point of Sale System");


titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
inputPanel.add(titleLabel, gbc);

gbc.gridy++;
gbc.gridwidth = 1;
JLabel nameLabel = new JLabel("Product Name:");
inputPanel.add(nameLabel, gbc);

gbc.gridx++;
productNameField = new JTextField(15);
inputPanel.add(productNameField, gbc);

gbc.gridx = 0;
gbc.gridy++;
JLabel priceLabel = new JLabel("Product Price:");
inputPanel.add(priceLabel, gbc);

gbc.gridx++;
productPriceField = new
JFormattedTextField(NumberFormat.getNumberInstance());
productPriceField.setColumns(10);
inputPanel.add(productPriceField, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = 2;
JButton addButton = new JButton("Add to Cart");
addButton.addActionListener(e -> addProductToCart());
inputPanel.add(addButton, gbc);

gbc.gridy++;
JButton checkoutButton = new JButton("Checkout");
checkoutButton.addActionListener(e -> checkout());
inputPanel.add(checkoutButton, gbc);

panel.add(inputPanel, BorderLayout.NORTH);

cartTextArea = new JTextArea();


cartTextArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(cartTextArea);
panel.add(scrollPane, BorderLayout.CENTER);

JPanel infoPanel = new JPanel(new BorderLayout(10, 10));


infoPanel.setBackground(new Color(240, 240, 240));
JLabel infoLabel = new JLabel("Shopping Cart",
SwingConstants.CENTER);
infoLabel.setFont(new Font("Arial", Font.BOLD, 18));
infoPanel.add(infoLabel, BorderLayout.NORTH);

totalLabel = new JLabel("Total: $0.00", SwingConstants.RIGHT);


infoPanel.add(totalLabel, BorderLayout.SOUTH);
panel.add(infoPanel, BorderLayout.SOUTH);

add(panel);
}

private void addProductToCart() {


String productName = productNameField.getText();
String priceText = productPriceField.getText();

if (isValidInput(productName, priceText)) {
try {
double price = Double.parseDouble(priceText);
Product product = new Product(productName, price);
shoppingCart.addItem(product);
updateCartTextArea();
updateTotalLabel();
} catch (NumberFormatException ex) {
showError("Invalid price format. Please enter a valid
number.");
}
}
clearInputFields();
}

private void updateCartTextArea() {


StringBuilder cartText = new StringBuilder();
for (Product item : shoppingCart.getItems()) {
cartText.append(item.getName()).append(" -
$").append(item.getPrice()).append("\n");
}
cartText.append("\nTotal:
$").append(shoppingCart.calculateTotal());
cartTextArea.setText(cartText.toString());
}

private void updateTotalLabel() {


String totalText = String.format("Total: $%.2f",
shoppingCart.calculateTotal());
totalLabel.setText(totalText);
}

private void clearInputFields() {


productNameField.setText("");
productPriceField.setValue(null);
}

private boolean isValidInput(String productName, String priceText) {


if (productName.isEmpty() || priceText.isEmpty()) {
showError("Please enter both product name and price.");
return false;
}
return true;
}

private void showError(String message) {


JOptionPane.showMessageDialog(this, message, "Error",
JOptionPane.ERROR_MESSAGE);
}

private void checkout() {


if (shoppingCart.getItems().isEmpty()) {
showError("Please add items to the cart before checking
out.");
return;
}

JOptionPane.showMessageDialog(this, "Checkout completed.\nTotal:


$" + shoppingCart.calculateTotal());

shoppingCart = new ShoppingCart();


updateCartTextArea();
updateTotalLabel();
}
class Main{
public static void main(String[] args){
try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}

SwingUtilities.invokeLater(PointOfSaleGUI::new);
}
}
}

Compiler:

You might also like