/**
* 抽象状态
*/
public abstract class State {
public abstract void WriteProgram(Work w);
}
/**
* 上午和中午状态类
*/
public class ForenoonState extends State {
@Override
public void WriteProgram(Work w) {
if (w.getHout() < 12) {
System.out.println(MessageFormat.format("当前时间:{0}点,上午工作,精神百倍", w.getHout()));
}else {
w.setState(new NoonState());
w.writeProgram();
}
}
}
/**
* 中午工作状态
*/
public class NoonState extends State {
@Override
public void WriteProgram(Work w) {
if (w.getHout() < 13) {
System.out.println(MessageFormat.format("当前时间:{0}点 饿了,午饭:犯困,午休", w.getHout()));
} else {
w.setState(new AfternoonState());
w.writeProgram();
}
}
}
/**
* 下午和傍晚工作状态类
*/
public class AfternoonState extends State {
@Override
public void WriteProgram(Work w) {
if (w.getHout() < 17) {
System.out.println(MessageFormat.format("当前时间:{0} 下午状态还不错,继续努力", w.getHout()));
} else {
w.setState(new EveningState());
w.writeProgram();
}
}
}
/**
* 晚间工作状态
*/
public class EveningState extends State {
@Override
public void WriteProgram(Work w) {
//若完成转为下班状态
if (w.isFinish()) {
w.setState(new RestState());
w.writeProgram();
} else {
if (w.getHout() < 21) {
System.out.println(MessageFormat.format("当前时间:{0}点 加班哦,疲累之极", w.getHout()));
} else {
w.setState(new SleepingState());
w.writeProgram();
}
//若员工必须在20点离开公司,可以取消下面代码注释,
/* if (w.getHout() < 20) {
System.out.println(MessageFormat.format("当前时间:{0}点 加班哦,疲累之极", w.getHout()));
} else {
w.setState(new StopState());
w.writeProgram();
}*/
}
}
}
/**
* 睡眠状态
*/
public class SleepingState extends State {
@Override
public void WriteProgram(Work w) {
System.out.println(MessageFormat.format("当前时间:{0}点 不行了,睡着了", w.getHout()));
}
}
/**
* 下班休息状态类
*/
public class RestState extends State {
@Override
public void WriteProgram(Work w) {
System.out.println(MessageFormat.format("当前时间:{0}点 下班回家了", w.getHout()));
}
}
/**
* 强制下班状态类
*/
public class StopState extends State {
@Override
public void WriteProgram(Work w) {
System.out.println(MessageFormat.format("当前时间:{0}点 员工必须20点前离开公司", w.getHout()));
}
}