Skip to content

Commit

Permalink
Exercises 3, 4 and 5
Browse files Browse the repository at this point in the history
Create and utilize structs and their properties, manipulating more advanced data structures.
  • Loading branch information
plasmus777 committed Apr 23, 2024
1 parent 03e234b commit 09aabb5
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
27 changes: 27 additions & 0 deletions learning/exercises/ex3_4_5.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
56 changes: 56 additions & 0 deletions learning/exercises/model/purchase.go
Original file line number Diff line number Diff line change
@@ -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())
}
19 changes: 19 additions & 0 deletions learning/exercises/model/purchase_item.go
Original file line number Diff line number Diff line change
@@ -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)")
}
5 changes: 5 additions & 0 deletions learning/interfaces/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package interfaces

func Interfaces() {

}
7 changes: 6 additions & 1 deletion learning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down Expand Up @@ -87,6 +89,9 @@ func main() {
case 15:
inheritance.Inheritance()

case 16:
interfaces.Interfaces()

case -1:
fmt.Println("Exiting program.")
return
Expand Down

0 comments on commit 09aabb5

Please sign in to comment.