-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeAnimal.go
More file actions
108 lines (89 loc) · 2.02 KB
/
makeAnimal.go
File metadata and controls
108 lines (89 loc) · 2.02 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
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Animal struct {
species string
food string
locomotion string
noise string
name string
}
type Cow struct {
species "cow"
food "grass"
noise "moo"
locomotion "walk"
}
type Bird struct {
species "bird"
food "worms"
noise "peep"
locomotion "fly"
}
type Snake struct {
food "mice"
locomotion "slither"
noise "hiss"
species "snake"
}
type animal interface {
CreateAnimal()
SeeAnimal()
}
func (a *Animal) Eat() string {
return fmt.Sprintf("This animal eats %s", a.food)
}
func (a *Animal) Move() string {
return fmt.Sprintf("This animal gets around via %s", a.locomotion)
}
func (a *Animal) Speak() string {
return fmt.Sprintf(a.noise)
}
animalData := []Animal{
{species: "cow", food: "grass", locomotion: "walk", noise: "moo"},
{species: "bird", food: "worms", locomotion: "fly", noise: "peep"},
{species: "snake", food: "mice", locomotion: "slither", noise: "hsss"},
}
animals := make([]Animal{})
func main() {
var currentAnimal Animal
var animalList []Animal
for {
fmt.Println(`Enter 'newanimal <animal-type> <animal-name>'
to create a new animal, or 'query <animal-name> <animal-fact>' to get information about an animal`)
reader := bufio.NewReader(os.Stdin)
str, _ := reader.ReadString('\n')
if strings.Contains(str, "newanimal") {
animalCreate := strings.Fields(str)
var newAnimal animal
for _, a := range animalData {
if a.type == animalCreate[1] {
newAnimal = a
newAnimal.name = animalCreate[2]
}
}
animalList = append(animalList, newAnimal)
}
if strings.Contains(str, "query") {
// fetch animal facts
animalQuery := strings.Fields(str)
}
animalQuery := strings.Fields(str)
for _, a := range animals {
if a.name == animalQuery[0] {
currentAnimal = a
if animalQuery[1] == "move" {
fmt.Println(currentAnimal.Move())
} else if animalQuery[1] == "eat" {
fmt.Println(currentAnimal.Eat())
} else {
fmt.Println(currentAnimal.Speak())
}
}
}
}
}