update calories
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2023-03-18 19:02:32 +07:00
parent 823786c3ea
commit bce61e698a
3 changed files with 28 additions and 9 deletions

2
.vscode/launch.json vendored
View File

@ -8,7 +8,7 @@
"name": "run", "name": "run",
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"mode": "auto", "mode": "debug",
"program": "${workspaceFolder}\\main.go" "program": "${workspaceFolder}\\main.go"
} }
] ]

View File

@ -6,20 +6,32 @@ import (
) )
var ( var (
caloriesMap = map[string]int{ productsMap = map[string]struct {
"чай": 79, caloriesIn100G int
"яблоко": 100, awgWeightG int
}{
"чай": {caloriesIn100G: 65, awgWeightG: 200},
"яблоко": {caloriesIn100G: 52, awgWeightG: 242},
"хлеб": {caloriesIn100G: 245, awgWeightG: 35},
} }
) )
func CalcCalories(text string) (int, error) { func CalcCalories(text string) (int, error) {
count, ok := caloriesMap[strings.ToLower(text)] product, ok := productsMap[strings.ToLower(text)]
if ok { if ok {
return count, nil return product.caloriesIn100G * product.awgWeightG / 100, nil
} }
arr := strings.Split(text, " ") arr := strings.Split(text, " ")
if len(arr) == 2 { if len(arr) == 2 {
coun1, err := strconv.Atoi(arr[0]) 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 { if err != nil {
return 0, nil return 0, nil
} }
@ -27,7 +39,7 @@ func CalcCalories(text string) (int, error) {
if err != nil { if err != nil {
return 0, nil return 0, nil
} }
return coun1 * count2 / 100, nil return count1 * count2 / 100, nil
} }
count, err := strconv.Atoi(text) count, err := strconv.Atoi(text)
if err != nil { if err != nil {

View File

@ -34,7 +34,13 @@ func TestCalcCalories(t *testing.T) {
{ {
"чай text", "чай text",
"чай", "чай",
79, 130,
false,
},
{
"чай 250 text",
"чай 250",
162,
false, false,
}, },
} }
@ -49,6 +55,7 @@ func TestCalcCalories(t *testing.T) {
return return
} }
if count != tt.want { if count != tt.want {
fmt.Println(count, tt.want)
fmt.Println(count, tt.want) fmt.Println(count, tt.want)
t.Errorf("error count: %v", err) t.Errorf("error count: %v", err)
return return