diff --git a/coursera/second_course/week4/assignment/animal_interface.go b/coursera/second_course/week4/assignment/animal_interface.go new file mode 100644 index 0000000..f737746 --- /dev/null +++ b/coursera/second_course/week4/assignment/animal_interface.go @@ -0,0 +1,151 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + "os" + "strings" +) + +type Animal interface { + Eat() + Move() + Speak() +} + +type Cow struct { + name string + food string `default:"grass"` + locomotion string `default:"walk"` + sound string `default:"moo"` +} + +func (a Cow) Eat() { + fmt.Println(a.food) +} + +func (a Cow) Move() { + fmt.Println(a.locomotion) +} +func (a Cow) Speak() { + fmt.Println(a.sound) +} + +type Bird struct { + Name string + Food string `default:"worms"` + Locomotion string `default:"fly"` + Sound string `default:"peep"` +} + +func (a Bird) Eat() { + fmt.Println(a.Food) +} + +func (a Bird) Move() { + fmt.Println(a.Locomotion) +} +func (a Bird) Speak() { + fmt.Println(a.Sound) +} + +type Snake struct { + Name string + Food string `default:"mice"` + Locomotion string `default:"slither"` + Sound string `default:"hsss"` +} + +func (a Snake) Eat() { + fmt.Println(a.Food) +} + +func (a Snake) Move() { + fmt.Println(a.Locomotion) +} +func (a Snake) Speak() { + fmt.Println(a.Sound) +} + +func NewAnimal(name string, animalType string) (Animal, error) { + switch animalType { + case "cow": + return Cow{name, "grass", "walk", "moo"}, nil + case "bird": + return Bird{name, "worms", "fly", "peep"}, nil + case "snake": + return Snake{name, "mice", "slither", "hiss"}, nil + default: + return Cow{}, errors.New(fmt.Sprintf("Error! Failed to create animal of type: %s. Options {cow, bird, snake}.", animalType)) + } +} + +func queryAnimal(animal Animal, query string) { + switch query { + case "eat": + animal.Eat() + case "move": + animal.Move() + case "speak": + animal.Speak() + default: + fmt.Printf("Error! Query parameter: %s invalid. Options: {eat, move, speak}\n", query) + } +} + +// Use the empty interface to map string to Any type +// var animalMap map[string]interface{} +var animalMap map[string]Animal + +func main() { + animalMap = make(map[string]Animal) + scanner := bufio.NewScanner(os.Stdin) + + fmt.Println("\n\nWelcome to your personal animal database. Two commands are available {newanimal, query}\n") + fmt.Println("The \"newanimal\" command takes two parameters (name, animalType) seperated by white space.") + fmt.Println("\"newanimal\" animal types are restricted to {cow, bird, snake}") + fmt.Println("Example: \"newanimal bob cow\"\n") + fmt.Println("The \"query\" command takes two parameters (name, action) seperated by white space.") + fmt.Println("\"query\" actions are restricted to {eat, move, speak}") + fmt.Println("Example: \"query bob eat\"\n") + + for { + fmt.Print(">") + scanner.Scan() + input := scanner.Text() + input = strings.TrimSpace(input) + splitInput := strings.Fields(input) + + // Ensure user command is proper size + if len(splitInput) != 3 { + fmt.Println("Error! Invalid input parameters. Please try again.") + continue + } + + command := splitInput[0] + animalName := splitInput[1] + arg := splitInput[2] + + switch command { + case "newanimal": + userAnimal, err := NewAnimal(animalName, arg) + if err != nil { + fmt.Println(err) + continue + } + animalMap[animalName] = userAnimal + case "query": + // check animal is in datastore + val, present := animalMap[animalName] + if present { + queryAnimal(val, arg) + } else { + fmt.Printf("Error! Failed to find animal %s in database\n", animalName) + } + default: + fmt.Println("Error! Unrecognized user command. Options {newanimal, query}") + } + } + +} diff --git a/coursera/second_course/week4/assignment/animalinfo_poly.go b/coursera/second_course/week4/assignment/animalinfo_poly.go new file mode 100644 index 0000000..631f5f7 --- /dev/null +++ b/coursera/second_course/week4/assignment/animalinfo_poly.go @@ -0,0 +1,105 @@ +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) + } + } + +} diff --git a/coursera/second_course/week4/assignment/newanimal.go b/coursera/second_course/week4/assignment/newanimal.go new file mode 100644 index 0000000..0e42d1d --- /dev/null +++ b/coursera/second_course/week4/assignment/newanimal.go @@ -0,0 +1,116 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +type Animal interface { + Eat() + Move() + Speak() +} + +type Cow struct { +} + +func (cow Cow) Eat() { + fmt.Println("grass") +} + +func (cow Cow) Move() { + fmt.Println("walk") +} + +func (cow Cow) Speak() { + fmt.Println("moo") +} + +type Bird struct { +} + +func (bird Bird) Eat() { + fmt.Println("worms") +} + +func (bird Bird) Move() { + fmt.Println("fly") +} + +func (bird Bird) Speak() { + fmt.Println("peep") +} + +type Snake struct { +} + +func (snake Snake) Eat() { + fmt.Println("mice") +} + +func (snake Snake) Move() { + fmt.Println("slither") +} + +func (snake Snake) Speak() { + fmt.Println("hsss") +} + +func createNewAnimal(animalName string, animalType string, animalMap map[string]Animal) { + switch animalType { + case "cow": + animalMap[animalName] = Cow{} + case "bird": + animalMap[animalName] = Bird{} + case "snake": + animalMap[animalName] = Snake{} + default: + fmt.Println("Invalid animal type") + return + } + fmt.Println("Created it!") +} + +func queryAnimal(animalName string, animalAction string, animalMap map[string]Animal) { + animal, ok := animalMap[animalName] + if !ok { + fmt.Printf("Could not find animal %s\n", animalName) + return + } + switch animalAction { + case "eat": + animal.Eat() + case "move": + animal.Move() + case "speak": + animal.Speak() + } + +} + +func main() { + scanner := bufio.NewScanner(os.Stdin) + animalMap := make(map[string]Animal) + + for { + fmt.Print(">") + var fields []string + if scanner.Scan() { + fields = strings.Fields(scanner.Text()) + if len(fields) != 3 { + fmt.Println("Wrong number of arguments") + continue + } + } + switch fields[0] { + case "newanimal": + createNewAnimal(fields[1], fields[2], animalMap) + case "query": + queryAnimal(fields[1], fields[2], animalMap) + default: + fmt.Println("Invalid command") + } + } +}