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.
96 lines
1.9 KiB
Go
96 lines
1.9 KiB
Go
// NOT DREW's BUT HAS GOOD INPUT COMMAND PROCESSING
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type Animal struct {
|
|
food string
|
|
locomotion string
|
|
noise string
|
|
}
|
|
|
|
func (a *Animal) Eat() string {
|
|
return a.food
|
|
}
|
|
|
|
func (a *Animal) Move() string {
|
|
return a.locomotion
|
|
}
|
|
|
|
func (a *Animal) Speak() string {
|
|
return a.noise
|
|
}
|
|
|
|
func main() {
|
|
|
|
cow := Animal{"grass", "walk", "moo"}
|
|
bird := Animal{"worms", "fly", "peep"}
|
|
snake := Animal{"mice", "slither", "hsss"}
|
|
|
|
var input string
|
|
var animal string
|
|
var information string
|
|
|
|
for {
|
|
|
|
fmt.Println("Hello. Enter in a line with a space separation an animal (cow, bird, or snake) and the information requested (eat, move or speak): ")
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
scanner.Scan()
|
|
input = scanner.Text()
|
|
|
|
input = strings.TrimSpace(input)
|
|
numberStrings := strings.Fields(input)
|
|
|
|
for _, item := range numberStrings {
|
|
if item == "cow" || item == "bird" || item == "snake" {
|
|
animal = item
|
|
}
|
|
|
|
if item == "eat" || item == "move" || item == "speak" {
|
|
information = item
|
|
}
|
|
}
|
|
|
|
var selectedAnimal *Animal
|
|
|
|
switch animal {
|
|
case "cow":
|
|
selectedAnimal = &cow
|
|
case "bird":
|
|
selectedAnimal = &bird
|
|
case "snake":
|
|
selectedAnimal = &snake
|
|
default:
|
|
fmt.Println("Invalid animal. Please, enter 'cow', 'bird' or 'snake'")
|
|
animal = ""
|
|
information = ""
|
|
continue
|
|
}
|
|
|
|
switch information {
|
|
case "eat":
|
|
fmt.Println("The action for the selected animal is: ", selectedAnimal.Eat())
|
|
case "move":
|
|
fmt.Println("The action for the selected animal is: ", selectedAnimal.Move())
|
|
case "speak":
|
|
fmt.Println("The action for the selected animal is: ", selectedAnimal.Speak())
|
|
default:
|
|
fmt.Println("Invalid action. Please, enter 'eat', 'move' or 'speak'")
|
|
animal = ""
|
|
information = ""
|
|
continue
|
|
}
|
|
|
|
animal = ""
|
|
information = ""
|
|
|
|
}
|
|
|
|
}
|