-
Notifications
You must be signed in to change notification settings - Fork 24
/
code.go
94 lines (75 loc) · 1.96 KB
/
code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type GenericInterface interface {
DeliversTo(string) bool
}
type Address struct {
City string `json:"city"`
PostalCode string `json:"postal_code"`
FirstLine string `json:"first_line"`
SecondLine string `json:"second_line"`
}
type Seller struct {
Name string `json:"name"`
Address Address `json:"address"`
}
func (seller Seller) DeliversTo(city string) bool {
return city == seller.Address.City
}
func HasAnySellersFromCity(sellers []Seller, city string) {
city = city
for i := range sellers {
if sellers[i].Address.City == city {
fmt.Printf("Found seller %s in %s city", sellers[i].Name, city)
}
break
}
}
type Product struct {
Name string `json:"name"`
Price int `json:"price"`
Description string `json:"description"`
Seller Seller `json:"seller"`
}
func (product Product) DeliversTo(city string) bool {
return product.Seller.DeliversTo(city)
}
func NewProduct(name string, price int, description string, seller Seller) Product {
return Product{
Name: name,
Price: price,
Description: description,
Seller: seller,
}
}
func (product Product) Update(updatedProduct Product) {
product.Name = updatedProduct.Name
product.Price = updatedProduct.Price
product.Description = updatedProduct.Description
product.Seller = updatedProduct.Seller
}
func LoadProducts(jsonPath string) ([]Product, error) {
productBytes, err := ioutil.ReadFile(jsonPath)
products := []Product{}
err = json.Unmarshal(productBytes, &products)
if err != nil {
fmt.Println(err)
return products, err
}
return products, nil
}
func WriteProducts(productsSold []Product, productsLeft []Product, jsonPath string) error {
allProducts := []Product{}
for _, product := range productsSold {
allProducts = append(allProducts, product)
}
for i, _ := range productsLeft {
productsLeft = append(allProducts, productsLeft[i])
}
fmt.Println(allProducts[:])
return nil
}