Skip to content
Open

Lab7 #605

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

import "fmt"

type Headphones struct {
Brand string
Connection string
Price float64
}

func (h Headphones) GetPrice() float64 {
return h.Price
}

func (h *Headphones) EditPrice(NewPrice float64) {
(*h).Price = NewPrice
}

func (h *Headphones) ApplyDiscount(Percent float64) {
(*h).Price = (h.Price / 100) * (100 - Percent)
}

func (h *Headphones) EditDescription(NewDescription string) {
(*h).Connection = NewDescription
}

func (h Headphones) GetInfo() {
fmt.Println("В продаже есть наушники от производителя", h.Brand, "с", h.Connection, "подключением и стоимостью:", h.Price)
}
29 changes: 29 additions & 0 deletions golang/Lab7/keyboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lab7

import "fmt"

type Keyboard struct {
Brand string
Connection string
Price float64
}

func (k Keyboard) GetPrice() float64 {
return k.Price
}

func (k *Keyboard) EditPrice(NewPrice float64) {
(*k).Price = NewPrice
}

func (k *Keyboard) ApplyDiscount(Percent float64) {
(*k).Price = (k.Price / 100) * (100 - Percent)
}

func (k *Keyboard) EditDescription(NewDescription string) {
(*k).Connection = NewDescription
}

func (k Keyboard) GetInfo() {
fmt.Println("В продаже есть клавиатура от производителя", k.Brand, "с", k.Connection, "подключением и стоимостью:", k.Price)
}
49 changes: 49 additions & 0 deletions golang/Lab7/lab7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lab7

import (
"fmt"
)

type Product interface {
GetInfo()
GetPrice() float64
EditPrice(float64)
ApplyDiscount(float64)
EditDescription(string)
}

func CalculatePrice(list []Product) float64 {
var sum float64 = 0
for _, price := range list {
sum += price.GetPrice()
}
return sum
}
Comment on lines +7 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В отдельный файл, разные сущности нужно всегда хранить отдельно


func RunLab7() {
var mouse Product = &Mouse{"Razer", "оптическим светодиодным", 7499}
var keyboard Product = &Keyboard{"HyperX", "беспроводным", 8999}
var headphones Product = &Headphones{"Logitech", "беспроводным", 12999}

mouse.GetInfo()
keyboard.GetInfo()
headphones.GetInfo()

var purchase []Product = []Product{mouse, keyboard, headphones}
sum := CalculatePrice(purchase)
fmt.Println("Общая стоимость товара:", sum)

mouse.EditDescription("оптическим")
mouse.ApplyDiscount(15)
keyboard.EditDescription("проводным")
keyboard.ApplyDiscount(20)
headphones.EditDescription("проводным")
headphones.ApplyDiscount(10)

mouse.GetInfo()
keyboard.GetInfo()
headphones.GetInfo()

sum = CalculatePrice(purchase)
fmt.Println("Общая стоимость товара:", sum)
}
29 changes: 29 additions & 0 deletions golang/Lab7/mouse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lab7

import "fmt"

type Mouse struct {
Brand string
Sensor string
Price float64
}

func (m Mouse) GetPrice() float64 {
return m.Price
}

func (m *Mouse) EditPrice(NewPrice float64) {
(*m).Price = NewPrice
}

func (m *Mouse) ApplyDiscount(Percent float64) {
(*m).Price = (m.Price / 100) * (100 - Percent)
}

func (m *Mouse) EditDescription(NewDescription string) {
(*m).Sensor = NewDescription
}

func (m Mouse) GetInfo() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А здесь почему без указателя? Есть какое-то необходимость в этом? Это не указание на ошибку, именно вопрос

fmt.Println("В продаже есть компьютерная мышь от производителя", m.Brand, "с ", m.Sensor, "сенсором и стоимостью:", m.Price)
}
2 changes: 2 additions & 0 deletions golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (

lab4 "isuct.ru/informatics2022/Lab4"
lab6 "isuct.ru/informatics2022/Lab6"
lab7 "isuct.ru/informatics2022/Lab7"
)

func main() {
fmt.Println("Головин Даниил Александрович")
lab4.RunLab4()
lab6.RunLab6()
lab7.RunLab7()
}
Loading