Created
May 27, 2011 21:38
-
-
Save bennyflint/996239 to your computer and use it in GitHub Desktop.
KataPotter in Groovy
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
import org.junit.Test | |
class KataPotter { | |
static float price = 8.0 | |
static Map discounts = [0:0.0, 1:1.0, 2:0.95, 3:0.90, 4:0.80, 5:0.75] | |
def calculateOrderTotal = { Map order -> | |
int numberOfBookSets = order.collect { it.value }.max(); | |
int finalSet = numberOfBookSets - 1 | |
List bookSets = initBookSets(finalSet) | |
order.each { | |
if (it.value == 0) { return } | |
for (currentSet in 0..it.value - 1) { | |
int nextSet = currentSet + 1 | |
if (shouldAddToNextSet(currentSet, finalSet, bookSets)) { | |
bookSets[nextSet] += 1 | |
} else { | |
bookSets[currentSet] += 1 | |
} | |
} | |
} | |
return total(bookSets) | |
} | |
def initBookSets = { | |
return (0..it).collect { 0 } | |
} | |
def shouldAddToNextSet = { int currentSet, int finalSet, List bookSets -> | |
return ((currentSet != finalSet) && isCheaperToPutBookInNextSet(bookSets[currentSet], bookSets[currentSet+1])) | |
} | |
def isCheaperToPutBookInNextSet = { booksInCurrentSet, booksInNextSet -> | |
return total([booksInCurrentSet + 1, booksInNextSet]) > total([booksInCurrentSet, booksInNextSet + 1]) | |
} | |
def total = { | |
return it.inject(0, { total, setSize -> | |
total += setSize * price * discounts[setSize] | |
}) | |
} | |
@Test | |
public void testSimpleDiscounts() { | |
Map order = [1:1, 2:1, 3:0, 4:0, 5:0] | |
assert calculateOrderTotal(order) == 8 * 2 * 0.95 | |
order = [1:1, 2:0, 3:1, 4:0, 5:1] | |
assert calculateOrderTotal(order) == 8 * 3 * 0.9 | |
order = [1:1, 2:1, 3:1, 4:0, 5:1] | |
assert calculateOrderTotal(order) == 8 * 4 * 0.8 | |
order = [1:1, 2:1, 3:1, 4:1, 5:1] | |
assert calculateOrderTotal(order) == 8 * 5 * 0.75 | |
} | |
@Test | |
public void testSeveralDiscounts() { | |
Map order = [1:2, 2:1, 3:0, 4:0, 5:0] | |
assert calculateOrderTotal(order) == 8 + (8 * 2 * 0.95) | |
order = [1:2, 2:2, 3:0, 4:0, 5:0] | |
assert calculateOrderTotal(order) == 2 * (8 * 2 * 0.95) | |
order = [1:2, 2:1, 3:2, 4:1, 5:0] | |
assert calculateOrderTotal(order) == (8 * 4 * 0.8) + (8 * 2 * 0.95) | |
order = [1:1, 2:2, 3:1, 4:1, 5:1] | |
assert calculateOrderTotal(order) == 8 + (8 * 5 * 0.75) | |
} | |
@Test | |
public void testEdgeCases() { | |
Map order = [1:2, 2:2, 3:2, 4:1, 5:1] | |
assert calculateOrderTotal(order) == 2 * (8 * 4 * 0.8) | |
order = [1:5, 2:5, 3:4, 4:5, 5:4] | |
assert calculateOrderTotal(order) == 3 * (8 * 5 * 0.75) + 2 * (8 * 4 * 0.8) | |
order = [1:3, 2:2, 3:2, 4:3, 5:2] | |
assert calculateOrderTotal(order) == 2 * (8 * 5 * 0.75) + 1 * (8 * 2 * 0.95) | |
order = [1:3, 2:3, 3:1, 4:2, 5:1] | |
assert calculateOrderTotal(order) == 2 * (8 * 4 * 0.8) + 1 * (8 * 2 * 0.95) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment