Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions internal/daysteps/daysteps.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
package daysteps

import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/Yandex-Practicum/tracker/internal/spentcalories"
)

const (
// Длина одного шага в метрах
stepLength = 0.65
// Количество метров в одном километре
mInKm = 1000
mInKm = 1000
)

func parsePackage(data string) (int, time.Duration, error) {
// TODO: реализовать функцию
parts := strings.Split(data, ",")
if len(parts) != 2 {
return 0, 0, fmt.Errorf("invalid input format")
}

steps, err := strconv.Atoi(parts[0])
if err != nil || steps <= 0 {
return 0, 0, fmt.Errorf("invalid steps: %v", err)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

При оборачивании нужно использовать %w (wrap), а просто при выводе в консоль %v

Suggested change
return 0, 0, fmt.Errorf("invalid steps: %v", err)
return 0, 0, fmt.Errorf("invalid steps: %w", err)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужно везде поправить

}

duration, err := time.ParseDuration(parts[1])
if err != nil {
return 0, 0, fmt.Errorf("invalid duration: %v", err)
}

if duration <= 0 {
return 0, 0, fmt.Errorf("invalid duration")
}

return steps, duration, nil
}

func DayActionInfo(data string, weight, height float64) string {
// TODO: реализовать функцию
steps, duration, err := parsePackage(data)
if err != nil || steps <= 0 {
log.Println("ошибка:", err.Error())
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно просто

Suggested change
log.Println("ошибка:", err.Error())
log.Println("ошибка:", err)

return ""
}

distance := float64(steps) * stepLength
distanceKm := distance / mInKm

calories, err := spentcalories.WalkingSpentCalories(steps, weight, height, duration)
if err != nil {
log.Println("ошибка:", err.Error())
return ""
}

return fmt.Sprintf("Количество шагов: %d.\nДистанция составила %.2f км.\nВы сожгли %.2f ккал.\n",
steps, distanceKm, calories)
}
94 changes: 79 additions & 15 deletions internal/spentcalories/spentcalories.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,102 @@
package spentcalories

import (
"fmt"
"strconv"
"strings"
"time"
)

// Основные константы, необходимые для расчетов.
const (
lenStep = 0.65 // средняя длина шага.
mInKm = 1000 // количество метров в километре.
minInH = 60 // количество минут в часе.
stepLengthCoefficient = 0.45 // коэффициент для расчета длины шага на основе роста.
walkingCaloriesCoefficient = 0.5 // коэффициент для расчета калорий при ходьбе
lenStep = 0.65
mInKm = 1000
minInH = 60
stepLengthCoefficient = 0.45
walkingCaloriesCoefficient = 0.5
)

func parseTraining(data string) (int, string, time.Duration, error) {
// TODO: реализовать функцию
parts := strings.Split(data, ",")
if len(parts) != 3 {
return 0, "", 0, fmt.Errorf("invalid input format")
}

steps, err := strconv.Atoi(parts[0])
if err != nil {
return 0, "", 0, fmt.Errorf("invalid step count: %v", err)
}

duration, err := time.ParseDuration(parts[2])
if err != nil {
return 0, "", 0, fmt.Errorf("invalid duration: %v", err)
}

if steps <= 0 {
return 0, "", 0, fmt.Errorf("invalid steps: %d", steps)
}
if duration <= 0 {
return 0, "", 0, fmt.Errorf("invalid duration: %s", parts[2])
}

return steps, parts[1], duration, nil
}

func distance(steps int, height float64) float64 {
// TODO: реализовать функцию
stepLen := height * stepLengthCoefficient
return float64(steps) * stepLen / mInKm
}

func meanSpeed(steps int, height float64, duration time.Duration) float64 {
// TODO: реализовать функцию
}

func TrainingInfo(data string, weight, height float64) (string, error) {
// TODO: реализовать функцию
if duration <= 0 {
return 0
}
dist := distance(steps, height)
hours := duration.Hours()
return dist / hours
}

func RunningSpentCalories(steps int, weight, height float64, duration time.Duration) (float64, error) {
// TODO: реализовать функцию
if steps <= 0 || weight <= 0 || height <= 0 || duration <= 0 {
return 0, fmt.Errorf("invalid input values")
}
speed := meanSpeed(steps, height, duration)
minutes := duration.Minutes()
return (weight * speed * minutes) / minInH, nil
}

func WalkingSpentCalories(steps int, weight, height float64, duration time.Duration) (float64, error) {
// TODO: реализовать функцию
if steps <= 0 || weight <= 0 || height <= 0 || duration <= 0 {
return 0, fmt.Errorf("invalid input values")
}
speed := meanSpeed(steps, height, duration)
minutes := duration.Minutes()
cals := (weight * speed * minutes) / minInH
return cals * walkingCaloriesCoefficient, nil
}

func TrainingInfo(data string, weight, height float64) (string, error) {
steps, activity, duration, err := parseTraining(data)
if err != nil {
return "", err
}

dist := distance(steps, height)
speed := meanSpeed(steps, height, duration)
var calories float64

switch activity {
case "Бег":
calories, err = RunningSpentCalories(steps, weight, height, duration)
case "Ходьба":
calories, err = WalkingSpentCalories(steps, weight, height, duration)
default:
return "", fmt.Errorf("неизвестный тип тренировки")
}

if err != nil {
return "", err
}

return fmt.Sprintf("Тип тренировки: %s\nДлительность: %.2f ч.\nДистанция: %.2f км.\nСкорость: %.2f км/ч\nСожгли калорий: %.2f\n",
activity, duration.Hours(), dist, speed, calories), nil
}