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
63 changes: 63 additions & 0 deletions golang/internal/lab5/lab5.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 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 output(answersX, answersY []float64) {
Expand All @@ -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)
}
}