feat(main): 添加并发计算和订单分发功能
- 新增 operations1.go 文件实现带通道的并发价格计算 - 新增 orderdispatch.go 文件实现订单分发和客户通知- 修改 main.go 调用新的并发计算和订单分发函数 - 更新 product.go 中 "Unsteady Chair"价格从 29.95 到 75 - 修正 product.go 中产品组初始化逻辑,确保正确添加新产品组- 在 CalcStoreTotal 函数中增加执行开始提示信息- 在 TotalPrice 方法中增加产品名称打印信息 - main 函数中调整执行顺序并增加换行符以改善输出格式
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("main function started")
|
||||
CalcStoreTotal(Products)
|
||||
fmt.Println("main function complete")
|
||||
CalcStoreTotal1(Products)
|
||||
main1()
|
||||
main2()
|
||||
fmt.Println("\nmain function complete")
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import "fmt"
|
||||
|
||||
func CalcStoreTotal(data ProductData) {
|
||||
fmt.Println("\nCalcStoreTotal function started:")
|
||||
var storeTotal float64
|
||||
for category, group := range data {
|
||||
storeTotal += group.TotalPrice(category)
|
||||
@@ -12,6 +13,7 @@ func CalcStoreTotal(data ProductData) {
|
||||
|
||||
func (group ProductGroup) TotalPrice(category string) (total float64) {
|
||||
for _, p := range group {
|
||||
fmt.Println(category, "product:", p.Name)
|
||||
total += p.Price
|
||||
}
|
||||
fmt.Println(category, "subtotal: ", ToCurrency(total))
|
||||
|
||||
36
14-concurrency/concurrency/operations1.go
Normal file
36
14-concurrency/concurrency/operations1.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func CalcStoreTotal1(data ProductData) {
|
||||
fmt.Println("\nCalcStoreTotal1 function started:")
|
||||
var storeTotal float64
|
||||
var channel chan float64 = make(chan float64, 2)
|
||||
for category, group := range data {
|
||||
go group.TotalPrice1(category, channel)
|
||||
}
|
||||
for i := 0; i < len(data); i++ {
|
||||
fmt.Println("-- channel read pending", len(channel), "items in buffer,size", cap(channel))
|
||||
value := <-channel
|
||||
fmt.Println("-- channel read complete", value)
|
||||
storeTotal += value
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
fmt.Println("Total: ", ToCurrency(storeTotal))
|
||||
}
|
||||
|
||||
func (group ProductGroup) TotalPrice1(category string, resultChannel chan float64) {
|
||||
var total float64
|
||||
for _, p := range group {
|
||||
// fmt.Println(category, "product:", p.Name)
|
||||
total += p.Price
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
}
|
||||
fmt.Println(category, "channel sending: ", ToCurrency(total))
|
||||
resultChannel <- total
|
||||
fmt.Println(category, "channel send complete")
|
||||
return
|
||||
}
|
||||
58
14-concurrency/concurrency/orderdispatch.go
Normal file
58
14-concurrency/concurrency/orderdispatch.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DispatchNotification struct {
|
||||
Customer string
|
||||
*Product
|
||||
Quantity int
|
||||
}
|
||||
|
||||
var Customers = []string{"Alice", "Bob", "Charlie", "David", "Edith", "Fred"}
|
||||
|
||||
func DispatchOrders(channel chan<- DispatchNotification) {
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
orderCount := rand.Intn(3) + 2
|
||||
fmt.Println("Order count:", orderCount)
|
||||
for i := 0; i < orderCount; i++ {
|
||||
channel <- DispatchNotification{
|
||||
Customer: Customers[rand.Intn(len(Customers))],
|
||||
Quantity: rand.Intn(5) + 1,
|
||||
Product: ProductList[rand.Intn(len(ProductList))],
|
||||
}
|
||||
//if i == 1 {
|
||||
// notification := <-channel
|
||||
// fmt.Println("Read:", notification.Customer)
|
||||
//}
|
||||
}
|
||||
|
||||
close(channel)
|
||||
}
|
||||
|
||||
func main1() {
|
||||
fmt.Println("\nmain1")
|
||||
dispatchChannel := make(chan DispatchNotification, 100)
|
||||
go DispatchOrders(dispatchChannel)
|
||||
for {
|
||||
if details, open := <-dispatchChannel; open {
|
||||
fmt.Println("Dispatching order to", details.Customer, ":", details.Product.Name, "x", details.Quantity)
|
||||
} else {
|
||||
fmt.Println("Channel has been closed")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main2() {
|
||||
fmt.Println("\nmain2")
|
||||
dispatchChannel := make(chan DispatchNotification, 100)
|
||||
go DispatchOrders(dispatchChannel)
|
||||
for details := range dispatchChannel {
|
||||
fmt.Println("Dispatching order to", details.Customer, ":", details.Product.Name, "x", details.Quantity)
|
||||
}
|
||||
fmt.Println("Channel has been closed")
|
||||
}
|
||||
@@ -16,7 +16,7 @@ var ProductList = []*Product{
|
||||
{"Corner Flags", "Soccer", 34.95},
|
||||
{"Stadium", "Soccer", 79500},
|
||||
{"Thinking Cap", "Chess", 16},
|
||||
{"Unsteady Chair", "Chess", 29.95},
|
||||
{"Unsteady Chair", "Chess", 75},
|
||||
{"Bling-Bling King", "Chess", 1200},
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func init() {
|
||||
if _, ok := Products[product.category]; ok {
|
||||
Products[product.category] = append(Products[product.category], product)
|
||||
} else {
|
||||
Products[product.category] = ProductGroup{}
|
||||
Products[product.category] = ProductGroup{product}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user