You are going to build a simple Inventory System with the following features:
We give an implemention of the basic classes for the system:
Product
- Represents a product with attributes such as id, name, price, and stockInventory
- Represents the inventory with methods to add, remove, update, and get the total value of productsCreate bad weather test cases for each feature of the Inventory System using JUnit5:
public class Product {
private String id;
private String name;
private int price;
private int stock;
public Product(String id, String name, int price, int stock) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
}
// todo: add Getters and setters
}
import java.util.ArrayList;
public class Inventory {
private ArrayList<Product> products;
public Inventory() {
products = new ArrayList<>();
}
public void addProduct(Product product) {
if (product == null || product.getId() == null || product.getId().isEmpty() || product.getName() == null || product.getName().isEmpty() || product.getPrice() < 0 || product.getStock() < 0) {
throw new IllegalArgumentException("Invalid product.");
}
products.add(product);
}
public void removeProduct(String productId) {
Product productToRemove = products.stream().filter(p -> p.getId().equals(productId)).findFirst().orElse(null);
if (productToRemove == null) {
throw new IllegalArgumentException("Product not found in inventory.");
}
products.remove(productToRemove);
}
public void updateProductStock(String productId, int newStock) {
Product productToUpdate = products.stream().filter(p -> p.getId().equals(productId)).findFirst().orElse(null);
if (productToUpdate == null) {
throw new IllegalArgumentException("Product not found in inventory.");
}
if (newStock < 0) {
throw new IllegalArgumentException("Invalid stock value.");
}
productToUpdate.setStock(newStock);
}
public int getTotalInventoryValue() {
return products.stream().mapToDouble(product -> product.getPrice() * product.getStock()).sum();
}
}