diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java index adb0fd3f5a..9f5e4d8fc4 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java @@ -12,8 +12,14 @@ public class DeliveredState implements PackageState { pkg.setState(new OrderedState()); } + @Override + public void printStatus() { + System.out.println("Package delivered to post office, not received yet."); + } + @Override public String toString() { return "DeliveredState{}"; } + } \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java index 56a267f160..0642c4c73c 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java @@ -12,6 +12,11 @@ public class OrderedState implements PackageState { System.out.println("The package is in it's root state."); } + @Override + public void printStatus() { + System.out.println("Package ordered, not delivered to the office yet."); + } + @Override public String toString() { return "OrderedState{}"; diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java index ff04d543b8..f3dfbb3fa7 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java @@ -19,4 +19,8 @@ public class Package { public void nextState() { state.next(this); } + + public void printStatus() { + state.printStatus(); + } } diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java index d7f7a5c110..d6656c78ac 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java @@ -5,4 +5,6 @@ public interface PackageState { void next(Package pkg); void prev(Package pkg); + + void printStatus(); } \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java index be76937159..de25672e67 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java @@ -16,6 +16,11 @@ public class ReceivedState implements PackageState { pkg.setState(new DeliveredState()); } + @Override + public void printStatus() { + System.out.println("Package was received by client."); + } + @Override public String toString() { return "ReceivedState{}"; diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java new file mode 100644 index 0000000000..1a63ea3ddf --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java @@ -0,0 +1,19 @@ +package com.baeldung.state; + +public class StateDemo { + + public static void main(String[] args) { + + Package pkg = new Package(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + } +}