diff --git a/golang/lab7/book.go b/golang/lab7/book.go new file mode 100644 index 00000000..ef1fd6e6 --- /dev/null +++ b/golang/lab7/book.go @@ -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)) +} \ No newline at end of file diff --git a/golang/lab7/clothes.go b/golang/lab7/clothes.go new file mode 100644 index 00000000..fc7df14b --- /dev/null +++ b/golang/lab7/clothes.go @@ -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)) +} \ No newline at end of file diff --git a/golang/lab7/electronics.go b/golang/lab7/electronics.go new file mode 100644 index 00000000..7f59218f --- /dev/null +++ b/golang/lab7/electronics.go @@ -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)) +} \ No newline at end of file diff --git a/golang/lab7/lab7.go b/golang/lab7/lab7.go new file mode 100644 index 00000000..fd3a4df1 --- /dev/null +++ b/golang/lab7/lab7.go @@ -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)) +} \ No newline at end of file diff --git a/golang/main.go b/golang/main.go index c1704552..7c85e216 100644 --- a/golang/main.go +++ b/golang/main.go @@ -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() +} \ No newline at end of file