https://github.com/julianamancera/labassign2_state-pattern
Software Engineering 2 - State Pattern: Laboratory Assignment 2
https://github.com/julianamancera/labassign2_state-pattern
software-engineering state-pattern vending-machine
Last synced: 3 months ago
JSON representation
Software Engineering 2 - State Pattern: Laboratory Assignment 2
- Host: GitHub
- URL: https://github.com/julianamancera/labassign2_state-pattern
- Owner: JulianaMancera
- Created: 2025-02-14T03:10:15.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-02-25T04:11:23.000Z (7 months ago)
- Last Synced: 2025-02-25T04:28:49.567Z (7 months ago)
- Topics: software-engineering, state-pattern, vending-machine
- Language: Java
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LabAssign2_State-Pattern
### Problem:
A vending machine needs to manage different states, including `"Idle"`, `"ItemSelected"`, `"Dispensing"`, and `"OutOfOrder"`. Each state has specific rules and restrictions regarding allowed operations, and the vending machine has associated attributes like item inventory and balance.### Requirements:
**1. Idle State:**
- Allow item selection.
- Disallow dispensing items and inserting coins.
**2. ItemSelected State:**
- Allow inserting coins and dispensing items.
- Disallow item selection
**3. Dispensing State:**
- Allow no operations.
- Automatically transition back to the "Idle" state after dispensing is complete.**4. OutOfOrder State:**
- Disallow all operations.**Current System:** The system currently relies on conditional statements within the VendingMachine class to check the machine state and determine valid actions. This approach becomes cumbersome and error-prone as the number of states and their associated logic grows.
### Implement the State Pattern to improve code maintainability and flexibility:
**1. Define VendingMachine States:**
- Create *separate classes* representing different machine states: `IdleState`, `ItemSelectedState`, `DispensingState`, and `OutOfOrderState`.**2. Implement State Interface:**
- Define an *interface* `VendingMachineState` with methods for common actions like `selectItem`, `insertCoin`, `dispenseItem`, and `setOutOfOrder`.**3. Implement State Behaviors:**
- Each concrete state class implements the `VendingMachineState` interface, providing specific behavior for its respective state. For example, the `IdleState` class would **allow item selection**, while the `OutOfOrderState `**wouldn't allow any operations**.**4. Update VendingMachine Class:**
- Include attributes for item inventory and balance.
- Remove state-specific logic from the `VendingMachine` class.
- Introduce a reference to the current `VendingMachineState` object.
- Delegate actions like `selectItem`, `insertCoin`, `dispenseItem`, and `setOutOfOrder` to the current state object through its corresponding methods.## UML Diagram
