diff --git a/learning/exercises/ex3_4_5.go b/learning/exercises/ex3_4_5.go new file mode 100644 index 0000000..bbf345a --- /dev/null +++ b/learning/exercises/ex3_4_5.go @@ -0,0 +1,27 @@ +package exercises + +import ( + "fmt" + + "github.com/plasmus777/go_lang/learning/exercises/model" +) + +// Create a model for item purchases containing: the date of ocurrence, the market's name and the bought items list. +// Have the model on a specific "model" package and create a function to initialize a model and return its related struct using pointers. +func Ex3_4_5() { + // purchase, error := model.InitializePurchase("") + purchase, error := model.InitializePurchase("Basic Market") + if error != nil { + fmt.Println("Error:", error) + } else { + purchase.AddItem2("Apple (1 pound)", 3, 1.29) + purchase.AddItem2("Pear (1 pound)", 2, 1.34) + purchase.AddItem2("Car Battery Charger", 1, 20.59) + purchase.AddItem2("Indoor Fly Trap", 1, 14.91) + + fmt.Println("Market:", purchase.Market) + fmt.Println("Time of the purchase:", purchase.PurchaseTime) + fmt.Println("-------- Bought Items --------") + purchase.ListItems() + } +} diff --git a/learning/exercises/model/purchase.go b/learning/exercises/model/purchase.go new file mode 100644 index 0000000..3b2f64f --- /dev/null +++ b/learning/exercises/model/purchase.go @@ -0,0 +1,56 @@ +package model + +import ( + "errors" + "fmt" + "time" +) + +type Purchase struct { + Market string + PurchaseTime time.Time + Items map[string]Item +} + +func InitializePurchase(name string) (*Purchase, error) { + if len(name) == 0 { + return nil, errors.New("The market's name cannot be empty.") + } + + purchase := Purchase{ + Market: name, + PurchaseTime: time.Now(), + Items: make(map[string]Item), + } + + return &purchase, nil +} + +func (p *Purchase) AddItem1(i Item) { + p.Items[i.Name] = i +} + +func (p *Purchase) AddItem2(name string, number int, price float64) { + p.Items[name] = Item{ + Name: name, + Number: number, + Price: price, + } +} + +func (p *Purchase) GetTotalValue() float64 { + var totalValue float64 + + for _, item := range p.Items { + totalValue += item.GetTotalValue() + } + + return totalValue +} + +func (p *Purchase) ListItems() { + for _, item := range p.Items { + item.PrintItem() + } + fmt.Println("\nTotal: $", p.GetTotalValue()) +} diff --git a/learning/exercises/model/purchase_item.go b/learning/exercises/model/purchase_item.go new file mode 100644 index 0000000..cce7970 --- /dev/null +++ b/learning/exercises/model/purchase_item.go @@ -0,0 +1,19 @@ +package model + +import "fmt" + +type Item struct { + Name string + Number int + Price float64 +} + +func (i *Item) GetTotalValue() float64 { + totalValue := float64(i.Number) * i.Price + + return totalValue +} + +func (i *Item) PrintItem() { + fmt.Println(i.Number, "X", i.Name, " - $", i.GetTotalValue(), " ($", i.Price, " each)") +} diff --git a/learning/interfaces/interfaces.go b/learning/interfaces/interfaces.go new file mode 100644 index 0000000..0ecf0b6 --- /dev/null +++ b/learning/interfaces/interfaces.go @@ -0,0 +1,5 @@ +package interfaces + +func Interfaces() { + +} diff --git a/learning/main.go b/learning/main.go index df9810c..c1c6654 100644 --- a/learning/main.go +++ b/learning/main.go @@ -6,12 +6,13 @@ import ( "github.com/plasmus777/go_lang/learning/arrays" "github.com/plasmus777/go_lang/learning/fluxcontrol" "github.com/plasmus777/go_lang/learning/inheritance" + "github.com/plasmus777/go_lang/learning/interfaces" "github.com/plasmus777/go_lang/learning/simple" ) func main() { - //exercises.Ex2() + // exercises.Ex3_4_5() input := 1 @@ -34,6 +35,7 @@ func main() { fmt.Println("13 - pointers") fmt.Println("14 - structs") fmt.Println("15 - inheritance") + fmt.Println("16 - interfaces") fmt.Println("") fmt.Println("-1 - exit") @@ -87,6 +89,9 @@ func main() { case 15: inheritance.Inheritance() + case 16: + interfaces.Interfaces() + case -1: fmt.Println("Exiting program.") return