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

Baldina Lab7 #445

Open
wants to merge 5 commits into
base: Baldina_Daria
Choose a base branch
from
Open
Changes from 2 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
64 changes: 64 additions & 0 deletions golang/Lab7/Lab7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package Lab7

import (
"fmt"
)

type Product interface {
GetName() string
GetPrice() float64
SetPrice(price float64)
ApplyDiscount(discount float64)
}

type Item struct {
Name string
Price float64
}

func (i *Item) GetName() string {
return i.Name
}

func (i *Item) GetPrice() float64 {
return i.Price
}

func (i *Item) SetPrice(price float64) {
i.Price = price
}

func (i *Item) ApplyDiscount(discount float64) {
i.Price -= discount
if i.Price < 0 {
i.Price = 0
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Нужно создать структуры конкретные, а не абстрактные. Создайте структуры Утюг, Микроволновка и Холодильник в отдельных файла и используйте здесь


func CalculateTotal(products []Product) float64 {
total := 0.0
for _, product := range products {
total += product.GetPrice()
}
return total
}

func RunLab7() {
item1 := &Item{Name: "Утюг", Price: 1000.0}
item2 := &Item{Name: "Микроволновка", Price: 1500.0}
item3 := &Item{Name: "Холодильник", Price: 3000.0}

item1.ApplyDiscount(100)
item2.ApplyDiscount(50)

products := []Product{item1, item2, item3}

fmt.Printf("Общая стоимость без учёта скидок: %.2f\n", CalculateTotal([]Product{
&Item{Name: "Утюг", Price: 1000.0},
&Item{Name: "Микроволновка", Price: 1500.0},
&Item{Name: "Холодильник", Price: 3000.0},
}))

totalAfterDiscounts := CalculateTotal(products)
fmt.Printf("Общая стоимость с учётом скидок: %.2f\n", totalAfterDiscounts)
}
Loading