Personas: customer looking to purchase a snack, manager responsible for refilling inventory and collecting payment information.
Manager:
Customer:
UserRole
User
VendingMachine
PaymentType
PaymentService
Item
InventoryService
A VendingMachine is composed of a PaymentService and an InventoryService. A VendingMachine performs actions on behalf of Customer users.
A PaymentService encapsulates functionality for payments. It accepts payments from Customers in the form of a PaymentType. A PaymentType knows how to process its payment and relay success or failure messages.
An InventoryService encapsulates creating Items for sale, listing available items for sale, tracking items that are sold and producing gross revenue per item reports.
interface: PaymentType
methods: processPayment();
Implementations: Coin, Cash, Card
VendingMachine is composed of:
Attributes: For each class, define the attributes (data) it will hold...
Methods: Define the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation.
public class VendingMachine {
PaymentService paymentService;
InventoryService inventoryService;
VendingMachine(PaymentService paymentService, InventoryService inventoryService) {
this.paymentService = paymentService;
this.inventoryService = inventoryService;
}
public List<Item> listItems() {
return inventoryService.getItems();
}
public String purchaseItem(String itemId, Double amount, PaymentType paymentType) {
Item item = inventoryService.get(itemId);
if (!validateAmount(amount, item)) {
throw new Error("Insufficent amount to purchase item {itemId}");
}
boolean isSuccess = paymentService.purchase(String itemId, paymentType);
if (isSuccess) {
inventorySystem.decrement(itemId);
}
}
}
Single Responsiblity Principle:
Interface Segregation:
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...