Challenge

Project 1

package Project1;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

class InputUtils {

private static Scanner scanner = new Scanner(System.in);

public static String getInput() {

return scanner.nextLine();

}

}

class Cart {

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

public void addProductToCart(Product product) {

products.add(product);

}

public List<Product> getProducts() {

return products;

}

}

class Catalog {

private List<Category> categories = new ArrayList<>();

public void addCategory(String categoryName) {

categories.add(new Category(categoryName));

}

public void addSubcategory(String categoryName, String subcategoryName) {

Category category = getCategoryByName(categoryName);

if (category != null) {

category.addSubcategory(subcategoryName);

}

}

public void displayCategories() {

System.out.println("Categories:");

for (Category category : categories) {

System.out.println(category.getName());

}

}

public void displaySubcategories(String categoryName) {

Category category = getCategoryByName(categoryName);

if (category != null) {

System.out.println("Subcategories of " + category.getName() + ":");

for (String subcategory : category.getSubcategories()) {

System.out.println(subcategory);

}

}

}

public void displayProducts(String categoryName, String subcategoryName) {

Category category = getCategoryByName(categoryName);

if (category != null && category.hasSubcategory(subcategoryName)) {

System.out.println("Products in " + subcategoryName + ":");

for (Product product : category.getProductsBySubcategory(subcategoryName)) {

System.out.println(product.getName() + " - " + product.getDescription() + " - " + product.getPrice());

}

} else {

System.out.println("Invalid category or subcategory.");

}

}

private Category getCategoryByName(String categoryName) {

for (Category category : categories) {

if (category.getName().equals(categoryName)) {

return category;

}

}

return null;

}

}

class Category {

private String name;

private List<String> subcategories = new ArrayList<>();

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

public Category(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void addSubcategory(String subcategory) {

subcategories.add(subcategory);

}

public List<String> getSubcategories() {

return subcategories;

}

public void addProduct(Product product) {

products.add(product);

}

public boolean hasSubcategory(String subcategory) {

return subcategories.contains(subcategory);

}

public List<Product> getProductsBySubcategory(String subcategory) {

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

for (Product product : products) {

if (product.getCategory().equals(subcategory)) {

filteredProducts.add(product);

}

}

return filteredProducts;

}

}

class User {

private String userId;

private String password;

public User(String userId, String password) {

this.userId = userId;

this.password = password;

}

public String getUserId() {

return userId;

}

public String getPassword() {

return password;

}

public boolean authenticate(String userId, String password) {

return this.userId.equals(userId) && this.password.equals(password);

}

}

class Admin extends User {

public Admin(String userId, String password) {

super(userId, password);

}

public void modifyCatalog(Catalog catalog) {

// Logic to modify the catalog

}

}

class Seller extends User {

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

public Seller(String userId, String password) {

super(userId, password);

}

public void addProduct(Product product) {

products.add(product);

}

public List<Product> getProducts() {

return products;

}

}

class Customer extends User {

public Customer(String userId, String password) {

super(userId, password);

}

public String getCustomerId() {

return getUserId();

}

}

class Product {

private String name;

private String description;

private double price;

private String category;

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

this.name = name;

this.description = description;

this.price = price;

this.category = category;

}

public String getName() {

return name;

}

public String getDescription() {

return description;

}

public double getPrice() {

return price;

}

public String getCategory() {

return category;

}

}

class Store {

private List<Seller> sellers = new ArrayList<>();

private List<Order> orders = new ArrayList<>();

private int orderIdCounter = 1;

public void addSeller(Seller seller) {

sellers.add(seller);

}

public void addProductForSeller(Seller seller) {

System.out.println("Seller adding a product...");

// Logic to add a product for a seller

}

public Order createOrder(Customer customer, Cart cart) {

System.out.println("Customer checking out...");

Order order = new Order(orderIdCounter++, customer, cart);

orders.add(order);

return order;

}

}

class Order {

private int orderId;

private Customer customer;

private Cart cart;

public Order(int orderId, Customer customer, Cart cart) {

this.orderId = orderId;

this.customer = customer;

this.cart = cart;

}

public int getOrderId() {

return orderId;

}

public Customer getCustomer() {

return customer;

}

public Cart getCart() {

return cart;

}

@Override

public String toString() {

return "Order ID: " + orderId +

"\nCustomer ID: " + customer.getCustomerId() +

"\nProducts: " + cart.getProducts().toString();

}

}

public class project {

public static void main(String[] args) {

// Dummy data

Catalog catalog = new Catalog();

catalog.addCategory("Electronics");

catalog.addSubcategory("Electronics", "Mobiles");

catalog.addSubcategory("Electronics", "Laptops");

Product product1 = new Product("Mobile1", "Description1", 100.0, "Mobiles");

Product product2 = new Product("Laptop1", "Description2", 500.0, "Laptops");

Seller seller = new Seller("Seller1", null);

seller.addProduct(product1);

seller.addProduct(product2);

Store store = new Store();

store.addSeller(seller);

Customer customer = new Customer("Customer1", null);

// Display products based on categories

System.out.println("Do you want to view products based on categories? (yes/no)");

String choice = InputUtils.getInput().toLowerCase();

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

catalog.displayCategories();

System.out.println("Enter the category name:");

String categoryName = InputUtils.getInput();

catalog.displaySubcategories(categoryName);

System.out.println("Enter the subcategory name:");

String subcategoryName = InputUtils.getInput();

catalog.displayProducts(categoryName, subcategoryName);

}

// Seller adding a product

store.addProductForSeller(seller);

// Customer checking out

Cart cart = new Cart();

cart.addProductToCart(product1);

cart.addProductToCart(product2);

Order order = store.createOrder(customer, cart);

System.out.println("Order created with ID: " + order.getOrderId());

// Write order details to a file

String filename = customer.getCustomerId() + ".txt";

try (FileWriter writer = new FileWriter(filename)) {

writer.write(order.toString());

System.out.println("Order details written to file: " + filename);

} catch (IOException e) {

System.out.println("Failed to write order details to file: " + filename);

e.printStackTrace();

}

}

}

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.