Adding week3 assignment

drew/sql-it
Drew Bednar 1 year ago
parent ad88f1694a
commit e7fd00ed1e

@ -0,0 +1,42 @@
package main
import (
"fmt"
)
type Animal struct {
food, locomotion, sound string
}
func (v Animal) Eat() {
fmt.Println(v.food)
}
func (v Animal) Move() {
fmt.Println(v.locomotion)
}
func (v Animal) Speak() {
fmt.Println(v.sound)
}
func main() {
m := map[string]Animal{
"cow" : Animal{"grass","walk","moo"},
"bird" : Animal{"worms","fly","peep"},
"snake" : Animal{"mice","slither","hsss"},
}
for{
fmt.Print(">")
an:="0"
ac:="0"
fmt.Scan(&an,&ac)
if ac=="eat"{
m[an].Eat()
} else if ac=="move"{
m[an].Move()
} else if ac=="speak"{
m[an].Speak()
}
}
}

@ -0,0 +1,92 @@
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// shorter declaration option
// type Animal struct {
// food, locomotion, sound string
// }
type Animal struct {
food string
locomotion string
noise string
}
var cow Animal = Animal{"grass", "walk", "moo"}
var bird Animal = Animal{"worms", "fly", "peep"}
var snake Animal = Animal{"mice", "slither", "hiss"}
func (a Animal) Eat() {
fmt.Println(a.food)
}
func (a Animal) Move() {
fmt.Println(a.locomotion)
}
func (a Animal) Speak() {
fmt.Println(a.noise)
}
func ParseUserInput(s string) []string {
noNewLine := strings.Trim(s, "\n")
return strings.Split(noNewLine, " ")
}
func ProcessCommand(cmd []string) {
switch cmd[0] {
case "cow":
PerformAction(cow, cmd[1])
case "bird":
PerformAction(bird, cmd[1])
case "snake":
PerformAction(snake, cmd[1])
default:
fmt.Println("Invalid animal. Options {cow, bird, snake}")
}
}
func PerformAction(v Animal, action string) {
switch action {
case "eat":
v.Eat()
case "move":
v.Move()
case "speak":
v.Speak()
default:
fmt.Println("Invalid action. Options: {eat, move, speak}")
}
}
func main() {
fmt.Println("Welcome to Animal Farm! We have a \"cow\", \"bird\", and \"snake\".")
fmt.Println("You can ask eat animal to \"eat\", \"move\", or \"speak\".")
fmt.Println("Example: >cow move")
inputReader := bufio.NewReader(os.Stdin)
for {
fmt.Printf(">")
userInput, _ := inputReader.ReadString('\n')
splitInputs := ParseUserInput(userInput)
if len(splitInputs) > 2 {
fmt.Println("Error user input contains more values than the expected \"<animal> <action>\"")
continue
}
if len(splitInputs) < 2 {
fmt.Println("Error user input contains less values than the expected \"<animal> <action>\"")
continue
}
ProcessCommand(splitInputs)
}
}

@ -0,0 +1,95 @@
// 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 = ""
}
}
Loading…
Cancel
Save