-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (152 loc) · 4.98 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import "fmt"
// The Car class represents a specific car in the store.
type Car struct {
Make string
Model string
Year int
Product
}
// The Product class represents a product in the store, including cars.
// It has attributes of a product, such as its name, quantity in stock, and price.
type Product struct {
Name string
Quantity int
Price float64
}
// The ProductInterface interface defines the methods that a Product must implement.
type ProductInterface interface {
DisplayProduct()
DisplayStatus()
UpdateQuantity(int) error
}
// DisplayProduct is a method of the Product class that displays the product's information.
func (p Product) DisplayProduct() {
fmt.Printf("Product: %s\n", p.Name)
fmt.Printf("Quantity: %d\n", p.Quantity)
fmt.Printf("Price: $%.2f\n", p.Price)
}
// DisplayStatus is a method of the Product class that displays the product's status (in stock or out of stock).
func (p Product) DisplayStatus() {
if p.Quantity > 0 {
fmt.Println("Status: In stock")
} else {
fmt.Println("Status: Out of stock")
}
}
// UpdateQuantity is a method of the Product class that updates the product's quantity in stock.
func (p Product) UpdateQuantity(quantity int) error {
if p.Quantity+quantity < 0 {
return fmt.Errorf("cannot set quantity to a negative value")
}
p.Quantity += quantity
return nil
}
// The Store class represents a store that sells products, including cars.
// It has a list of products in stock and a list of sold products.
type Store struct {
Products []ProductInterface
SoldProducts []ProductInterface
}
// The StoreInterface interface defines the methods that a Store must implement.
type StoreInterface interface {
AddProduct(ProductInterface)
ListProducts()
SellProduct(string) error
ListSoldProducts()
SearchProduct(string) ProductInterface
}
// AddProduct is a method of the Store class that adds a product to the store's list of products in stock.
func (s *Store) AddProduct(p ProductInterface) {
s.Products = append(s.Products, p)
}
// ListProducts is a method of the Store class that lists all the products in the store's list of products in stock.
func (s *Store) ListProducts() {
for _, p := range s.Products {
p.DisplayProduct()
p.DisplayStatus()
fmt.Println()
}
}
// SellProduct is a method of the Store class that sells a product from the store's list of products in stock and adds it to the store's list of sold products.
func (s *Store) SellProduct(name string) error {
// Search for the product in the store's list of products in stock.
product := s.SearchProduct(name)
if product == nil {
return fmt.Errorf("product not found")
}
//Asser the product is a Product type.
p, ok := product.(*Product)
if !ok {
return fmt.Errorf("product is not a Product type")
}
// Check if there is sufficient quantity in stock to sell the product.
if p.Quantity < 1 {
return fmt.Errorf("product is out of stock")
}
// Remove the product from the store's list of products in stock and add it to the store's list of sold products.
for i, p := range s.Products {
// Assert that the p variable is of type Product.
p, ok := p.(*Product)
if !ok {
return fmt.Errorf("product has wrong type")
}
if p.Name == name {
s.SoldProducts = append(s.SoldProducts, p)
s.Products = append(s.Products[:i], s.Products[i+1:]...)
break
}
}
// Update the product's quantity in stock.
err := product.UpdateQuantity(-1)
if err != nil {
return err
}
return nil
}
// ListSoldProducts is a method of the Store class that lists all the products in the store's list of sold products.
func (s *Store) ListSoldProducts() {
for _, p := range s.SoldProducts {
p.DisplayProduct()
fmt.Println()
}
}
// SearchProduct is a method of the Store class that searches for a product with a given name in the store's list of products in stock.
// It returns the product if found, or nil if not found.
func (s *Store) SearchProduct(name string) ProductInterface {
for _, p := range s.Products {
// Assert that the p variable is of type Product.
p, ok := p.(*Product)
if !ok {
return nil
}
if p.Name == name {
return p
}
}
return nil
}
func main() {
// Create a new store.
store := &Store{}
// Add some cars to the store.
store.AddProduct(&Car{Make: "Toyota", Model: "Camry", Year: 2020, Product: Product{Name: "Toyota Camry", Quantity: 3, Price: 30000}})
store.AddProduct(&Car{Make: "Honda", Model: "Accord", Year: 2021, Product: Product{Name: "Honda Accord", Quantity: 5, Price: 35000}})
store.AddProduct(&Car{Make: "Ford", Model: "Mustang", Year: 2019, Product: Product{Name: "Ford Mustang", Quantity: 2, Price: 40000}})
// List the products in the store.
fmt.Println("Products in stock:")
store.ListProducts()
fmt.Println()
// Sell a car from the store.
err := store.SellProduct("Toyota Camry")
if err != nil {
fmt.Println(err)
}
// List the products in the store again.
fmt.Println("\nProducts in stock:")
store.ListProducts()
fmt.Println()
// List the sold products.
fmt.Println("\nSold products:")
store.ListSoldProducts()
}