Challenge

E-COMMERCE CONSOLE PROJECT

// An e-commerce console project in Java. The classes are organized into packages.

package com.ecommerce.model;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

// Define exceptions

class AuthenticationException extends Exception {

public AuthenticationException(String message) {

super(message);

}

}

class ProductNotFoundException extends Exception {

public ProductNotFoundException(String message) {

super(message);

}

}

class CatalogModificationException extends Exception {

public CatalogModificationException(String message) {

super(message);

}

}

class CartEmptyException extends Exception {

public CartEmptyException(String message) {

super(message);

}

}

// Create a basic Product class

public class Product {

private String name;

private double price;

private String category;

private String subcategory;

public Product(String name, double price, String category, String subcategory) {

this.name = name;

this.price = price;

this.category = category;

this.subcategory = subcategory;

}

// Getters and setters

}

// Create a basic User class

public class User {

private String userId;

private String password;

public User(String userId, String password) {

this.userId = userId;

this.password = password;

}

// Getters and setters

}

// Create a basic Cart class

public class Cart {

private List<Product> cartItems;

public Cart() {

this.cartItems = new ArrayList<>();

}

public void addToCart(Product product) {

cartItems.add(product);

}

public void checkout(String customerId) throws CartEmptyException {

if (cartItems.isEmpty()) {

throw new CartEmptyException("Cannot checkout with an empty cart.");

}

// Create an order and write order details to a file

try (FileWriter writer = new FileWriter(customerId + "_order.txt")) {

for (Product product : cartItems) {

// Write order details to the file

writer.write(product.getName() + "\t" + product.getPrice() + "\n");

}

} catch (IOException e) {

e.printStackTrace(); // Handle the exception properly

}

// Clear the cart after checkout

cartItems.clear();

}

}

// Create a basic Catalog class

public class Catalog {

private Map<String, List<Product>> productCatalog;

public Catalog() {

this.productCatalog = new HashMap<>();

}

public void addProduct(Product product) {

productCatalog.computeIfAbsent(product.getCategory(), k -> new ArrayList<>()).add(product);

}

public List<Product> getProductsByCategory(String category) throws ProductNotFoundException {

if (!productCatalog.containsKey(category)) {

throw new ProductNotFoundException("No products found in the category: " + category);

}

return productCatalog.get(category);

}

}

// Create a basic Store class

public class Store {

private Catalog catalog;

private Cart cart;

private User currentUser;

public Store(Catalog catalog) {

this.catalog = catalog;

this.cart = new Cart();

}

public void viewProductsByCategory(String category) {

try {

List<Product> products = catalog.getProductsByCategory(category);

for (Product product : products) {

System.out.println(product.getName() + "\t" + product.getPrice());

}

} catch (ProductNotFoundException e) {

System.out.println(e.getMessage());

}

}

public void addToCart(Product product) {

cart.addToCart(product);

}

public void checkout() {

try {

cart.checkout(currentUser.getUserId());

System.out.println("Checkout successful. Order details written to file.");

} catch (CartEmptyException e) {

System.out.println(e.getMessage());

}

}

// Example method for initializing the store with dummy data

public void initializeDummyData() {

catalog.addProduct(new Product("Product1", 10.99, "Electronics", "Laptops"));

catalog.addProduct(new Product("Product2", 5.99, "Clothing", "Shirts"));

}

public Catalog getCatalog() {

return catalog;

}

public Cart getCart() {

return cart;

}

public void setCurrentUser(User user) {

this.currentUser = user;

}

}

// Create Admin and Customer classes

class Admin extends User {

public Admin(String userId, String password) {

super(userId, password);

}

}

class Customer extends User {

public Customer(String userId, String password) {

super(userId, password);

}

}

// Create Seller class

class Seller extends User {

public Seller(String userId, String password) {

super(userId, password);

}

public void addProductToCatalog(Product product, Catalog catalog) {

catalog.addProduct(product);

}

}

// Create Administration class

class Administration {

public static void modifyCatalog(Product product, Catalog catalog) throws CatalogModificationException {

try {

catalog.addProduct(product);

} catch (Exception e) {

throw new CatalogModificationException("Error modifying catalog: " + e.getMessage());

}

}

}

// Create ProductService class

class ProductService {

public static Product findProductByName(String productName, Catalog catalog) throws ProductNotFoundException {

List<Product> products = new ArrayList<>();

for (List<Product> productList : catalog.getProductCatalog().values()) {

products.addAll(productList);

}

for (Product product : products) {

if (product.getName().equalsIgnoreCase(productName)) {

return product;

}

}

throw new ProductNotFoundException("Product not found: " + productName);

}

}

// Create OrderService class

class OrderService {

public static void writeOrderToFile(String customerId, List<Product> products) throws IOException {

try (FileWriter writer = new FileWriter(customerId + "_order.txt")) {

for (Product product : products) {

writer.write(product.getName() + "\t" + product.getPrice() + "\n");

}

}

}

}

// Main class

public class ECommerceConsole {

public static void main(String[] args) {

// Create instances of relevant classes

Catalog catalog = new Catalog();

Store store = new Store(catalog);

// Initialize dummy data

store.initializeDummyData();

// Example usage

Scanner scanner = new Scanner(System.in);

// User Authentication

System.out.print("Enter user ID: ");

String userId = scanner.nextLine();

System.out.print("Enter password: ");

String password = scanner.nextLine();

try {

User user = AuthenticationService.authenticateUser(userId, password);

store.setCurrentUser(user);

// View products by category

System.out.print("Do you want to view products by category? (yes/no): ");

String viewProducts = scanner.nextLine().toLowerCase();

if (viewProducts.equals("yes")) {

System.out.print("Enter category: ");

String category = scanner.nextLine();

store.viewProductsByCategory(category);

}

// Add product to cart

System.out.print("Enter product name to add to cart: ");

String productName = scanner.nextLine();

Product product = ProductService.findProductByName(productName, store.getCatalog());

store.addToCart(product);

// Checkout

System.out.print("Do you want to checkout? (yes/no): ");

String checkout = scanner.nextLine().toLowerCase();

if (checkout.equals("yes")) {

store.checkout();

}

} catch (AuthenticationException | ProductNotFoundException | CatalogModificationException |

CartEmptyException | IOException e) {

System.out.println(e.getMessage());

}

}

}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.