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
81 changes: 81 additions & 0 deletions golang/internal/lab5/lab5.go
Original file line number Diff line number Diff line change
@@ -1 +1,82 @@
package lab5

import "errors"

const (
ErrAge = "неверно указан возраст, пожалуйста, укажите реальное значение"
ErrWeight = "неверно указан вес, пожалуйста, укажите реальное значение"
ErrHeight = "неверно указан рост, пожалуйста, укажите реальное значение"
)

type Cat struct {
age int
weight float64
height float64
name string
}

func NewCat(age int, weight, height float64, name string) (*Cat, error) {
c := &Cat{
age: age,
weight: weight,
height: height,
name: name,
}
if error := c.SetWeight(weight); error != nil {
return nil, error
}
if error := c.SetHeight(height); error != nil {
return nil, error
}
if error := c.SetAge(age); error != nil {
return nil, error
}
return c, nil
}

func (c *Cat) GetAge() int {
return c.age
}

func (c *Cat) SetAge(age int) error {
if age < 0 || age > 200 {
return errors.New(ErrAge)
}

c.age = age
return nil
}

func (c *Cat) GetWeight() float64 {
return c.weight
}

func (c *Cat) SetWeight(weight float64) error {
if weight < 0 || weight > 200 {
return errors.New(ErrWeight)
}

c.weight = weight
return nil
}

func (c *Cat) GetHeight() float64 {
return c.height
}

func (c *Cat) SetHeight(height float64) error {
if height < 0 || height > 200 {
return errors.New(ErrHeight)
}

c.height = height
return nil
}

func (c *Cat) GetName() string {
return c.name
}

func (c *Cat) SetName(name string) {
c.name = name
}
12 changes: 12 additions & 0 deletions golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"isuct.ru/informatics2022/internal/lab4"
"isuct.ru/informatics2022/internal/lab5"
)

func main() {
Expand All @@ -13,4 +14,15 @@ func main() {
SliceForTaskB := []float64{2.25, 1.31, 1.39, 1.44, 1.56, 1.92}
resultS := lab4.TaskB(SliceForTaskB, 2.25)
fmt.Println("Результаты TaskB:", resultS)
// LAB 5
fmt.Printf("\nLABORATORY: 5\n")
cat, err := lab5.NewCat(5, 3.5, 25.6, "Stesha")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Возраст: %d\n", cat.GetAge())
fmt.Printf("Вес: %.2f\n", cat.GetWeight())
fmt.Printf("Рост: %.3f\n", cat.GetHeight())
fmt.Println("Имя:", cat.GetName())
}