Skip to content

Instantly share code, notes, and snippets.

@hibri
Created May 12, 2022 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hibri/a3a178c2226d896e25f0e06d8dbed504 to your computer and use it in GitHub Desktop.
Save hibri/a3a178c2226d896e25f0e06d8dbed504 to your computer and use it in GitHub Desktop.
Checkout_kata_12-05-2022
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
type item_discount struct {
name rune
quantity int
discount int
}
func GetPrices() map[rune]int {
var prices = make(map[rune]int)
prices['A'] = 50
prices['B'] = 30
return prices
}
func TestAlwaysTrue(t *testing.T) {
assert.True(t, true)
}
func TestEmptyBasketIsZero(t *testing.T) {
var TotalPrice = ScanWithPriceAndDiscount("")
assert.Equal(t, 0, TotalPrice)
}
func TestItemAisFifty(t *testing.T) {
var TotalPrice = ScanWithPriceAndDiscount("A")
assert.Equal(t, 50, TotalPrice)
}
func TestItemAAisOneHundred(t *testing.T) {
var TotalPrice = ScanWithPriceAndDiscount("AA")
assert.Equal(t, 100, TotalPrice)
}
func TestItemBisThirty(t *testing.T) {
var TotalPrice = ScanWithPriceAndDiscount("B")
assert.Equal(t, 30, TotalPrice)
}
func TestItemAAAisOneThirty(t *testing.T) {
var TotalPrice = ScanWithPriceAndDiscount("AAA")
assert.Equal(t, 130, TotalPrice)
}
func TestItemAAAAisOneEighty(t *testing.T) {
var TotalPrice = ScanWithPriceAndDiscount("AAAA")
assert.Equal(t, 180, TotalPrice)
}
func TestItemAAAAAAis260(t *testing.T) {
var TotalPrice = ScanWithPriceAndDiscount("AAAAAA")
assert.Equal(t, 260, TotalPrice)
}
func TestItemBBis45(t *testing.T) {
var TotalPrice = ScanWithPriceAndDiscount("BB")
assert.Equal(t, 45, TotalPrice)
}
func TestItemABisEighty(t *testing.T) {
var TotalPrice = ScanWithPriceAndDiscount("AB")
assert.Equal(t, 80, TotalPrice)
}
func ScanWithPriceAndDiscount(items string) int {
return Scan(items, GetPrices(), GetDiscounts())
}
func Scan(items string, prices map[rune]int, discounts map[rune]item_discount) int {
discount := CalculateDiscount(items, discounts)
total := 0
for _, item := range items {
total += prices[item]
}
return total - discount
}
func GetDiscounts() map[rune]item_discount {
var discounts = make(map[rune]item_discount)
discounts['A'] = item_discount{name: 'A', quantity: 3, discount: 20}
discounts['B'] = item_discount{name: 'B', quantity: 2, discount: 15}
return discounts
}
func CalculateDiscount(items string, discounts map[rune]item_discount) int {
counts := GetCounts(items)
discount := 0
discount = (counts['A'] / discounts['A'].quantity) * discounts['A'].discount
discount += (counts['B'] / discounts['B'].quantity) * discounts['B'].discount
return discount
}
func GetCounts(items string) map[rune]int {
var counts = make(map[rune]int)
for _, item := range items {
counts[item]++
}
return counts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment