From e7fd00ed1e135ca5f94f7afbb8cae8779bad0ae0 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sun, 24 Sep 2023 12:38:03 -0400 Subject: [PATCH] Adding week3 assignment --- .../week3/assignment/AnimalsFun.go | 42 ++++++++ .../second_course/week3/assignment/animal.go | 92 ++++++++++++++++++ .../week3/assignment/module3activity.go | 95 +++++++++++++++++++ 3 files changed, 229 insertions(+) create mode 100644 coursera/second_course/week3/assignment/AnimalsFun.go create mode 100644 coursera/second_course/week3/assignment/animal.go create mode 100644 coursera/second_course/week3/assignment/module3activity.go diff --git a/coursera/second_course/week3/assignment/AnimalsFun.go b/coursera/second_course/week3/assignment/AnimalsFun.go new file mode 100644 index 0000000..e1a6a41 --- /dev/null +++ b/coursera/second_course/week3/assignment/AnimalsFun.go @@ -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() + } + } +} \ No newline at end of file diff --git a/coursera/second_course/week3/assignment/animal.go b/coursera/second_course/week3/assignment/animal.go new file mode 100644 index 0000000..6372ce1 --- /dev/null +++ b/coursera/second_course/week3/assignment/animal.go @@ -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 \" \"") + continue + } + + if len(splitInputs) < 2 { + fmt.Println("Error user input contains less values than the expected \" \"") + continue + } + ProcessCommand(splitInputs) + } +} diff --git a/coursera/second_course/week3/assignment/module3activity.go b/coursera/second_course/week3/assignment/module3activity.go new file mode 100644 index 0000000..13ff651 --- /dev/null +++ b/coursera/second_course/week3/assignment/module3activity.go @@ -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 = "" + + } + +}