package beer
import (
"fmt"
"strconv"
"strings"
)
func Verse(i int) (string, error) {
if i < 0 {
return "", fmt.Errorf("input must be positive")
}
if i > 99 {
return "", fmt.Errorf("input must be below 99")
}
if i == 0 {
return `No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
`, nil
}
var ret strings.Builder
ret.WriteString(btl(i))
ret.WriteString("of beer on the wall, ")
ret.WriteString(btl(i))
ret.WriteString("of beer.\nTake")
ret.WriteString(one(i))
ret.WriteString("down and pass it around, ")
ret.WriteString(btl(i - 1))
ret.WriteString("of beer on the wall.\n")
return ret.String(), nil
}
func one(i int) string {
if i == 1 {
return " it "
}
return " one "
}
func btl(i int) string {
switch i {
case 0:
return "no more bottles "
case 1:
return strconv.Itoa(i) + " bottle "
default:
return strconv.Itoa(i) + " bottles "
}
}
func Verses(upper, lower int) (string, error) {
if lower > upper {
return "", fmt.Errorf("lower should be lower than upper")
}
var ret strings.Builder
for i := upper; i >= lower; i-- {
v, err := Verse(i)
if err != nil {
return "", err
}
ret.WriteString(v)
ret.WriteByte('\n')
}
return ret.String(), nil
}
func Song() string {
s, _ := Verses(99, 0)
return s
}