Last active
August 28, 2023 13:14
-
-
Save AhmetEnesKCC/3ff42a58f68aa30631f5e7552a23cf45 to your computer and use it in GitHub Desktop.
cashier.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```go | |
package main | |
import ( | |
"fmt" | |
) | |
type Item struct { | |
Name string | |
Price float64 | |
discount float64 | |
} | |
type Describable interface { | |
Desription() string | |
} | |
func (i Item) Desription() { | |
if i.discount > 0 { | |
fmt.Printf("Name: %s, Price: %.2f, Discounted Price: %.2f\n", i.Name, i.Price, calculatePrice(i)) | |
} else { | |
fmt.Printf("Name: %s, Price: %.2f\n", i.Name, i.Price) | |
} | |
} | |
func calculatePrice(item Item) float64 { | |
if item.discount > 0 { | |
return item.Price - item.discount | |
} | |
return item.Price | |
} | |
func totalPrice(items []Item) float64 { | |
var total float64 | |
for _, item := range items { | |
price := calculatePrice(item) | |
total += price | |
} | |
return total | |
} | |
func main() { | |
var items [4]Item | |
items[0] = Item{ | |
Name: "Laptop", | |
Price: 50000, | |
discount: 5000, | |
} | |
items[1] = Item{ | |
Name: "Phone", | |
Price: 40000, | |
discount: 3200, | |
} | |
items[2] = Item{ | |
Name: "Playstation", | |
Price: 20000, | |
discount: 1200, | |
} | |
items[3] = Item{ | |
Name: "Vergisiz Phone", | |
Price: 2000, | |
discount: 0, | |
} | |
for _, item := range items { | |
item.Desription() | |
} | |
fmt.Printf("\a") | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment