Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "os"
- "bufio"
- "strings"
- )
- type Animal interface {
- Eat()
- Move()
- Speak()
- }
- type cow struct {}
- type snake struct {}
- type bird struct {}
- func (c cow) Eat() {
- fmt.Printf("grass\n>")
- }
- func (c cow) Speak() {
- fmt.Printf("moo\n>")
- }
- func (c cow) Move() {
- fmt.Printf("walk\n>")
- }
- func (s snake) Eat() {
- fmt.Printf("mice\n>")
- }
- func (s snake) Speak() {
- fmt.Printf("hsss\n>")
- }
- func (s snake) Move() {
- fmt.Printf("slither\n>")
- }
- func (b bird) Eat() {
- fmt.Printf("worms\n>")
- }
- func (b bird) Speak() {
- fmt.Printf("peep\n>")
- }
- func (b bird) Move() {
- fmt.Printf("fly\n>")
- }
- func main() {
- var inS []string
- input := bufio.NewScanner(os.Stdin)
- m := make(map[string]Animal)
- Cow := cow{}
- Bird := bird{}
- Snake := snake{}
- fmt.Print(">")
- for input.Scan() {
- fmt.Printf(">")
- inS = strings.Split(input.Text(), " ")
- switch inS[0] {
- case "newanimal": {
- switch inS[2] {
- case "cow": {
- m[inS[1]] = Cow
- fmt.Printf("Created it!\n>")
- break
- }
- case "bird": {
- m[inS[1]] = Bird
- fmt.Printf("Created it!\n>")
- break
- }
- case "snake": {
- m[inS[1]] = Snake
- fmt.Printf("Created it!\n>")
- break
- }
- default: {
- fmt.Printf("Error. Type again.\n>")
- break
- }
- }
- break
- }
- case "query": {
- switch inS[2] {
- case "eat": {
- m[inS[1]].Eat()
- break
- }
- case "move": {
- m[inS[1]].Move()
- break
- }
- case "speak": {
- m[inS[1]].Speak()
- break
- }
- default: {
- fmt.Printf("Error. Type again.\n>")
- break
- }
- }
- break
- }
- default: {
- fmt.Printf("Error. Type again.\n>")
- break
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement