diff --git a/golang/internal/lab5/lab5.go b/golang/internal/lab5/lab5.go index fc01ead1..d0269b76 100644 --- a/golang/internal/lab5/lab5.go +++ b/golang/internal/lab5/lab5.go @@ -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 +} diff --git a/golang/main.go b/golang/main.go index 4d543191..41233841 100644 --- a/golang/main.go +++ b/golang/main.go @@ -4,6 +4,7 @@ import ( "fmt" "isuct.ru/informatics2022/internal/lab4" + "isuct.ru/informatics2022/internal/lab5" ) func main() { @@ -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()) }