Created
March 18, 2013 18:00
-
-
Save chexxor/5189328 to your computer and use it in GitHub Desktop.
An algorithm for our company's dishwasher. Because people need instructions.
This file contains 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 Dishwasher { | |
public enum StatusCode { READY_TO_WASH, WASHING, READY_TO_EMPTY } | |
public enum Reason { OK, FULL, MUST_REMOVE_DISHES, MUST_EXECUTE_WASH_CYCLE } | |
public Integer capacity {get; set;} // 0 - 100 | |
public StatusCode status {get; set;} | |
public hasSoap {get; set;} | |
public Dishwasher() { | |
this.capacity = 0; | |
} | |
public String addDish(Dish dirtyDish) { | |
Reason addDishMessage = Reason.OK; | |
// Can we add a dish? | |
Reason canAddMessage = this.canAdd(dirtyDish); | |
if (canAddMessage != Reason.OK) { | |
addDishMessage = canAddMessage; | |
return addDishMessage; | |
} | |
// We can add a dish, so add it. | |
this.capacity += dirtyDish.getSizeCode(); | |
// Are we full? | |
if (this.capacity >= 97) | |
addDishMessage = Reason.FULL; | |
return addDishMessage; | |
} | |
public Reason canAdd(Dish dirtyDish) { | |
Reason resultReason = Reason.OK; | |
if (this.status == StatusCode.WASHING) | |
resultReason = Reason.IS_WASHING; | |
if (this.status == StatusCode.READY_TO_EMPTY) | |
resultReason = Reason.MUST_REMOVE_DISHES; | |
if (resultReason != Reason.OK) | |
return resultReason; | |
if (this.capacity + dirtyDish.getSizeCode() > 100) | |
resultReason = Reason.MUST_EXECUTE_WASH_CYCLE; | |
return resultReason; | |
} | |
public Boolean executeWashCycle() { | |
if (this.hasSoap == false) | |
return false; | |
this.status = StatusCode.WASHING; | |
// * Washing dishes * | |
this.status = StatusCode.READY_TO_EMPTY; | |
return true; | |
} | |
} | |
public class Dish { | |
public enum SizeCode { S, M, L } | |
public SizeCode sizeCode {get; set;} | |
public Dish(SizeCode sizeCode) { | |
this.sizeCode = sizeCode' | |
} | |
public Integer getSize() { | |
if (this.sizeCode == SizeCode.S) | |
return 1; | |
else if (this.sizeCode == SizeCode.M) | |
return 2; | |
else if (this.sizeCode == SizeCode.L) | |
return 3; | |
} | |
} | |
public class Sink { | |
public addDish() { | |
throw new Exception('Sink is for rinsing. Please add dish to a Dishwasher.'); | |
} | |
} | |
public class TestDishwasherInteraction { | |
public void testMethod testDirtyDish_AddToSink_ShouldError() { | |
// Create test data | |
Dish dirtyCup = new Dish(SizeCode.S); | |
Sink fourthFloorSink = new Sink(); | |
// Can we add a dirty dish to the sink? | |
String sinkError = ''; | |
try { | |
// Invoke functionality | |
fourthFloorSink.addDish(dirtyCup); | |
} | |
catch (Exception e) { | |
sinkError = e.getMessage(); | |
} | |
// Check results | |
System.assert(sinkError.contains('add dish to a Dishwasher')); // No! | |
} | |
public void testMethod testDirtyDish_AddToEmptyDishwasher_ShouldAccept() { | |
// Create test data | |
Dish dirtyCup = new Dish(SizeCode.S); | |
Dishwasher fourthFloorDishwasher = new Dishwasher(); | |
// Can we add a dirty dish to an empty dishwasher? | |
// Invoke functionality | |
Dishwasher.Reason addMessage = fourthFloorDishwasher.addDish(dirtyCup); | |
// Check results | |
System.assertEquals(Reason.OK, addMessage); // Yes! | |
} | |
public void testMethod testDirtyDish_AddToFullDishwasher_ShouldFail() { | |
// Create test data | |
Dish dirtyCup = new Dish(SizeCode.S); | |
Dishwasher fourthFloorDishwasher = new Dishwasher(); | |
fourthFloorDishwasher.capacity = 100; | |
// Can we add a dirty dish to a full dishwasher? | |
// Invoke functionality | |
Dishwasher.Reason addMessage = fourthFloorDishwasher.addDish(dirtyCup); | |
// Check results | |
System.assertEquals(Dishwasher.Reason.MUST_EXECUTE_WASH_CYCLE, addMessage); | |
} | |
public void testMethod testDirtyDish_HasSoap_RunDishwasher_ShouldComplete() { | |
//Create test data | |
Dishwasher fourthFloorDishwasher = new Dishwasher(); | |
fourthFloorDishwasher.hasSoap = true; | |
//Invoke functionality | |
Boolean isCycleComplete = fourthFloorDishwasher.executeWashCycle(); | |
// Check results | |
System.assertEquals(true, isCycleComplete); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment