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

Project Report CSE 202

Uploaded by

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

Project Report CSE 202

Uploaded by

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

UNIVERSITY OF ASIA PACIFIC

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

PROJECT REPORT

Course Title: Object-Oriented Programming I Lab


Course Code:CSE-202

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;

Customer(int customerID, String customerName)


getCustomerID()
getCustomerName()

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;
}

public String getCustomerName() {


return customerName;
}

}
//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;

//Product default constructor


public Product()
{

}
//Constructor for Product class
public Product(int productid, String productName, double price) {
this.productName = productName;
this.productid = productid;
this.price = price;
}

//getter and setter for the attribute of Product class


public double getPrice() {
return price;
}

public void setPrice(double price) {


this.price = price;
}

public int getProductid() {


return productid;
}

public String getProductName() {


return productName;
}

//getProductDetails method returns a string of the details of price id and name of


the product
public String getProductDetails() {
return "Product ID: " + productid + ", Name: " + productName + ", Price: " +
price;
}

//applydiscount method.it is later used in individual discount applying method


public void applyDiscount(double discount) {
double discountedPrice = this.price - (this.price * (discount / 100));
this.price = discountedPrice;
}

@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<>();

//default constructor of ShoppingCart class.it shows a message when PoS is created


public ShoppingCart() {
JOptionPane.showMessageDialog(null, "welcome to the department store");
}

//this method is used to add product to the Arraylist


public void addItem(Product product, int quantity) {
if (quantity <= 0) {
JOptionPane.showMessageDialog(null, "Quantity must be greater than zero.",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
for (int i = 0; i < quantity; i++) {
list.add(product);
}
}

//this method remove product from Product arraylist


public void removeItem(Product product) {
list.remove(product);
}

//this method clears the list


public void clearCart() {
list.clear();
}

//this method return the list in Arraylist,we used toString method to change the
list
public ArrayList<Product> getCartItem() {
return list;
}

//this method gives the total price of the product


public double getTotalPrice() {
double total = 0;
for (Product product : list) {
total += product.getPrice();
}
return total;
}

//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();
}

//application of DiscountProvider interface


@Override
public void applyDiscount(double discount) {
int productID = Integer.parseInt(JOptionPane.showInputDialog("Enter Product ID
to Apply Discount:"));
applyDiscountToSpecificProduct(productID, discount);
}

//application of RegisterCustomer interface


@Override
public void registerCustomer(Customer customer) {
this.customer = customer;
}

//this method is created to use in the interface


private void applyDiscountToSpecificProduct(int productID, double discount) {
Product product = null;
for (Product item : list) {
if (item.getProductid() == productID) {
product = item;
break;
}
}
if (product!= null) {
product.applyDiscount(discount);
JOptionPane.showMessageDialog(null, "Discount applied to Product ID: " +
productID + ". New price: " + product.getPrice(), "Discount Applied",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Product not found.", "Info",
JOptionPane.INFORMATION_MESSAGE);
}
}

//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;
}

double totalPrice = getTotalPrice();


String message = "Customer: " + customer.getCustomerName() +
"\nTotal Price: " + totalPrice +
"\nDo you want to purchase?";

int response = JOptionPane.showConfirmDialog(null, message, "Purchase",


JOptionPane.YES_NO_OPTION);

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"
};

int choice = JOptionPane.showOptionDialog(


null,
"Select an option",
"Menu",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options,
options[0]
);

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.

2.after clicking the ok button,a few option will be shown:

3.Option 1 is register Customer.A person have to be registered to


purchase.After clicking the Register Customer option these following
option is shown:
4.Option 2 is Add Product to Cart.when clicking the option these
following output is shown:
5.User can see the details of the cart in “view cart details” option.

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.

When clicked yes.

When clicked no.


If the person is not registered before purchasing,this following output
will be 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy