Project 1
Create an ecommerce console project with the following specifications in java
● That ecommerce should have these classes:
Administration, Store, Cart,Customer, Product, Seller, Catalog, User
● Admin class will check whether the user is an admin or normal user if it is admin he/she can modify the catalog.
● User class will check all the user authentication and will check user Id and Password for all users.
● Cart will be taking care of all the cart items and will be having the option of checkout as well.
● Catalog is responsible for all the catalog activities.
● In the Store class-
○ Create an order object and assign this user to the order with an order id at the appropriate place.
○ Complete the logical store functionalities for seller and administrator.
○ Make your code modular- divide it in many different small methods.
● In all the remaining classes-
○ Add default and parameterized constructors.
A. 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.
B. The seller should be able to add products to different categories as well.
C. Divide your project in different packages.
D. Add relevant exception handling to different methods.
E. On checkout by the Customer, create an order and write all the order details to a file with the filename same as the customer id.
Below is an implementation of the described e-commerce console project in. The project is structured into packages to improve modularity, and each class has relevant constructors and functionalities. The code also includes basic exception handling, file writing for order details, and dummy product data for testing.
Project Structure:
Com.ecommerce.admin
Com.ecommerce.store
Com.ecommerce.user
Com.ecommerce.cart
Com.ecommerce.catalog
Code:
1. Admin.java
Package com.ecommerce.admin;
Import com.ecommerce.catalog.Catalog;
Public class Administration {
Public boolean isAdmin(String userId, String password) {
// Dummy admin check
Return userId.equals(“admin”) && password.equals(“admin123”);
}
Public void modifyCatalog(Catalog catalog, String action, String category, String productName) {
Switch (action.toLowerCase()) {
Case “add”:
Catalog.addCategory(category, productName);
Break;
Case “remove”:
Catalog.removeProduct(category, productName);
Break;
Default:
System.out.println(“Invalid action!”);
}
}
}
2. Catalog.java
Package com.ecommerce.catalog;
Import java.util.*;
Public class Catalog {
Private Map<String, List<String>> categories;
Public Catalog() {
Categories = new HashMap<>();
addDummyData();
}
Private void addDummyData() {
Categories.put(“Electronics”, Arrays.asList(“Laptop”, “Smartphone”));
Categories.put(“Clothing”, Arrays.asList(“T-Shirt”, “Jeans”));
Categories.put(“Books”, Arrays.asList(“Fiction”, “Non-fiction”));
}
Public void displayProducts(String category) {
If (categories.containsKey(category)) {
System.out.println(“Products in “ + category + “: “ + categories.get(category));
} else {
System.out.println(“Category not found!”);
}
}
Public void addCategory(String category, String productName) {
Categories.putIfAbsent(category, new ArrayList<>());
Categories.get(category).add(productName);
}
Public void removeProduct(String category, String productName) {
If (categories.containsKey(category)) {
Categories.get(category).remove(productName);
}
}
Public Set<String> getAllCategories() {
Return categories.keySet();
}
}
3. User.java
Package com.ecommerce.user;
Import java.util.HashMap;
Import java.util.Map;
Public class User {
Private Map<String, String> userDatabase;
Public User() {
userDatabase = new HashMap<>();
userDatabase.put(“admin”, “admin123”);
userDatabase.put(“customer1”, “password1”);
}
Public boolean authenticate(String userId, String password) {
Return userDatabase.containsKey(userId) && userDatabase.get(userId).equals(password);
}
}
4. Cart.java
Package com.ecommerce.cart;
Import java.util.*;
Public class Cart {
Private List<String> items;
Public Cart() {
Items = new ArrayList<>();
}
Public void addItem(String item) {
Items.add(item);
}
Public void removeItem(String item) {
Items.remove(item);
}
Public void viewCart() {
System.out.println(“Your cart items: “ + items);
}
Public List<String> checkout() {
System.out.println(“Checking out with items: “ + items);
Return new ArrayList<>(items);
}
}
5. Store.java
Package com.ecommerce.store;
Import com.ecommerce.admin.Administration;
Import com.ecommerce.catalog.Catalog;
Import com.ecommerce.cart.Cart;
Import com.ecommerce.user.User;
Import java.io.FileWriter;
Import java.io.IOException;
Import java.util.Scanner;
Public class Store {
Public static void main(String[] args) {
User user = new User();
Catalog catalog = new Catalog();
Cart cart = new Cart();
Administration admin = new Administration();
Scanner sc = new Scanner(System.in);
System.out.print(“Enter user ID: “);
String userId = sc.nextLine();
System.out.print(“Enter password: “);
String password = sc.nextLine();
If (!user.authenticate(userId, password)) {
System.out.println(“Authentication failed!”);
Return;
}
If (admin.isAdmin(userId, password)) {
System.out.println(“Welcome Admin!”);
System.out.print(“Enter action (add/remove): “);
String action = sc.nextLine();
System.out.print(“Enter category: “);
String category = sc.nextLine();
System.out.print(“Enter product name: “);
String productName = sc.nextLine();
Admin.modifyCatalog(catalog, action, category, productName);
} else {
System.out.println(“Welcome Customer!”);
While (true) {
System.out.println(“\n1. View Categories\n2. View Products\n3. Add to Cart\n4. Checkout\n5. Exit”);
System.out.print(“Choose an option: “);
Int choice = sc.nextInt();
Sc.nextLine(); // Consume newline
Switch (choice) {
Case 1:
System.out.println(“Available Categories: “ + catalog.getAllCategories());
Break;
Case 2:
System.out.print(“Enter category to view products: “);
String cat = sc.nextLine();
Catalog.displayProducts(cat);
Break;
Case 3:
System.out.print(“Enter product name to add to cart: “);
String product = sc.nextLine();
Cart.addItem(product);
Break;
Case 4:
createOrder(userId, cart.checkout());
break;
case 5:
System.out.println(“Exiting…”);
Return;
Default:
System.out.println(“Invalid option!”);
}
}
}
}
Private static void createOrder(String userId, List<String> items) {
String fileName = userId + “_order.txt”;
Try (FileWriter writer = new FileWriter(fileName)) {
Writer.write(“Order Details for “ + userId + “\n”);
Writer.write(“Items: “ + items);
System.out.println(“Order created! Check file: “ + fileName);
} catch (IOException e) {
System.out.println(“Error writing order to file: “ + e.getMessage());
}
}
}
Usage and Testing
Compile and run the Store class.
Log in as admin (admin/admin123) to modify the catalog.
Log in as customer (customer1/password1) to view categories, add items to the cart, and checkout.
Check the generated file for order details.
This project can be extended by adding more functionality like payment systems, user registration, etc.