Challenge

Project 1

public class User {
private String userId;
private String password;

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

// getters and setters
}

public class Customer extends User {
private Cart cart;

public Customer(String userId, String password) {
super(userId, password);
this.cart = new Cart();
}

// getters and setters
}

public class Seller extends User {
public Seller(String userId, String password) {
super(userId, password);
}

// methods for adding products to catalog
}

public class Administration extends User {
public Administration(String userId, String password) {
super(userId, password);
}

// methods for modifying catalog
}

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

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

// getters and setters
}

public class Catalog {
private List<Product> products;

public Catalog() {
this.products = new ArrayList<>();
}

public void addProduct(Product product) {
this.products.add(product);
}

public List<Product> getProductsByCategory(String category) {
return this.products.stream()
.filter(product -> product.getCategory().equals(category))
.collect(Collectors.toList());
}
}

public class Cart {
private List<Product> items;

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

public void addItem(Product item) {
this.items.add(item);
}

public void removeItem(Product item) {
this.items.remove(item);
}

public List<Product> getItems() {
return this.items;
}

public double getTotalPrice() {
return this.items.stream()
.mapToDouble(Product::getPrice)
.sum();
}
}

public class Order {
private String orderId;
private Customer customer;
private List<Product> items;
private double totalPrice;

public Order(String orderId, Customer customer, List<Product> items, double totalPrice) {
this.orderId = orderId;
this.customer = customer;
this.items = items;
this.totalPrice = totalPrice;
}

// getters and setters
}

public class Store {
private Catalog catalog;
private List<Order> orders;

public Store() {
this.catalog = new Catalog();
this.orders = new ArrayList<>();
}

public void addProduct(Product product) {
this.catalog.addProduct(product);
}

public void placeOrder(Customer customer) {
List<Product> items = customer.getCart().getItems();
double totalPrice = customer.getCart().getTotalPrice();
String orderId = UUID.randomUUID().toString();
Order order = new Order(orderId, customer, items, totalPrice);
this.orders.add(order);
customer.getCart().getItems().clear();
// write order details to file with customer id as filename
}

// other methods for seller and administrator functionality
}

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.