diff --git a/internal/commands/release.go b/internal/commands/release.go new file mode 100644 index 0000000..c7123e8 --- /dev/null +++ b/internal/commands/release.go @@ -0,0 +1,38 @@ +package commands + +import ( + "errors" + "fmt" + + "codeflow.dananglin.me.uk/apollo/pokedex/internal/poketrainer" +) + +func ReleaseFunc(trainer *poketrainer.Trainer) CommandFunc { + return func(args []string) error { + if args == nil { + return errors.New("the name of the Pokemon has not been specified") + } + + if len(args) != 1 { + return fmt.Errorf( + "unexpected number of Pokemon names: want 1; got %d", + len(args), + ) + } + + pokemonName := args[0] + + if _, caught := trainer.GetPokemonFromPokedex(pokemonName); !caught { + return fmt.Errorf( + "you haven't caught a %s", + pokemonName, + ) + } + + trainer.RemovePokemonFromPokedex(pokemonName) + + fmt.Printf("%s was released back into the wild.\n", pokemonName) + + return nil + } +} diff --git a/internal/poketrainer/trainer.go b/internal/poketrainer/trainer.go index 2cece52..e808119 100644 --- a/internal/poketrainer/trainer.go +++ b/internal/poketrainer/trainer.go @@ -48,6 +48,10 @@ func (t *Trainer) GetPokemonFromPokedex(name string) (pokeapi.Pokemon, bool) { return details, ok } +func (t *Trainer) RemovePokemonFromPokedex(name string) { + delete(t.pokedex, name) +} + func (t *Trainer) ListAllPokemonFromPokedex() { if len(t.pokedex) == 0 { fmt.Println("You have no Pokemon in your Pokedex.") diff --git a/main.go b/main.go index 6577bf3..1e4732f 100644 --- a/main.go +++ b/main.go @@ -63,6 +63,10 @@ func run() { description: "List the names of all the Pokemon in your Pokedex", callback: commands.PokedexFunc(trainer), }, + "release": { + description: "Release a Pokemon back into the wild", + callback: commands.ReleaseFunc(trainer), + }, "visit": { description: "Visit a location area", callback: commands.VisitFunc(client, trainer),