HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Basic Pokedex using if-else statements

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
elsestatementspokedexusingbasic

Problem

Our instructor told us to create a basic Java program using everything we've learned in class thus far (basic prints, selection structures, looping, GUI, arrays, etc) and being a Pokemon fan, I decided to make a basic GUI Pokedex that lets the user search for the details of a particular starter Pokemon.

This program also asks the user if he wants to search by Pokemon type or by region of origin, just in case the user does not know the name of the Pokemon.

Here is the code I came up with and it runs:

```
import javax.swing.*;
public class StarterPokedex {

public static void main(String[] args) {

String choice, desc=null, pokemon=null, searchBy=null, region=null, type=null, pokeNo=null, species=null, habitat=null;

choice = JOptionPane.showInputDialog(null, "Do you know the name of the Starter Pokemon you are looking for? (Yes/No)", "Welcome to the Starter Pokedex!", 3);
if (choice.equalsIgnoreCase("yes")) {
pokemon = JOptionPane.showInputDialog(null, "Enter the name of the Starter Pokemon: ", "Welcome to the Starter Pokedex!", JOptionPane.PLAIN_MESSAGE); }
else if (choice.equalsIgnoreCase("no")) {
searchBy = JOptionPane.showInputDialog(null, "Search by: 'type' or 'region'?", "Welcome to the Starter Pokedex!", 3);

if (searchBy.equalsIgnoreCase("type")) {
type = JOptionPane.showInputDialog(null, "Enter pokemon type: (Fire/Water/Grass)", "Welcome to the Starter Pokedex!", JOptionPane.PLAIN_MESSAGE);

if (type.equalsIgnoreCase("Fire")) {
pokemon = JOptionPane.showInputDialog(null, "Here are the list of Starter Pokemon that are fire-type: \n\nCharmander \nCyndaquil \nTorchic \nChimchar \nTepig \nFennekin \n\nWhich Pokemon would you like to search?", "Fire-type Starter Pokemon", JOptionPane.PLAIN_MESSAGE); }
else if (type.equalsIgnoreCase("Water")) {
pokemon = JOptionPane.showInputDia

Solution

Your first problem that everything takes place in the main method. Much of the user interaction and initialization should be separated out into seperate methods to remove clutter.

Then, you are confusing data with code. You have about a dozen snippets like:

else if (pokemon.equalsIgnoreCase("Totodile")) {
    region="Johto";
    pokeNo="158";
    species="Big Jaw";
    type="Water";
    habitat="Water's-edge";
    desc="Its well-developed jaws are powerful and capable of crushing anything. Even its trainer must be careful.";
}


First, we should create a Pokemon object that represents each Pokemon:

class Pokemon {
    final String name;
    final String region;
    final int    pokeNo;
    final String species;
    final String type;
    final String habitat;
    final String description;

    Pokemon(String name,
            String region,
            int pokeNo,
            String species,
            String type,
            String habitat,
            String description) {
        this.name        = name;
        this.region      = region;
        this.pokeNo      = pokeNo;
        this.species     = species;
        this.type        = type;
        this.habitat     = habitat;
        this.description = description;
    }
}


Such code is rather ugly in Java, but it will make our code cleaner in a moment.

Next, we create a data structure that maps names to Pokemon objects. For now, we will require that proper capitalization of the name is used.

Map pokemonsByName = new HashMap();


then we can populate this data strucure, e.g.

pokemonsByName.put(
    "Totodile",
    new Pokemon(
        "Totodile",
        "Johto",
        158,
        "Big Jaw",
        "Water",
        "Water's-edge",
        "Its well-developed jaws are powerful and capable of crushing anything. "
        + "Even its trainer must be careful."
    )
);


When we have done all of this for all Pokemon, we can query for Pokemons by name:

Pokemon requestedPokemon = pokemonsByName.get(pokemonName);
if (requestedPokemon == null) {
  // no such Pokemon was found
} else {
  // we have found a pokemon, so display it.
}


But this makes our code longer! Why should we do this then? Well, as you add more and more Pokemon, you may not want to recompile your app each time. Instead, you might load all Pokemon stats from a CSV file at startup (there are open source libraries that will help you with this). Now you only have to edit the data file and restart the program in order to support new Pokemons.

Then you can add further improvements. For example, there are only a limited number of Pokemon types. Instead of using a String (which could contain typos), we can use an enum (enumeration) that only supports certain values:

enum PokemonType {
  WATER, FIRE, GRASS;
}


In the Pokemon class, you would then change the type of the type field from String to PokemonType. The constructor would then be invoked like

new Pokemon(
  ...,
  PokemonType.WATER, // instead of the string "Water"
  ...
);

Code Snippets

else if (pokemon.equalsIgnoreCase("Totodile")) {
    region="Johto";
    pokeNo="158";
    species="Big Jaw";
    type="Water";
    habitat="Water's-edge";
    desc="Its well-developed jaws are powerful and capable of crushing anything. Even its trainer must be careful.";
}
class Pokemon {
    final String name;
    final String region;
    final int    pokeNo;
    final String species;
    final String type;
    final String habitat;
    final String description;

    Pokemon(String name,
            String region,
            int pokeNo,
            String species,
            String type,
            String habitat,
            String description) {
        this.name        = name;
        this.region      = region;
        this.pokeNo      = pokeNo;
        this.species     = species;
        this.type        = type;
        this.habitat     = habitat;
        this.description = description;
    }
}
Map<String, Pokemon> pokemonsByName = new HashMap<String, Pokemon>();
pokemonsByName.put(
    "Totodile",
    new Pokemon(
        "Totodile",
        "Johto",
        158,
        "Big Jaw",
        "Water",
        "Water's-edge",
        "Its well-developed jaws are powerful and capable of crushing anything. "
        + "Even its trainer must be careful."
    )
);
Pokemon requestedPokemon = pokemonsByName.get(pokemonName);
if (requestedPokemon == null) {
  // no such Pokemon was found
} else {
  // we have found a pokemon, so display it.
}

Context

StackExchange Code Review Q#37238, answer score: 13

Revisions (0)

No revisions yet.