-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimal.go
More file actions
96 lines (85 loc) · 1.54 KB
/
animal.go
File metadata and controls
96 lines (85 loc) · 1.54 KB
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
package main
import (
"fmt"
)
type Animal interface {
Eat()
Move()
Speak()
}
type Cow struct {
}
func (a Cow) Eat(){
fmt.Println("grass")
}
func (a Cow) Move(){
fmt.Println("walk")
}
func (a Cow) Speak(){
fmt.Println("moo")
}
type Bird struct {
}
func (a Bird) Eat(){
fmt.Println("worms")
}
func (a Bird) Move(){
fmt.Println("fly")
}
func (a Bird) Speak(){
fmt.Println("peep")
}
type Snake struct {
}
func (a Snake) Eat(){
fmt.Println("mice")
}
func (a Snake) Move(){
fmt.Println("slither")
}
func (a Snake) Speak(){
fmt.Println("hsss")
}
func main() {
fmt.Println("Create new animal with the newanimal command (e.g.):")
fmt.Println("newanimal milka cow")
fmt.Println("Query existing animals with the comman query (e.g):")
fmt.Println("query milka eat")
m := make(map[string]Animal)
var command string
var param1 string
var param2 string
for {
fmt.Println(">")
fmt.Scanln(&command, ¶m1, ¶m2)
if command == "newanimal" {
error := false
switch param2 {
case "cow": m[param1] = Cow{}
case "snake": m[param1] = Snake{}
case "bird": m[param1] = Bird{}
default: {fmt.Println("Animal type does not exist (only cow, snake or bird allowed")
error = true}
}
if !error{
fmt.Println("Created it!")
}
} else if command == "query" {
a, isin := m[param1]
if isin {
switch param2 {
case "eat":
a.Eat()
case "move":
a.Move()
case "speak":
a.Speak()
default:
fmt.Println("Unknown action")
}
} else {
fmt.Println("Animal not found")
}
}
}
}