From 8f796cc9d6761dcc1677302af96ab7f6c198968a Mon Sep 17 00:00:00 2001 From: busiginandrew Date: Sat, 6 Jan 2024 20:58:58 +0300 Subject: [PATCH] lab5dontbeatme --- golang/internal/lab5/lab5.go | 63 ++++++++++++++++++++++++++++++++++++ golang/main.go | 10 ++++++ 2 files changed, 73 insertions(+) create mode 100644 golang/internal/lab5/lab5.go diff --git a/golang/internal/lab5/lab5.go b/golang/internal/lab5/lab5.go new file mode 100644 index 00000000..7973d123 --- /dev/null +++ b/golang/internal/lab5/lab5.go @@ -0,0 +1,63 @@ +package lab5 + +import ( + "errors" +) + +const ( + ErrIncorrectSpeed = "incorrect speed" + ErrIncorrectWeight = "incorrect weight" +) + +type Car struct { + speed int + weight int + name string +} + +func (c *Car) SetSpeed(speed int) error { + if speed >= 0 && speed <= 500 { + c.speed = speed + return nil + } + return errors.New(ErrIncorrectSpeed) +} + +func (c *Car) SetWeight(weight int) error { + if weight >= 1000 && weight <= 2000 { + c.weight = weight + return nil + } + return errors.New(ErrIncorrectWeight) +} + +func (c *Car) SetName(name string) { + c.name = name +} + +func (c Car) GetSpeed() int { + return c.speed +} + +func (c Car) GetWeight() int { + return c.weight +} + +func (c Car) GetName() string { + return c.name +} + +func NewCar(speed int, weight int, name string) (*Car, error) { + tmp := &Car{ + speed: speed, + weight: weight, + name: name, + } + if err := tmp.SetSpeed(speed); err != nil { + return nil, err + } + if err := tmp.SetWeight(weight); err != nil { + return nil, err + } + return tmp, nil +} diff --git a/golang/main.go b/golang/main.go index bc227679..d040f1f5 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 output(answersX, answersY []float64) { @@ -19,4 +20,13 @@ func main() { fmt.Println("__________________") xL, yL = lab4.TaskB([]float64{1.84, 2.71, 3.81, 4.56, 5.62}) output(xL, yL) + + car, err := lab5.NewCar(200, 1000, "BUSIGINMOBIL") + if (err == nil) { + fmt.Printf("Car's speed is %d\n", car.GetSpeed()) + fmt.Printf("Car's weight is %d\n", car.GetWeight()) + fmt.Printf("Car's name is %s\n", car.GetName()) + } else { + fmt.Println(err) + } }