Skip to content
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

lab5 #279

Open
wants to merge 1 commit into
base: Hosrovjan_Aleksandr_Armenovich
Choose a base branch
from
Open

lab5 #279

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

import "fmt"

type pistol struct {
magazineCapacity int
model string
caliber string
}

func (c pistol) GetmagazineCapacity() int {
return c.magazineCapacity
}

func NewPistol(capacityVarious int, modelVarious, caliberVarious string) (pistol, error) {
var p pistol = pistol{
model: modelVarious,
caliber: caliberVarious,
}
var err = p.SetmagazineCapacity(capacityVarious)
return p, err
}

func (c *pistol) SetmagazineCapacity(capacity int) error {
if capacity >= 5 && capacity <= 30 {
c.magazineCapacity = capacity
return nil
}
return fmt.Errorf("invalid pistol magazine capacity")
}

func (c pistol) GetModel() string {
return c.model
}

func (c *pistol) SetModel(model string) {
c.model = model
}

func (c pistol) GetCaliber() string {
return c.caliber
}
26 changes: 26 additions & 0 deletions golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ package main

import (
"fmt"
"log"

"github.com/stretchr/testify/internal/lab4"
"github.com/stretchr/testify/internal/pistolStruct"
)

func checkErrors(err error) {
if err != nil {
log.Fatal(err)
}
}

func main() {
fmt.Println("Хосровян Александр Арменович")

Expand All @@ -19,4 +27,22 @@ func main() {
fmt.Println("Task B:")
var list = []float64{0.2, 0.3, 0.38, 0.43, 0.57}
fmt.Println(lab4.TaskB(list))

fmt.Println("Lab5:")
pistol, err := pistolStruct.NewPistol(9, "Desert Eagler", ".357 Magnum")
checkErrors(err)

pistol2, err := pistolStruct.NewPistol(15, "...", "...")
checkErrors(err)

pistol2.SetModel("Glock")
pistol.SetModel("Revolver")

err = pistol.SetmagazineCapacity(6)
checkErrors(err)
fmt.Printf("Pistol's magazine capacity is %d\n", pistol.GetmagazineCapacity())
fmt.Printf("Its model is %s\n", pistol.GetModel())

fmt.Printf("Pistol's magazine capacity is %d\n", pistol2.GetmagazineCapacity())
fmt.Printf("Its model is %s\n", pistol2.GetModel())
}