-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e8ab81
commit 73c506a
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"./pets" | ||
) | ||
|
||
func describePet(pet pets.Pet) string { | ||
return fmt.Sprintf("%s is %d years old. It's sound is %s", pet.GetName(), pet.GetAge(), pet.GetSound()) | ||
} | ||
|
||
func main() { | ||
reader := bufio.NewReader(os.Stdin) | ||
fmt.Println("Are you a dog person or cat person? (dog/cat)") | ||
petType, _ := reader.ReadString('\n') | ||
petType = strings.Split(petType, "\n")[0] | ||
|
||
pet := pets.PetFactory(petType) | ||
petDescription := describePet(pet) | ||
|
||
fmt.Println(petDescription) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package pets | ||
|
||
type cat struct { | ||
name string | ||
sound string | ||
age int | ||
} | ||
|
||
func (c cat) GetName() string { | ||
return c.name | ||
} | ||
|
||
func (c cat) GetAge() int { | ||
return c.age | ||
} | ||
|
||
func (c cat) GetSound() string { | ||
return c.sound | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package pets | ||
|
||
type dog struct { | ||
name string | ||
sound string | ||
age int | ||
} | ||
|
||
func (d dog) GetName() string { | ||
return d.name | ||
} | ||
|
||
func (d dog) GetAge() int { | ||
return d.age | ||
} | ||
|
||
func (d dog) GetSound() string { | ||
return d.sound | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package pets | ||
|
||
// Pet defines the general structure of all pets | ||
type Pet interface { | ||
GetName() string | ||
GetSound() string | ||
GetAge() int | ||
} | ||
|
||
// PetFactory is a factory that return the pet requested | ||
func PetFactory(petType string) Pet { | ||
if petType == "dog" { | ||
return dog{ | ||
name: "Chester", | ||
age: 2, | ||
sound: "bark", | ||
} | ||
} else if petType == "cat" { | ||
return cat{ | ||
name: "Mr. Buttons", | ||
age: 3, | ||
sound: "meow", | ||
} | ||
} | ||
return nil | ||
} |