mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
Simplify signature of InventoryMutateForm::of
This complicates the subclasses’ implementation, but it avoids having to pass an unnecessary parameter.
This commit is contained in:
parent
7365b384e3
commit
8ab01fd65f
|
@ -48,7 +48,13 @@ class ConsumableMutateForm extends InventoryMutateForm {
|
||||||
getMetric());
|
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();
|
ConsumableMutateForm form = new ConsumableMutateForm();
|
||||||
form.setName(product.getName());
|
form.setName(product.getName());
|
||||||
form.setQuantity(item.getQuantity().getAmount().longValueExact());
|
form.setQuantity(item.getQuantity().getAmount().longValueExact());
|
||||||
|
|
|
@ -81,7 +81,7 @@ class InventoryController {
|
||||||
@GetMapping("/inventory/edit/{pid}")
|
@GetMapping("/inventory/edit/{pid}")
|
||||||
String edit(Model model, @PathVariable Product pid) {
|
String edit(Model model, @PathVariable Product pid) {
|
||||||
UniqueInventoryItem item = inventory.findByProduct(pid).get();
|
UniqueInventoryItem item = inventory.findByProduct(pid).get();
|
||||||
final InventoryMutateForm form = InventoryMutateForm.of(pid, item);
|
final InventoryMutateForm form = InventoryMutateForm.of(item);
|
||||||
|
|
||||||
return edit(model, form);
|
return edit(model, form);
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,13 +111,14 @@ abstract class InventoryMutateForm {
|
||||||
* @param item an {@link UniqueInventoryItem} holding a {@link Quantity}
|
* @param item an {@link UniqueInventoryItem} holding a {@link Quantity}
|
||||||
* @return an object of an {@link InventoryMutateForm} subclass
|
* @return an object of an {@link InventoryMutateForm} subclass
|
||||||
*/
|
*/
|
||||||
public static InventoryMutateForm of(Product product, UniqueInventoryItem item) {
|
public static InventoryMutateForm of(UniqueInventoryItem item) {
|
||||||
if (product instanceof Consumable consumable) {
|
Product product = item.getProduct();
|
||||||
return ConsumableMutateForm.of(consumable, item);
|
if (product instanceof Consumable) {
|
||||||
} else if (product instanceof Rentable rentable) {
|
return ConsumableMutateForm.of(item);
|
||||||
return RentableMutateForm.of(rentable, item);
|
} else if (product instanceof Rentable) {
|
||||||
|
return RentableMutateForm.of(item);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("InventoryMutateForm::ofProductAndItem not supported for given types");
|
throw new IllegalArgumentException("InventoryMutateForm::of not supported for given types");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,13 @@ class RentableMutateForm extends InventoryMutateForm {
|
||||||
this.wholesalePrice = wholesalePrice;
|
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();
|
RentableMutateForm form = new RentableMutateForm();
|
||||||
form.setName(product.getName());
|
form.setName(product.getName());
|
||||||
form.setQuantity(item.getQuantity().getAmount().longValueExact());
|
form.setQuantity(item.getQuantity().getAmount().longValueExact());
|
||||||
|
|
Loading…
Reference in a new issue