valera/internal/states/eat_bot_state/calories.go

52 lines
1.1 KiB
Go

package eat_bot_state
import (
"strconv"
"strings"
)
var (
productsMap = map[string]struct {
caloriesIn100G int
awgWeightG int
}{
"чай": {caloriesIn100G: 65, awgWeightG: 200},
"яблоко": {caloriesIn100G: 52, awgWeightG: 242},
"хлеб": {caloriesIn100G: 245, awgWeightG: 35},
"огурец": {caloriesIn100G: 15, awgWeightG: 20},
"помидор": {caloriesIn100G: 18, awgWeightG: 18},
}
)
func calcCalories(text string) (int, error) {
product, ok := productsMap[strings.ToLower(text)]
if ok {
return product.caloriesIn100G * product.awgWeightG / 100, nil
}
arr := strings.Split(text, " ")
if len(arr) == 2 {
product, ok := productsMap[strings.ToLower(arr[0])]
if ok {
count, err := strconv.Atoi(arr[1])
if err != nil {
return 0, nil
}
return product.caloriesIn100G * count / 100, nil
}
count1, err := strconv.Atoi(arr[0])
if err != nil {
return 0, nil
}
count2, err := strconv.Atoi(arr[1])
if err != nil {
return 0, nil
}
return count1 * count2 / 100, nil
}
count, err := strconv.Atoi(text)
if err != nil {
return 0, nil
}
return count, nil
}