PoS Lab Assignment
PoS Lab Assignment
ID:22201260
SECTION:E(2)
import java.util.ArrayList;
import java.util.Scanner;
class Customer {
private int customerID;
private String customerName;
class Product {
private int productid;
private String productName;
private double price;
interface DiscountProvider {
void applyDiscount(double discountPercent);
}
interface CustomerRegistration {
void registerCustomer(Customer customer);
}
public ShoppingCart() {
super(0, "Cart", 0); // Default Product ID and name for the cart
}
@Override
public void applyDiscount(double discountPercent) {
for (Product product : items) {
double originalPrice = product.getPrice();
double discountedPrice = originalPrice * (1 - (discountPercent / 100));
product.setPrice(discountedPrice);
}
}
@Override
public void registerCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
StringBuilder cartString = new StringBuilder("Shopping Cart for Customer: ");
if (customer == null) {
cartString.append("Not Registered");
} else {
cartString.append(customer.getCustomerName());
}
cartString.append("\nItems in Cart:\n");
for (Product item : items) {
cartString.append(item.getProductInfo()).append("\n");
}
cartString.append("Total Price: ").append(getTotalPrice());
return cartString.toString();
}
}
while (true) {
System.out.println("Menu:");
System.out.println("1. Register Customer");
System.out.println("2. Add Product to Cart");
System.out.println("3. Remove Product from Cart");
System.out.println("4. Apply Discount");
System.out.println("5. View Cart Details");
System.out.println("0. Exit");
switch (choice) {
case 1:
System.out.print("Enter Customer ID: ");
int customerID = scan.nextInt();
scan.nextLine(); // Consume newline
System.out.print("Enter Customer Name: ");
String customerName = scan.nextLine();
Customer customer = new Customer(customerID, customerName);
cart.registerCustomer(customer);
System.out.println("Customer registered: " +
customer.getCustomerName());
break;
case 2:
System.out.print("Enter Product ID: ");
int productID = scan.nextInt();
scan.nextLine(); // Consume newline
System.out.print("Enter Product Name: ");
String productName = scan.nextLine();
System.out.print("Enter Product Price: ");
double productPrice = scan.nextDouble();
System.out.print("Enter Quantity: ");
int quantity = scan.nextInt();
Product product = new Product(productID, productName,
productPrice);
cart.addItem(product, quantity);
System.out.println("Added " + quantity + " x " + productName + "
to the cart.");
break;
case 3:
System.out.print("Enter Product ID to remove: ");
int idToRemove = scan.nextInt();
Product productToRemove = null;
for (Product item : cart.getCartItems()) {
if (item.getProductid() == idToRemove) {
productToRemove = item;
break;
}
}
if (productToRemove != null) {
cart.removeItem(productToRemove);
System.out.println("Removed Product ID: " + idToRemove);
} else {
System.out.println("Product not found.");
}
break;
case 4:
System.out.print("Enter Discount Percentage: ");
double discountPercent = scan.nextDouble();
cart.applyDiscount(discountPercent);
System.out.println("Applied a " + discountPercent + "%
discount.");
break;
case 5:
System.out.println("Cart Details:");
System.out.println(cart);
break;
case 0:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
if (choice == 0) {
break;
}
}
scan.close();
}
}
OUTPUT: