// resteraunt package main import ( "bufio" "fmt" "os" "strconv" "strings" "sync" "time" ) var num_of_app = 20 var num_of_entree = 12 var num_of_dessert = 12 func main() { wg := &sync.WaitGroup{} channel := make(chan string) fmt.Println("Resteraunt inventory: ") fmt.Println(num_of_app, "Appetizers") fmt.Println(num_of_entree, "Entrees") fmt.Println(num_of_dessert, "Desserts") file, err := os.Open("orders.txt") if err != nil { fmt.Println("Error opening file") return } defer file.Close() scanner := bufio.NewScanner(file) order_num := 1 go monitor_order(channel) for scanner.Scan() { line := scanner.Text() line_list := strings.SplitAfter(line, " ") apps_from_file := strings.Replace(line_list[0], " ", "", -1) entrees_from_file := strings.Replace(line_list[1], " ", "", -1) desserts_from_file := strings.Replace(line_list[2], " ", "", -1) apps_for_order, err := strconv.Atoi(apps_from_file) entrees_for_order, err := strconv.Atoi(entrees_from_file) desserts_for_order, err := strconv.Atoi(desserts_from_file) if err != nil { fmt.Println("Error accessing numbers in file") } wg.Add(1) time.Sleep(time.Second * 2) go cook_order(order_num, apps_for_order, entrees_for_order, desserts_for_order, channel, wg) order_num += 1 } wg.Wait() close(channel) time.Sleep(time.Second) fmt.Println("Dinner service has ended!") } func monitor_order(channel chan string) { for { msg, open := <-channel if !open { break } fmt.Println(msg) } } func cook_order(order_num int, apps int, entrees int, desserts int, channel chan string, wg *sync.WaitGroup) { fmt.Println("Order number ", order_num, " has started") fmt.Println(apps, "Appetizers,", entrees, "Entrees,", desserts, "Desserts.") fmt.Println("") for app := 0; app < apps; app++ { cook_appetizer() if num_of_app == 0 { channel <- "Out of appetizers!" } } for entree := 0; entree < entrees; entree++ { cook_entree() if num_of_entree == 0 { channel <- "Out of entrees!" } } for dessert := 0; dessert < desserts; dessert++ { cook_dessert() if num_of_dessert == 0 { channel <- "Out of desserts!" } } fmt.Println("Order number ", order_num, " has finished") wg.Done() } func cook_appetizer() { time.Sleep(time.Second * 1) num_of_app -= 1 } func cook_entree() { time.Sleep(time.Second * 5) num_of_entree -= 1 } func cook_dessert() { time.Sleep(time.Second * 3) num_of_dessert -= 1 }