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

Lab7 #526

Open
wants to merge 7 commits into
base: Krukova_Kristina
Choose a base branch
from
Open

Lab7 #526

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

import "fmt"

type Book struct {
name string
author string
price float64
description string
}

func NewBook(name string, author string, price float64, description string) *Book {
book := &Book{name: name, author: author, price: price, description: description}
return book
}

func (b *Book) getName() string {
return b.name
}

func (b *Book) setPrice(newPrice float64) {
b.price = newPrice
}

func (b *Book) getPrice() float64 {
return b.price
}

func (b *Book) changeData(name string, price float64, description string) {
b.name = name
b.price = price
b.description = description
}

func (b *Book) getData() {
fmt.Printf("name: %s\nauthor: %s\nprice: %.2f\ndescription: %s\n\n", b.name, b.author, b.price, b.description)
}

func (b *Book) applyDiscount(perDiscount float64) {
b.price = b.price * (1 - (perDiscount / 100))
}
41 changes: 41 additions & 0 deletions golang/lab7/clothes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lab7

import "fmt"

type Clothes struct {
name string
brand string
price float64
description string
}

func NewClothes(name string, brand string, price float64, description string) *Clothes {
clothes := &Clothes{name: name, brand: brand, price: price, description: description}
return clothes
}

func (c *Clothes) getName() string {
return c.name
}

func (c *Clothes) setPrice(newPrice float64) {
c.price = newPrice
}

func (c *Clothes) getPrice() float64 {
return c.price
}

func (c *Clothes) changeData(name string, price float64, description string) {
c.name = name
c.price = price
c.description = description
}

func (c *Clothes) getData() {
fmt.Printf("name: %s\nbrand: %s\nprice: %.2f\ndescription: %s\n\n", c.name, c.brand, c.price, c.description)
}

func (c *Clothes) applyDiscount(perDiscount float64) {
c.price = c.price * (1 - (perDiscount / 100))
}
41 changes: 41 additions & 0 deletions golang/lab7/electronics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lab7

import "fmt"

type Electronics struct {
name string
model string
price float64
description string
}

func NewElectronics(name string, model string, price float64, description string) *Electronics {
electronics := &Electronics{name: name, model: model, price: price, description: description}
return electronics
}

func (e *Electronics) getName() string {
return e.name
}

func (e *Electronics) setPrice(newPrice float64) {
e.price = newPrice
}

func (e *Electronics) getPrice() float64 {
return e.price
}

func (e *Electronics) changeData(name string, price float64, description string) {
e.name = name
e.price = price
e.description = description
}

func (e *Electronics) getData() {
fmt.Printf("name: %s\nmodel: %s\nprice: %.2f\ndescription: %s\n\n", e.name, e.model, e.price, e.description)
}

func (e *Electronics) applyDiscount(perDiscount float64) {
e.price = e.price * (1 - (perDiscount / 100))
}
56 changes: 56 additions & 0 deletions golang/lab7/lab7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package lab7

import (
"fmt"
)

type Products interface {
getName() string
setPrice(float64)
getPrice() float64
changeData(string, float64, string)
getData()
applyDiscount(float64)
}

func calculateDiscount(products []Products) float64 {
var sum float64 = 0
for _, product := range products {
sum += product.getPrice()
}

return sum
}

func RunLab7() {
product1 := NewBook("Harry Potter", "JK Rowling", 1000.0, "A book on Harry Potter")
product2 := NewClothes("T-shirt", "Nike", 900.0, "Nice T-shirt")
product3 := NewElectronics("Lamp", "Diode", 100.0, "Wonderful lamp")

listOfProduct := []Products{product1, product2, product3}

fmt.Printf("\nПродукты до изменения\n\n")
product1.getData()
product2.getData()
product1.changeData("Improved book Harry Potter", 1500.0, "Interesting book")
product2.changeData("Premium T-shirt", 1000.0, "A high-quality cotton T-shirt.")
fmt.Printf("\nПродукты после изменения\n\n")
product1.getData()
product2.getData()

fmt.Println("Товар-----------Цена")
for _, product := range listOfProduct {
fmt.Printf("%s-------%.2f $\n", product.getName(), product.getPrice())
}
fmt.Printf("Цена корзины до скидки: %.2f $\n\n", calculateDiscount(listOfProduct))

product1.applyDiscount(10)
product2.applyDiscount(20)
product3.applyDiscount(15)

fmt. Println("Товар-----------Цена")
for _, product := range listOfProduct {
fmt.Printf("%s-------%.2f $\n", product.getName(), product.getPrice())
}
fmt.Printf("Цена корзины после скидки: %.2f $\n", calculateDiscount(listOfProduct))
}
15 changes: 9 additions & 6 deletions golang/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import ("fmt"
"isuct.ru/informatics2022/lab6"
import (
"fmt"
"isuct.ru/informatics2022/lab4"
"isuct.ru/informatics2022/lab6"
"isuct.ru/informatics2022/lab7"
)

func main() {
fmt.Println("Krukova Kristina Ivanovna")
lab4.AnswerLab4()
lab6.RunLab6()
}
fmt.Println("Krukova Kristina Ivanovna")
lab4.AnswerLab4()
lab6.RunLab6()
lab7.RunLab7()
}
Loading