Last active
January 25, 2018 11:47
-
-
Save arturoherrero/79f50d97abd0c20d2f6d66eb25e72f71 to your computer and use it in GitHub Desktop.
Checkout Kata https://www.slideshare.net/chrismdp/lean-code
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
#!/usr/bin/env ruby | |
@products = { | |
"apples" => 100, | |
"mele" => 100, | |
"pommes" => 100, | |
"bananas" => 150, | |
"cherries" => 75, | |
} | |
@cart = [] | |
def total_price | |
@cart.reduce(0) { |total, cart_product| total += @products[cart_product] } | |
end | |
def count_in_cart(product) | |
@cart.reduce(0) do |total, cart_product| | |
total += cart_product == product ? 1 : 0 | |
end | |
end | |
def total_discount | |
discount = @cart.size / 5 | |
discount * 200 | |
end | |
def discount(product, number_of_items_for_discount, price_discounted) | |
number_of_product_in_cart = count_in_cart(product) | |
discounts = number_of_product_in_cart / number_of_items_for_discount | |
discounts * price_discounted | |
end | |
def cherries_discount | |
discount("cherries", 2, 20) | |
end | |
def bananas_discount | |
discount("bananas", 2, 150) | |
end | |
def pommes_discount | |
discount("pommes", 3, 100) | |
end | |
def mele_discount | |
discount("mele", 2, 50) | |
end | |
def apples_discount | |
kind_of_apple = count_in_cart("apples") + count_in_cart("pommes") + count_in_cart("mele") | |
discounts = kind_of_apple / 4 | |
discounts * 100 | |
end | |
def read_input | |
STDIN.gets.chomp.split(",") | |
end | |
while true | |
@cart << read_input | |
@cart.flatten! | |
puts total_price - total_discount - cherries_discount - bananas_discount - pommes_discount - mele_discount - apples_discount | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment