-
Notifications
You must be signed in to change notification settings - Fork 0
first commit #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
first commit #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,23 @@ | ||
| package actioninfo | ||
|
|
||
| import ( | ||
| ... | ||
| "fmt" | ||
| ) | ||
|
|
||
| // создайте интерфейс DataParser | ||
| ... | ||
| type DataParser interface { | ||
| Parse(datastring string) (err error) | ||
| ActionInfo() (string, error) | ||
| } | ||
|
|
||
| // создайте функцию Info() | ||
| ... | ||
| func Info(dataset []string, dp DataParser) { | ||
| for _, data := range dataset { | ||
| err := dp.Parse(data) | ||
| if err != nil { | ||
| fmt.Println("Ошибка парсинга:", err) | ||
| continue | ||
| } | ||
| fmt.Println(dp.ActionInfo()) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,19 +1,56 @@ | ||||||
| package daysteps | ||||||
|
|
||||||
| import ( | ||||||
| ... | ||||||
| "errors" | ||||||
| "fmt" | ||||||
| "strconv" | ||||||
| "strings" | ||||||
| "time" | ||||||
|
|
||||||
| "github.com/Yandex-Practicum/go1fl-sprint5-final/internal/personaldata" | ||||||
| "github.com/Yandex-Practicum/go1fl-sprint5-final/internal/spentenergy" | ||||||
| ) | ||||||
|
|
||||||
| const ( | ||||||
| StepLength = 0.65 | ||||||
| ) | ||||||
|
|
||||||
| // создайте структуру DaySteps | ||||||
| ... | ||||||
|
|
||||||
| type DaySteps struct { | ||||||
| Steps int | ||||||
| Duration time.Duration | ||||||
| personaldata.Personal | ||||||
| } | ||||||
|
|
||||||
| // создайте метод Parse() | ||||||
| ... | ||||||
| func (ds *DaySteps) Parse(datastring string) (err error) { | ||||||
| parts := strings.Split(datastring, ",") | ||||||
| if len(parts) == 2 { | ||||||
| num, err := strconv.Atoi(parts[0]) | ||||||
| if err != nil { | ||||||
| return errors.New("ошибка конвертации строки в int") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| ds.Steps = num | ||||||
| dur, err := time.ParseDuration(parts[1]) | ||||||
| if err != nil { | ||||||
| return errors.New("ошибка конвертации строки в time.Time") | ||||||
| } | ||||||
| ds.Duration = dur | ||||||
| return nil | ||||||
| } else { | ||||||
| return errors.New("длина parts не равна 2") | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // создайте метод ActionInfo() | ||||||
| ... | ||||||
| func (ds DaySteps) ActionInfo() (string, error) { | ||||||
| if ds.Duration <= 0 { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут нигде не используется в расчетах, так что можно опустить |
||||||
| return "", errors.New("продолжительность прогулки должна быть больше 0") | ||||||
| } | ||||||
| dist := spentenergy.Distance(ds.Steps) | ||||||
| cal, err := spentenergy.WalkingSpentCalories(ds.Steps, ds.Weight, ds.Height, ds.Duration) | ||||||
| if err != nil { | ||||||
| return "", errors.New("ошибка подсчета каллорий") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| return fmt.Sprintf("Количество шагов: %d.\nДистанция составила %.2f км.\nВы сожгли %.2f ккал.", ds.Steps, dist, cal), nil | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,15 @@ | ||
| package personaldata | ||
|
|
||
| import ... | ||
| import "fmt" | ||
|
|
||
| // Ниже создайте структуру Personal | ||
| ... | ||
| type Personal struct { | ||
| Name string | ||
| Weight float64 | ||
| Height float64 | ||
| } | ||
|
|
||
| // Ниже создайте метод Print() | ||
| ... | ||
|
|
||
| func (personal Personal) Print() { | ||
| fmt.Printf("Имя: %s\nВес: %.2f\nРост: %.2f\n", personal.Name, personal.Weight, personal.Height) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,77 @@ | ||
| package trainings | ||
|
|
||
| import ( | ||
| ... | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/Yandex-Practicum/go1fl-sprint5-final/internal/personaldata" | ||
| "github.com/Yandex-Practicum/go1fl-sprint5-final/internal/spentenergy" | ||
| ) | ||
|
|
||
| // создайте структуру Training | ||
| ... | ||
|
|
||
| type Training struct { | ||
| Steps int | ||
| TrainingType string | ||
| Duration time.Duration | ||
| personaldata.Personal | ||
| } | ||
|
|
||
| // создайте метод Parse() | ||
| ... | ||
| func (t *Training) Parse(datastring string) (err error) { | ||
| var num int | ||
| var dur time.Duration | ||
| var act string | ||
| slice := strings.Split(datastring, ",") | ||
| if len(slice) == 3 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Аналогично нужно инвертировать условие |
||
| num, err = strconv.Atoi(slice[0]) | ||
| if err != nil { | ||
| return errors.New("Ошибка конвертации строки в int") | ||
| } | ||
| t.Steps = num | ||
|
|
||
| act = slice[1] | ||
| if act != "Бег" || act != "Ходьба" { | ||
| return errors.New("неизвестный тип тренировки") | ||
| } | ||
| t.TrainingType = act | ||
| dur, err = time.ParseDuration(slice[2]) | ||
| if err != nil { | ||
| return errors.New("Ошибка конвертации строки в time.Time") | ||
| } | ||
| t.Duration = dur | ||
| } else { | ||
| return errors.New("Длина slice не равна 3") | ||
| } | ||
| return nil | ||
|
|
||
| } | ||
|
|
||
| // создайте метод ActionInfo() | ||
| ... | ||
| func (t Training) ActionInfo() (string, error) { | ||
| distance := spentenergy.Distance(t.Steps) | ||
| if t.Duration <= 0 { | ||
| return "", fmt.Errorf("длительность тренировки должна быть больше 0") | ||
| } | ||
| meanSpeed := spentenergy.MeanSpeed(t.Steps, t.Duration) | ||
| if t.TrainingType == "Бег" { | ||
| cal, err := spentenergy.RunningSpentCalories(t.Steps, t.Weight, t.Duration) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return fmt.Sprintf("Тип тренировки: %s\nДлительность: %.2f ч.\nДистанция: %.2f км.\nСкорость: %.2f км/ч\nСожгли калорий: %.2f", t.TrainingType, t.Duration, distance, meanSpeed, cal), nil | ||
|
|
||
| } else if t.TrainingType == "Ходьба" { | ||
| cal, err := spentenergy.WalkingSpentCalories(t.Steps, t.Weight, t.Height, t.Duration) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return fmt.Sprintf("Тип тренировки: %s\nДлительность: %.2f ч.\nДистанция: %.2f км.\nСкорость: %.2f км/ч\nСожгли калорий: %.2f", t.TrainingType, t.Duration, distance, meanSpeed, cal), nil | ||
|
|
||
| } else { | ||
| return "неизвестный тип тренировки", errors.New("unknown training type") | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Большой блок IF и маленький else - плохо!

Можно просто инвертировать условие, чтобы был маленький IF с выходом из функции