Loading...
Determine how these objects will interact with each other to fulfill the use cases...
Design inheritance trees where applicable to promote code reuse and polymorphism. This step involves identifying common attributes and behaviors that can be abstracted into parent classes...
OptimizationStrategy interface to allow flexibility in implementing different storage optimization strategies.InventoryManager, OrderManager, and ShipmentManager to ensure a single instance per warehouse.Order and Shipment objects dynamically.
public class Warehouse {
private String id;
private String name;
private List<StorageLocation> locations;
private InventoryManager inventoryManager;
private OrderManager orderManager;
private ShipmentManager shipmentManager;
public Warehouse(String id, String name) {
this.id = id;
this.name = name;
this.locations = new ArrayList<>();
this.inventoryManager = InventoryManager.getInstance();
this.orderManager = OrderManager.getInstance();
this.shipmentManager = ShipmentManager.getInstance();
}
public void addLocation(StorageLocation location) {
locations.add(location);
}
public List<StorageLocation> getAvailableLocations() {
return locations.stream()
.filter(StorageLocation::isAvailable)
.collect(Collectors.toList());
}
}
public class InventoryManager {
private static InventoryManager instance;
private Map<Item, Integer> inventory;
private InventoryManager() {
inventory = new HashMap<>();
}
public static InventoryManager getInstance() {
if (instance == null) {
instance = new InventoryManager();
}
return instance;
}
public void addStock(Item item, int quantity) {
inventory.put(item, inventory.getOrDefault(item, 0) + quantity);
}
public void removeStock(Item item, int quantity) {
if (!inventory.containsKey(item) || inventory.get(item) < quantity) {
throw new IllegalStateException("Insufficient stock!");
}
inventory.put(item, inventory.get(item) - quantity);
}
public Map<Item, Integer> getStockLevels() {
return inventory;
}
}
public interface OptimizationStrategy {
StorageLocation suggestStorageLocation(Item item);
}
public class SimpleOptimizationStrategy implements OptimizationStrategy {
@Override
public StorageLocation suggestStorageLocation(Item item) {
// Logic to suggest the simplest available storage location
return WarehouseManager.getAvailableLocations()
.stream()
.findFirst()
.orElseThrow(() -> new RuntimeException("No available storage locations"));
}
}
public class OrderManager {
private static OrderManager instance;
private List<Order> orders;
private OrderManager() {
orders = new ArrayList<>();
}
public static OrderManager getInstance() {
if (instance == null) {
instance = new OrderManager();
}
return instance;
}
public Order createOrder(Map<Item, Integer> items) {
Order order = new Order(UUID.randomUUID().toString(), items);
orders.add(order);
return order;
}
public void fulfillOrder(String orderId) {
Order order = orders.stream()
.filter(o -> o.getId().equals(orderId))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Order not found!"));
order.markComplete();
}
}
| Single Responsibility | ✅ Yes | Classes have focused responsibilities, reducing reasons for change. |
| Open/Closed | ✅ Yes | The system supports extension via interfaces and modular components without modifying existing code. |
| Liskov Substitution | ✅ Yes | Interfaces and subclasses can replace base types without breaking functionality. |
| Interface Segregation | ✅ Yes | Interfaces are specific and minimal, ensuring classes only implement relevant methods. |
| Dependency Inversion | ✅ Yes | High-level modules rely on abstractions, with interfaces and dependency injection ensuring decoupling. |
Using inheritance for strategy to extend to different storage as required
Try creating a class, flow, state and/or sequence diagram using the diagramming tool. Mermaid flow diagrams can be used to represent system use cases. You can ask the interviewer bot to create a starter diagram if unfamiliar with the tool. Briefly explain your diagrams if necessary...
Critically examine your design for any flaws or areas for future improvement...