Last active
August 29, 2015 14:22
-
-
Save Chen-Michael/2106178ae1063770183c to your computer and use it in GitHub Desktop.
Interview questions-Microwave
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Food { | |
| private String name = ""; | |
| private boolean shell = false; | |
| private boolean skin = false; | |
| public Food(String name) { | |
| super(); | |
| setName(name); | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public boolean isShell() { | |
| return shell; | |
| } | |
| public void setShell(boolean shell) { | |
| this.shell = shell; | |
| } | |
| public boolean isSkin() { | |
| return skin; | |
| } | |
| public void setSkin(boolean skin) { | |
| this.skin = skin; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface Heating { | |
| public void start(Food food) throws Exception; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface Microwave { | |
| public boolean open(); | |
| public boolean putFood(Food food); | |
| public Food getFood(); | |
| public boolean close(); | |
| public boolean startHeating(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MicrowaveContext { | |
| private MicrowaveState state = null; | |
| public MicrowaveContext(MicrowaveState state){ | |
| this.state = state; | |
| } | |
| public void setState(MicrowaveState state) { | |
| this.state = state; | |
| } | |
| public void push() { | |
| state.push(this); | |
| } | |
| public void pull(){ | |
| state.pull(this); | |
| } | |
| public boolean canOpen(){ | |
| return state.canOpen(); | |
| } | |
| public boolean canClose(){ | |
| return state.canClose(); | |
| } | |
| public boolean canPutFood(){ | |
| return state.canPutFood(); | |
| } | |
| public boolean canGetFood(){ | |
| return state.canGetFood(); | |
| } | |
| public boolean canHeating(){ | |
| return state.canHeating(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface MicrowaveState { | |
| public void push(MicrowaveContext c); | |
| public void pull(MicrowaveContext c); | |
| public boolean canOpen(); | |
| public boolean canClose(); | |
| public boolean canPutFood(); | |
| public boolean canGetFood(); | |
| public boolean canHeating(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MicrowaveStepFive implements MicrowaveState { | |
| @Override | |
| public void push(MicrowaveContext c) { | |
| c.setState(new MicrowaveStepOne()); | |
| } | |
| @Override | |
| public void pull(MicrowaveContext c) { | |
| c.setState(new MicrowaveStepFour()); | |
| } | |
| @Override | |
| public boolean canOpen() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canClose() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canPutFood() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canGetFood() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canHeating() { | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MicrowaveStepFour implements MicrowaveState { | |
| @Override | |
| public void push(MicrowaveContext c) { | |
| c.setState(new MicrowaveStepFive()); | |
| } | |
| @Override | |
| public void pull(MicrowaveContext c) { | |
| c.setState(new MicrowaveStepThree()); | |
| } | |
| @Override | |
| public boolean canOpen() { | |
| return true; | |
| } | |
| @Override | |
| public boolean canClose() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canPutFood() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canGetFood() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canHeating() { | |
| return true; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MicrowaveStepOne implements MicrowaveState { | |
| @Override | |
| public void push(MicrowaveContext c) { | |
| c.setState(new MicrowaveStepTwo()); | |
| } | |
| @Override | |
| public void pull(MicrowaveContext c) { | |
| c.setState(new MicrowaveStepThree()); | |
| } | |
| @Override | |
| public boolean canOpen() { | |
| return true; | |
| } | |
| @Override | |
| public boolean canClose() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canPutFood() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canGetFood() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canHeating() { | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MicrowaveStepThree implements MicrowaveState { | |
| @Override | |
| public void push(MicrowaveContext c) { | |
| c.setState(new MicrowaveStepFour()); | |
| } | |
| @Override | |
| public void pull(MicrowaveContext c) { | |
| c.setState(new MicrowaveStepTwo()); | |
| } | |
| @Override | |
| public boolean canOpen() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canClose() { | |
| return true; | |
| } | |
| @Override | |
| public boolean canPutFood() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canGetFood() { | |
| return true; | |
| } | |
| @Override | |
| public boolean canHeating() { | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MicrowaveStepTwo implements MicrowaveState { | |
| @Override | |
| public void push(MicrowaveContext c) { | |
| c.setState(new MicrowaveStepThree()); | |
| } | |
| @Override | |
| public void pull(MicrowaveContext c) { | |
| c.setState(new MicrowaveStepOne()); | |
| } | |
| @Override | |
| public boolean canOpen() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canClose() { | |
| return true; | |
| } | |
| @Override | |
| public boolean canPutFood() { | |
| return true; | |
| } | |
| @Override | |
| public boolean canGetFood() { | |
| return false; | |
| } | |
| @Override | |
| public boolean canHeating() { | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NormalHeating implements Heating { | |
| @Override | |
| public void start(Food food) throws Exception { | |
| if (food == null) | |
| throw new Exception("Please put the food in a microwave oven."); | |
| if (food.isShell() || food.isSkin()) | |
| throw new Exception("The food with a shell or skin."); | |
| System.out.println("Heating success."); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NormalMicrowave implements Microwave { | |
| MicrowaveContext context = null; | |
| Heating heating = null; | |
| Food food = null; | |
| public NormalMicrowave() { | |
| super(); | |
| this.heating = new NormalHeating(); | |
| this.context = new MicrowaveContext(new MicrowaveStepOne()); | |
| } | |
| public NormalMicrowave(Heating heating) { | |
| super(); | |
| this.heating = heating; | |
| this.context = new MicrowaveContext(new MicrowaveStepOne()); | |
| } | |
| @Override | |
| public boolean open() { | |
| if (context.canOpen() && this.food != null) { | |
| context.pull(); | |
| return true; | |
| } else if (context.canOpen() && this.food == null) { | |
| context.push(); | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public boolean putFood(Food food) { | |
| if (context.canPutFood()) { | |
| context.push(); | |
| this.food = food; | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public Food getFood() { | |
| if (context.canGetFood()) { | |
| context.pull(); | |
| Food food = this.food; | |
| this.food = null; | |
| return food; | |
| } | |
| return null; | |
| } | |
| @Override | |
| public boolean close() { | |
| if (context.canClose() && this.food != null) { | |
| context.push(); | |
| return true; | |
| } else if (context.canClose() && this.food == null) { | |
| context.pull(); | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public boolean startHeating() { | |
| try { | |
| if (context.canHeating()) { | |
| context.push(); | |
| heating.start(this.food); | |
| context.push(); | |
| }else{ | |
| return false; | |
| } | |
| } catch (Exception e) { | |
| System.out.println(e.getMessage()); | |
| return false; | |
| } | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment