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

лабораторная работа 7, Ткачёв Михаил #388

Open
wants to merge 13 commits into
base: Tkachev_Mihail
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
19e473e
начало работы над лабораторной работай 7, соданны структуры продуктов…
xakristl8 Nov 5, 2024
8edbd16
структуры распределенны по разным файлам, доработан метод для установ…
xakristl8 Nov 6, 2024
74ff844
добавленна функция для суммирования цен всех товаров, сделанный метод…
xakristl8 Nov 6, 2024
29a938e
Merge branch 'Tkachev_Mihail' into lab7
xakristl8 Nov 6, 2024
0ebfca3
исправльенно название функции lab7 на CompleteLab7
xakristl8 Nov 6, 2024
b4b3a23
исправленны конфликты
xakristl8 Nov 6, 2024
8f8d21c
изменение названия GetTotalCost() на GetTotalPrice() тк ругался литер
xakristl8 Nov 6, 2024
1af01d1
изменение названия GetTotalCost() на GetTotalPrice() и totalCost на t…
xakristl8 Nov 6, 2024
c0d741c
испровления конфликтов
xakristl8 Nov 6, 2024
996df79
интерфейс и функции для взаимодейстия с ним перенесенные в отедльный …
xakristl8 Nov 7, 2024
90a6719
изменение panic() на log.Fatal()
xakristl8 Nov 7, 2024
38e4d79
определенны enums, добавленны глобальные переменные для ошибок
xakristl8 Nov 9, 2024
154a11e
изменино название функции на более понятное
xakristl8 Nov 16, 2024
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
48 changes: 48 additions & 0 deletions golang/lab7/car.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package lab7

import "errors"

var ErrColor error = errors.New("invalid color")
var ErrMaxSpeed error = errors.New("the wrong maxSpeed value")

type Color string

const (
Red Color = "красный"
Green Color = "зелённый"
White Color = "белый"
Black Color = "чёрный"
)

type car struct {
product
color Color
maxSpeed float64
}

func (c *car) SetColor(color Color) error {
for _, standardColor := range []Color{Red, Green, White, Black} {
if color == standardColor {
c.color = color
return nil
}
}
return ErrColor
}

func (c *car) SetSpeed(maxSpeed float64) error {
if maxSpeed < 0 || maxSpeed > 250 {
return ErrMaxSpeed
}
c.maxSpeed = maxSpeed
return nil
}

func NewCar(id int, name string, price float64, color Color, maxSpeed float64) *car {
c := &car{
product: newProduct(id, name, price),
}
c.SetColor(color)
c.SetSpeed(maxSpeed)
return c
}
12 changes: 12 additions & 0 deletions golang/lab7/food.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lab7

type food struct {
product
}

func Newfood(id int, name string, price float64) *food {
f := &food{
product: newProduct(id, name, price),
}
return f
}
37 changes: 37 additions & 0 deletions golang/lab7/furniture.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lab7

import "errors"

var ErrMaterial error = errors.New("invalid material")

type Material string

const (
Skin Material = "кожа"
Wood Material = "дерево"
Plywood Material = "Фанера"
Dsp Material = "дсп"
)

type furniture struct {
product
material Material
}

func (f *furniture) SetMaterial(material Material) error {
for _, standardmaterial := range []Material{Skin, Wood, Plywood, Dsp} {
if material == standardmaterial {
f.material = material
return nil
}
}
return ErrMaterial
}

func NewFurniture(id int, name string, price float64, material Material) *furniture {
f := &furniture{
product: newProduct(id, name, price),
}
f.SetMaterial(material)
return f
}
42 changes: 42 additions & 0 deletions golang/lab7/lab7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lab7

import (
"fmt"
"log"
)

func CompleteLab7() {
var err error

bentli := NewCar(0, "бентли", 100000, Red, 200)
sofa := NewFurniture(1, "диван", 1000, Skin)
chear := NewFurniture(2, "стул", 10, Wood)
cucumber := Newfood(3, "огурец", 5)

var ProductList []Product = []Product{
bentli,
sofa,
chear,
cucumber,
}

fmt.Printf("общяя стоимость товара до скидок: %v \n", GetTotalPrice(ProductList))
for _, product := range ProductList {
err = product.SetDiscount(10)
if err != nil {
log.Fatal(err)
}
}
fmt.Printf("общяя стоимость товара после скидок в 10 процентов: %v \n", GetTotalPrice(ProductList))

err = bentli.SetColor(White)
if err != nil {
log.Fatal(err)
}
err = sofa.SetMaterial(Plywood)
if err != nil {
log.Fatal(err)
}

displayListProducts(ProductList)
}
43 changes: 43 additions & 0 deletions golang/lab7/product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package lab7

import "errors"

var ErrPercentageValue error = errors.New("the wrong percentage value")

type product struct {
id int
name string
price float64
discount float64
}

func (p product) GetPrice() float64 { return p.price }

func (p product) GetName() string { return p.name }

func (p product) GetDiscount() float64 { return p.discount }

func (p *product) SetDiscount(percent float64) error {
if percent < 1 || percent > 100 {
return ErrPercentageValue
}
p.discount = percent

change := (p.price / 100) * percent

if change > p.price {
p.price = 0
return nil
}
p.price -= change
return nil
}

func newProduct(id int, name string, price float64) product {
return product{
id: id,
name: name,
price: price,
discount: 0,
}
}
28 changes: 28 additions & 0 deletions golang/lab7/productInterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package lab7

import "fmt"

type Product interface {
SetDiscount(float64) error
GetDiscount() float64
GetPrice() float64
GetName() string
}

func displayListProducts(PList []Product) {
fmt.Println("____список товаров_____")
for _, p := range PList {
fmt.Printf(
"________________________\nтовар \"%v\" стоит %v$ благодоря скидке в %v процентов\n",
p.GetName(), p.GetPrice(), p.GetDiscount())
}
}

func GetTotalPrice(ProductList []Product) float64 {
var totalPrice float64

for _, product := range ProductList {
totalPrice += product.GetPrice()
}
return totalPrice
}
2 changes: 2 additions & 0 deletions golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"

"isuct.ru/informatics2022/lab7"
"isuct.ru/informatics2022/laba4"
"isuct.ru/informatics2022/laba6"
)
Expand All @@ -12,4 +13,5 @@ func main() {

laba4.CompleteLaba4()
laba6.CompleteLaba6()
lab7.CompleteLab7()
}
Loading