1.4-Software-Development-Principles

Simple Inventory System - Bad Weather Testing

You are going to build a simple Inventory System with the following features:

  1. Add a product to the inventory
  2. Remove a product from the inventory
  3. Update the stock of a product in the inventory
  4. Get the total value of the inventory

We give an implemention of the basic classes for the system:

  1. Product - Represents a product with attributes such as id, name, price, and stock
  2. Inventory - Represents the inventory with methods to add, remove, update, and get the total value of products

assignment

Create 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();
    }
}