You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
1.6 KiB
Go
106 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func splitUnpack(s string) (string, string, string) {
|
|
x := strings.Split(s, " ")
|
|
return x[0], x[1], x[2]
|
|
}
|
|
|
|
type Animal interface {
|
|
Eat()
|
|
Move()
|
|
Speak()
|
|
}
|
|
|
|
type Cow struct{}
|
|
|
|
type Bird struct{}
|
|
|
|
type Snake struct{}
|
|
|
|
func (an Snake) Eat() {
|
|
fmt.Println("mice")
|
|
}
|
|
|
|
func (an Snake) Move() {
|
|
fmt.Println("slither")
|
|
}
|
|
|
|
func (an Snake) Speak() {
|
|
fmt.Println("hsss")
|
|
}
|
|
|
|
func (an Bird) Eat() {
|
|
fmt.Println("worms")
|
|
}
|
|
|
|
func (an Bird) Move() {
|
|
fmt.Println("fly")
|
|
}
|
|
|
|
func (an Bird) Speak() {
|
|
fmt.Println("peep")
|
|
}
|
|
|
|
func (an Cow) Eat() {
|
|
fmt.Println("grass")
|
|
}
|
|
|
|
func (an Cow) Move() {
|
|
fmt.Println("walk")
|
|
}
|
|
|
|
func (an Cow) Speak() {
|
|
fmt.Println("moo")
|
|
}
|
|
|
|
func newanimal(i1, i2 string, cages map[string]Animal) {
|
|
switch i2 {
|
|
case "cow":
|
|
cages[i1] = Cow{}
|
|
case "snake":
|
|
cages[i1] = Snake{}
|
|
case "bird":
|
|
cages[i1] = Bird{}
|
|
}
|
|
fmt.Println("Created it!")
|
|
}
|
|
|
|
func query(i1, i2 string, cages map[string]Animal) {
|
|
switch i2 {
|
|
case "speak":
|
|
cages[i1].Speak()
|
|
case "move":
|
|
cages[i1].Move()
|
|
case "eat":
|
|
cages[i1].Eat()
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
var commandtype string
|
|
var input1 string
|
|
var input2 string
|
|
animalCages := make(map[string]Animal)
|
|
|
|
for {
|
|
fmt.Println(">")
|
|
scanner.Scan()
|
|
commandtype, input1, input2 = splitUnpack(scanner.Text())
|
|
if commandtype == "newanimal" {
|
|
newanimal(input1, input2, animalCages)
|
|
} else if commandtype == "query" {
|
|
query(input1, input2, animalCages)
|
|
}
|
|
}
|
|
|
|
}
|