-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create and utilize structs and their properties, manipulating more advanced data structures.
- Loading branch information
1 parent
03e234b
commit 09aabb5
Showing
5 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
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
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() | ||
} | ||
} |
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
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()) | ||
} |
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
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)") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package interfaces | ||
|
||
func Interfaces() { | ||
|
||
} |
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