diff --git a/src/main/java/catering/inventory/ConsumableMutateForm.java b/src/main/java/catering/inventory/ConsumableMutateForm.java index c41893a..b123661 100644 --- a/src/main/java/catering/inventory/ConsumableMutateForm.java +++ b/src/main/java/catering/inventory/ConsumableMutateForm.java @@ -48,7 +48,13 @@ class ConsumableMutateForm extends InventoryMutateForm { getMetric()); } - public static ConsumableMutateForm of(Consumable product, UniqueInventoryItem item) { + public static ConsumableMutateForm of(UniqueInventoryItem item) { + Consumable product; + if (item.getProduct() instanceof Consumable consumable) { + product = consumable; + } else { + throw new IllegalArgumentException("Item must be for a product of type Consumable"); + } ConsumableMutateForm form = new ConsumableMutateForm(); form.setName(product.getName()); form.setQuantity(item.getQuantity().getAmount().longValueExact()); diff --git a/src/main/java/catering/inventory/InventoryController.java b/src/main/java/catering/inventory/InventoryController.java index 06bee3b..c239f64 100644 --- a/src/main/java/catering/inventory/InventoryController.java +++ b/src/main/java/catering/inventory/InventoryController.java @@ -81,7 +81,7 @@ class InventoryController { @GetMapping("/inventory/edit/{pid}") String edit(Model model, @PathVariable Product pid) { UniqueInventoryItem item = inventory.findByProduct(pid).get(); - final InventoryMutateForm form = InventoryMutateForm.of(pid, item); + final InventoryMutateForm form = InventoryMutateForm.of(item); return edit(model, form); } diff --git a/src/main/java/catering/inventory/InventoryMutateForm.java b/src/main/java/catering/inventory/InventoryMutateForm.java index 5d6d724..a9c8f8e 100644 --- a/src/main/java/catering/inventory/InventoryMutateForm.java +++ b/src/main/java/catering/inventory/InventoryMutateForm.java @@ -111,13 +111,14 @@ abstract class InventoryMutateForm { * @param item an {@link UniqueInventoryItem} holding a {@link Quantity} * @return an object of an {@link InventoryMutateForm} subclass */ - public static InventoryMutateForm of(Product product, UniqueInventoryItem item) { - if (product instanceof Consumable consumable) { - return ConsumableMutateForm.of(consumable, item); - } else if (product instanceof Rentable rentable) { - return RentableMutateForm.of(rentable, item); + public static InventoryMutateForm of(UniqueInventoryItem item) { + Product product = item.getProduct(); + if (product instanceof Consumable) { + return ConsumableMutateForm.of(item); + } else if (product instanceof Rentable) { + return RentableMutateForm.of(item); } else { - throw new IllegalArgumentException("InventoryMutateForm::ofProductAndItem not supported for given types"); + throw new IllegalArgumentException("InventoryMutateForm::of not supported for given types"); } } diff --git a/src/main/java/catering/inventory/RentableMutateForm.java b/src/main/java/catering/inventory/RentableMutateForm.java index 43884fc..88e2682 100644 --- a/src/main/java/catering/inventory/RentableMutateForm.java +++ b/src/main/java/catering/inventory/RentableMutateForm.java @@ -23,7 +23,13 @@ class RentableMutateForm extends InventoryMutateForm { this.wholesalePrice = wholesalePrice; } - public static RentableMutateForm of(Rentable product, UniqueInventoryItem item) { + public static RentableMutateForm of(UniqueInventoryItem item) { + Rentable product; + if (item.getProduct() instanceof Rentable rentable) { + product = rentable; + } else { + throw new IllegalArgumentException("Item must be for a product of type Rentable"); + } RentableMutateForm form = new RentableMutateForm(); form.setName(product.getName()); form.setQuantity(item.getQuantity().getAmount().longValueExact());