| 
									
										
										
										
											2019-06-21 12:18:33 +03:00
										 |  |  | // Copyright © 2018 Inanc Gumus | 
					
						
							|  |  |  | // Learn Go Programming Course | 
					
						
							|  |  |  | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-10-30 19:34:44 +03:00
										 |  |  | // For more tutorials  : https://learngoprogramming.com | 
					
						
							|  |  |  | // In-person training  : https://www.linkedin.com/in/inancgumus/ | 
					
						
							|  |  |  | // Follow me on twitter: https://twitter.com/inancgumus | 
					
						
							| 
									
										
										
										
											2019-06-21 12:18:33 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func runCmd(input string, games []game, byID map[int]game) bool { | 
					
						
							|  |  |  | 	fmt.Println() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cmd := strings.Fields(input) | 
					
						
							|  |  |  | 	if len(cmd) == 0 { | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch cmd[0] { | 
					
						
							|  |  |  | 	case "quit": | 
					
						
							|  |  |  | 		return cmdQuit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case "list": | 
					
						
							|  |  |  | 		return cmdList(games) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case "id": | 
					
						
							|  |  |  | 		return cmdByID(cmd, games, byID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func cmdQuit() bool { | 
					
						
							|  |  |  | 	fmt.Println("bye!") | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func cmdList(games []game) bool { | 
					
						
							|  |  |  | 	for _, g := range games { | 
					
						
							|  |  |  | 		printGame(g) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func cmdByID(cmd []string, games []game, byID map[int]game) bool { | 
					
						
							|  |  |  | 	if len(cmd) != 2 { | 
					
						
							|  |  |  | 		fmt.Println("wrong id") | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	id, err := strconv.Atoi(cmd[1]) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		fmt.Println("wrong id") | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	g, ok := byID[id] | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		fmt.Println("sorry. i don't have the game") | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	printGame(g) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } |