-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW4M1.go
113 lines (93 loc) · 1.59 KB
/
W4M1.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// Animal defines the behaviors
type Animal interface {
Eat()
Move()
Speak()
}
// Cow is one specified animal
type Cow struct {
}
// Eat is how a cow eat
func (a Cow) Eat() {
fmt.Println("grass")
}
// Move is how a cow move
func (a Cow) Move() {
fmt.Println("walk")
}
// Speak is how a cow speak
func (a Cow) Speak() {
fmt.Println("moo")
}
// Bird is one specified animal
type Bird struct {
}
// Eat is how a bird eat
func (a Bird) Eat() {
fmt.Println("worms")
}
// Move is how a bird move
func (a Bird) Move() {
fmt.Println("fly")
}
// Speak is how a bird speak
func (a Bird) Speak() {
fmt.Println("peep")
}
// Snake is one specified animal
type Snake struct {
}
// Eat is how a snake eat
func (a Snake) Eat() {
fmt.Println("worms")
}
// Move is how a snake move
func (a Snake) Move() {
fmt.Println("slither")
}
// Speak is how a snake speak
func (a Snake) Speak() {
fmt.Println("hsss")
}
func main() {
reader := bufio.NewReader(os.Stdin)
animals := make(map[string]Animal)
for {
fmt.Print("> ")
input, _ := reader.ReadString('\n')
s := strings.Split(strings.TrimSpace(input), " ")
switch s[0] {
case "newanimal":
switch s[2] {
case "cow":
animals[s[1]] = new(Cow)
case "bird":
animals[s[1]] = new(Bird)
case "snake":
animals[s[1]] = new(Snake)
}
fmt.Println("Created it!")
case "query":
a, ok := animals[s[1]]
if ok {
switch s[2] {
case "eat":
a.Eat()
case "move":
a.Move()
case "speak":
a.Speak()
}
} else {
fmt.Println("Not found!")
}
}
}
}