-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek3.go
78 lines (71 loc) · 1.37 KB
/
week3.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Animal struct {
food string
locomotion string
noise string
}
func (a *Animal) Eat() {
fmt.Println(a.food)
}
func (a *Animal) Move() {
fmt.Println(a.locomotion)
}
func (a *Animal) Speak() {
fmt.Println(a.noise)
}
func initAnimalObjects() (map[string]*Animal){
cow := &Animal{
food: "grass",
locomotion: "walk",
noise: "moo",
}
bird := &Animal{
food: "worms",
locomotion: "fly",
noise: "peep",
}
snake := &Animal{
food: "mice",
locomotion: "slither",
noise: "hsss",
}
m := make(map[string]*Animal)
m["cow"] = cow
m["bird"]= bird
m["snake"] = snake
return m
}
func main() {
objectMap := initAnimalObjects()
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Println("select a animal from cow,bird,snake.")
fmt.Print("> ")
scanner.Scan()
object := strings.ToLower(scanner.Text())
if _,ok:=objectMap[object];!ok{
fmt.Println("animal not selected from above list")
break
}
fmt.Println("select option to know about animal - eat,move,speak.")
fmt.Print("> ")
scanner.Scan()
action := strings.ToLower(scanner.Text())
switch action {
case "eat":
objectMap[object].Eat()
case "move":
objectMap[object].Move()
case "speak":
objectMap[object].Speak()
default:
fmt.Println("action not selected from eat,move,speak.")
}
}
}