Challenge

Project 1

package ecommerce;

public class Administration {
public boolean isAdmin(User user) {
// Check if the user is an admin
return user.getRole().equals("admin");
}

public void modifyCatalog(Catalog catalog, Product product) {
// Modify the catalog
catalog.addProduct(product);
}
}

Store: This class will be responsible for all the store functionalities for seller and administrator.

package ecommerce;

public class Store {
private Order order;

public void createOrder(User user, int orderId) {
// Create an order object and assign this user to the order with an order id
order = new Order(user, orderId);
}

public void checkout(Cart cart) {
// Complete the checkout process
order.setCart(cart);
order.checkout();
}

public void addProduct(Seller seller, Product product) {
// Add a product to the seller's catalog
seller.getCatalog().addProduct(product);
}

public void modifyCatalog(Administration admin, Catalog catalog, Product product) {
// Modify the catalog
if (admin.isAdmin(user)) {
admin.modifyCatalog(catalog, product);
}
}
}

Cart: This class will be taking care of all the cart items and will have the option of checkout as well.

package ecommerce;

public class Cart {
private List<Product> items;

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

public void addItem(Product product) {
// Add an item to the cart
items.add(product);
}

public void removeItem(Product product) {
// Remove an item from the cart
items.remove(product);
}

public void checkout() {
// Complete the checkout process
// Write all the order details to a file with the filename same as the customer id
// ...
}
}

Catalog: This class will be responsible for all the catalog activities.

package ecommerce;

public class Catalog {
private Map<String, List<Product>> products;

public Catalog() {
products = new HashMap<>();
}

public void addProduct(Product product) {
// Add a product to the catalog
String category = product.getCategory();
if (!products.containsKey(category)) {
products.put(category, new ArrayList<>());
}
products.get(category).add(product);
}

public List<Product> getProductsByCategory(String category) {
// Get all the products in a category
return products.get(category);
}
}

Customer: This class will check all the user authentication and will check user Id and Password for all users.

package ecommerce;

public class Customer extends User {
private Cart cart;

public Customer(String id, String password) {
super(id, password, "customer");
cart = new Cart();
}

public void addToCart(Product product) {
// Add a product to the cart
cart.addItem(product);
}

public void removeFromCart(Product product) {
// Remove a product from the cart
cart.removeItem(product);
}

public void checkout(Store store) {
// Complete the checkout process
store.checkout(cart);
}
}

Product: This class will represent a product in the ecommerce store.

package ecommerce;

public class Product {
private String name;
private String category;
private double price;

public Product(String name, String category, double price) {
this.name = name;
this.category = category;
this.price = price;
}

public String getName() {
return name;
}

public String getCategory() {
return category;
}

public double getPrice() {
return price;
}
}

Seller: This class will represent a seller in the ecommerce store.

package ecommerce;

public class Seller extends User {
private Catalog catalog;

public Seller(String id, String password) {
super(id, password, "seller");
catalog = new Catalog();
}

public Catalog getCatalog() {
return catalog;
}
}

User: This class will represent a user in the ecommerce store.

package ecommerce;

public class User {
private String id;
private String password;
private String role;

public User(String id, String password, String role) {
this.id = id;
this.password = password;
this.role = role;
}

public String getId() {
return id;
}

public String getPassword() {
return password;
}

public String getRole() {
return role;
}
}

Add some dummy products data in your code with categories for each product added. Add subcategories to relevant categories. Now, ask the customer to view products based on categories and display those products based on categories in the ecommerce store to the Customer if yes.

// Add some dummy products data
Product p1 = new Product("Product 1", "Category 1", 10.0);
Product p2 = new Product("Product 2", "Category 1", 20.0);
Product p3 = new Product("Product 3", "Category 2", 30.0);
Product p4 = new Product("Product 4", "Category 2", 40.0);

// Add the products to the catalog
Catalog catalog = new Catalog();
catalog.addProduct(p1);
catalog.addProduct(p2);
catalog.addProduct(p3);
catalog.addProduct(p4);

// Ask the customer to view products based on categories
Scanner sc = new Scanner(System.in);
System.out.print("Do you want to view products based on categories? (y/n): ");
String choice = sc.nextLine();
if (choice.equals("y")) {
// Display the products based on categories
System.out.println("Categories:");
for (String category : catalog.getCategories()) {
System.out.println(category);
}
System.out.print("Enter a category: ");
String category = sc.nextLine();
List<Product> products = catalog.getProductsByCategory(category);
for (Product product : products) {
System.out.println(product.getName() + " - " + product.getPrice());
}
}

The seller should be able to add products to different categories as well.

// Create a seller
Seller seller = new Seller("seller1", "password");

// Add a product to the seller's catalog
Product p5 = new Product("Product 5", "Category 3", 50.0);
seller.getCatalog().addProduct(p5);

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.