Project Report CSE 202
Project Report CSE 202
PROJECT REPORT
Submitted by:
Abdullah Bin Hossain
ID:22201260
Md.Sirajus Salekin
ID:22201268
Md Noushad Jahan Ramim
ID:22201257
Objective: Creating a ShoppingCart PoS system.
Design Diagram:
Customer Class
private int customerID;
private String customerName;
Interface DiscountProvider
Void applyDiscount(double discount)
Interface CustomerRegistration
Void registerCustomer(Customer customer)
Product class
int productid
String productName
double price
Product()
Product(int productid, String productName, double price)
void getPrice()
void getProduct()
void getProductname()
void getProductDetails()
void applyDiscount(double discount)
string toString()
ShoppingCart extends Product implements
DiscountProvider, CustomerRegistration
customer (Customer object)
items (ArrayList<Product>)
ShoppingCart()
void addItem(Product product, int quantity)
void removeItem(Product product)
void clearCart()
arraylist<product> getCartItem()
double getTotalPrice()
string toString()
void applyDiscount(double discount)
void registerCustomer(Customer customer)
purchase()
void applyDiscountToSpecificProduct(int productid,double discount)
Void purchase()
PoS Class
main(String[] args)
IMPLEMENTATION:
The full code of the project is given below with proper commenting:
import javax.swing.*;
import java.util.ArrayList;
//Customer class
class Customer {
private int customerID;
private String customerName;
//constructor of customer
public Customer(int customerID, String customerName) {
this.customerID = customerID;
this.customerName = customerName;
}
//getter for the attribute of customer class
public int getCustomerID() {
return customerID;
}
}
//interface for applying discount
interface DiscountProvider {
void applyDiscount(double discount);
}
//interface for registering customer
interface CustomerRegistration {
void registerCustomer(Customer customer);
}
//Product class.
class Product {
private int productid;
private String productName;
private double price;
}
//Constructor for Product class
public Product(int productid, String productName, double price) {
this.productName = productName;
this.productid = productid;
this.price = price;
}
@Override
public String toString() {
return "Product ID: " + productid + ", Name: " + productName + ", Price: " +
price;
}
}
//ShoppingCart class implementing discountprovider and customerRegistration interface
class ShoppingCart extends Product implements DiscountProvider, CustomerRegistration {
private Customer customer;
private ArrayList<Product> list = new ArrayList<>();
//this method return the list in Arraylist,we used toString method to change the
list
public ArrayList<Product> getCartItem() {
return list;
}
//overriding toString
@Override
public String toString() {
StringBuilder cartDetails = new StringBuilder("Shopping Cart for Customer: ");
if (customer == null) {
cartDetails.append("Not Registered");
} else {
cartDetails.append(customer.getCustomerName());
}
cartDetails.append("\nItems in Cart:\n");
for (Product product : list) {
cartDetails.append(product.getProductDetails()).append("\n");
}
cartDetails.append("Total Price: ").append(getTotalPrice());
return cartDetails.toString();
}
//Purchase method,when putting yes to the yes_no option,it clears the cart
public void purchase() {
if (customer == null) {
JOptionPane.showMessageDialog(null, "No customer registered.", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if (response == JOptionPane.YES_OPTION) {
clearCart();
JOptionPane.showMessageDialog(null, "Purchase successful Cart cleared.",
"Success", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Purchase canceled.", "Info",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
//PoS class.main method in instantiated here
public class PoS {
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart();
while (true) {
try {
String[] options = {
"Register Customer",
"Add Product to Cart",
"Remove Product from Cart",
"Apply Discount to Cart",
"View Cart Details",
"Purchase",
"Exit"
};
switch (choice) {
case 0:
int customerID =
Integer.parseInt(JOptionPane.showInputDialog("Enter Customer ID:"));
String customerName = JOptionPane.showInputDialog("Enter
Customer Name:");
Customer customer = new Customer(customerID, customerName);
cart.registerCustomer(customer);
JOptionPane.showMessageDialog(null, "Customer registered.
Customer name: " + customerName, "Success", JOptionPane.INFORMATION_MESSAGE);
break;
case 1:
int productID =
Integer.parseInt(JOptionPane.showInputDialog("Enter Product ID:"));
String productName = JOptionPane.showInputDialog("Enter
Product Name:");
double productPrice =
Double.parseDouble(JOptionPane.showInputDialog("Enter Product Price:"));
if (productPrice <= 0) {
JOptionPane.showMessageDialog(null, "Product price must be
greater than zero.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
int quantity =
Integer.parseInt(JOptionPane.showInputDialog("Enter Quantity:"));
Product product = new Product(productID, productName,
productPrice);
cart.addItem(product, quantity);
JOptionPane.showMessageDialog(null, "Added " + quantity + " "
+ productName + " to the cart.", "Success", JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
int productIDToRemove =
Integer.parseInt(JOptionPane.showInputDialog("Enter Product ID to Remove:"));
Product productToRemove = null;
for (Product productInCart : cart.getCartItem()) {
if (productInCart.getProductid() == productIDToRemove) {
productToRemove = productInCart;
break;
}
}
if (productToRemove!= null) {
cart.removeItem(productToRemove);
JOptionPane.showMessageDialog(null, "Removed Product ID: "
+ productIDToRemove, "Success", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Product not found.",
"Info", JOptionPane.INFORMATION_MESSAGE);
}
break;
case 3:
int productIDForDiscount =
Integer.parseInt(JOptionPane.showInputDialog("Enter Product ID:"));
double discount =
Double.parseDouble(JOptionPane.showInputDialog("Enter Discount Percentage:"));
cart.applyDiscount(discount);
break;
case 4:
if (cart.getCartItem().isEmpty()) {
options[5] = "View Cart Details (Empty)";
} else {
options[5] = "View Cart Details";
}
JOptionPane.showMessageDialog(null, cart.toString(), "Cart
Details", JOptionPane.INFORMATION_MESSAGE);
break;
case 5:
cart.purchase();
break;
case 6:
JOptionPane.showMessageDialog(null, "Exiting...", "Info",
JOptionPane.INFORMATION_MESSAGE);
return; // Exit
default:
JOptionPane.showMessageDialog(null, "Invalid choice. Try
again.", "Error", JOptionPane.ERROR_MESSAGE);
break;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter valid
numbers.", "Error", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "An unexpected error occurred: " +
e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
Output:
1.after build and running the the code this message is shown.
6.User can remove a product from the cart by clicking the “Remove
Product From Cart” option.after clicking the option these following
output is shown.
In the cart details option we will be able to see,
7.User can apply discount to the cart by clicking the “Apply Discount to
Cart” option.by clicking the option these following output is shown.
User can see the details after discount in the “View Cart Details”
option.
8.User can purchase whetever in the cart by clicking the Purchase
option.By clicking the “Purchase” option,the following output is shown.
9.after purchasing,the user can exit of the program by clicking the exit
button.
ERROR:
If a user makes any error like putting any string instead of numbers in
the id or other option,then the following output will be shown.