From bce61e698aa5c022eb66882c3e618dd3aaf8781b Mon Sep 17 00:00:00 2001 From: FedorovVladimir Date: Sat, 18 Mar 2023 19:02:32 +0700 Subject: [PATCH] update calories --- .vscode/launch.json | 2 +- calories/calories.go | 26 +++++++++++++++++++------- calories/calories_test.go | 9 ++++++++- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index d113d6d..ae6bf4d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "run", "type": "go", "request": "launch", - "mode": "auto", + "mode": "debug", "program": "${workspaceFolder}\\main.go" } ] diff --git a/calories/calories.go b/calories/calories.go index a91bd66..57b8257 100644 --- a/calories/calories.go +++ b/calories/calories.go @@ -6,20 +6,32 @@ import ( ) var ( - caloriesMap = map[string]int{ - "чай": 79, - "яблоко": 100, + productsMap = map[string]struct { + caloriesIn100G int + awgWeightG int + }{ + "чай": {caloriesIn100G: 65, awgWeightG: 200}, + "яблоко": {caloriesIn100G: 52, awgWeightG: 242}, + "хлеб": {caloriesIn100G: 245, awgWeightG: 35}, } ) func CalcCalories(text string) (int, error) { - count, ok := caloriesMap[strings.ToLower(text)] + product, ok := productsMap[strings.ToLower(text)] if ok { - return count, nil + return product.caloriesIn100G * product.awgWeightG / 100, nil } arr := strings.Split(text, " ") 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 { return 0, nil } @@ -27,7 +39,7 @@ func CalcCalories(text string) (int, error) { if err != nil { return 0, nil } - return coun1 * count2 / 100, nil + return count1 * count2 / 100, nil } count, err := strconv.Atoi(text) if err != nil { diff --git a/calories/calories_test.go b/calories/calories_test.go index 45631cd..c5ef165 100644 --- a/calories/calories_test.go +++ b/calories/calories_test.go @@ -34,7 +34,13 @@ func TestCalcCalories(t *testing.T) { { "чай text", "чай", - 79, + 130, + false, + }, + { + "чай 250 text", + "чай 250", + 162, false, }, } @@ -49,6 +55,7 @@ func TestCalcCalories(t *testing.T) { return } if count != tt.want { + fmt.Println(count, tt.want) fmt.Println(count, tt.want) t.Errorf("error count: %v", err) return