BAEL-1997 add additional method to the states

This commit is contained in:
Denis 2018-08-09 21:06:30 +02:00
parent 26a28dd663
commit 7c422c70ad
6 changed files with 41 additions and 0 deletions

View File

@ -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{}";
}
}

View File

@ -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{}";

View File

@ -19,4 +19,8 @@ public class Package {
public void nextState() {
state.next(this);
}
public void printStatus() {
state.printStatus();
}
}

View File

@ -5,4 +5,6 @@ public interface PackageState {
void next(Package pkg);
void prev(Package pkg);
void printStatus();
}

View File

@ -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{}";

View File

@ -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();
}
}