'adding user input to different sub classes according to user input

i just started learning C# and this is my 5th week doing so

users will input the pokemon name hp and exp in order to add a pokemon to a specific subclass

eg name = charmander, will send to the sub class Charmander and have ability = "solar power"

how do i

1.) check and confirm that the pokemon has been sent to the correct subclass and

2.) after sending the input pokemon to the correct subclass, use a for loop to display the pokemon in the list as the following:

if theres 3 pokemons in the list, sort by ascending hp order

==============
pokemon name: charmander
pokemon hp: 20
pokemon exp: 50
==============

==============
pokemon name: pikachu
pokemon hp: 40
pokemon exp: 10
==============

==============
pokemon name: eevee
pokemon hp: 50
pokemon exp: 90
==============

program code:

                Console.Write("enter pokemon name : ");
                string name = Console.ReadLine();

                
                //enters pokemon hp
                Console.Write("enter pokemon HP : ");
                int hp = Convert.ToInt32(Console.ReadLine());

                //enters pokemon EXP 
                Console.Write("enter pokemon EXP : ");
                int exp = Convert.ToInt32(Console.ReadLine());

                //to make sure ability exists in current context
                string ability = "";

                

                //enter name Validation. toupper() changes name to lowercase
                if (name.ToLower() != "charmander" && name.ToLower() != "eevee" && name.ToLower() != "pikachu") {
                    Console.WriteLine("only can add Charmander, Eevee and Pikachu!!");
                }

                //enter hp Validation
                else if (hp <= 0) {
                    Console.WriteLine("HP cannot be below 0!!");
                }

                //enter EXP validation
                else if (exp <= 0) {
                    Console.WriteLine("EXP cannot be below 0!!");
                }

                //after validating name hp and exp, will add the pokemons Name, Hp and EXP to the dictionary
                else {
                            
                    pokemonlist.Add(name.ToString()); //pokemon name

                        if (name.ToLower() == "pikachu") {
                            new Pikachu(name, hp, exp, ability);
                        }

                        if (name.ToLower() == "charmander") {
                            new Charmander(name, hp, exp, ability);
                        }
                        
                        if (name.ToLower() == "eevee") {
                            new Eevee(name, hp, exp, ability);
                        }


                    pokemonlist.Add(hp.ToString()); //pokemon hp, converts to string :v
                    pokemonlist.Add(exp.ToString()); //pokemon exp
                    Console.WriteLine("+++++++++++++++++++++++");
                    Console.WriteLine("Pokemon has been added!");
                    Console.WriteLine("+++++++++++++++++++++++");
                }

classes code:


public class Pokemon{

        public string name {get; set;}
        public string hp {get; set;}
        public string exp {get; set;}
        public string ability {get; set;}
        public string evolveTo {get; set;}
        public Pokemon(string name, int hp, int exp, string ability) {
            
        }
    }

    //child : Parent
    //individual subclasses
    public class Charmander : Pokemon {

        public Charmander(string name, int hp, int exp, string ability):base(name, hp, exp, ability) {
            this.name = "Charmander";
            this.ability = "Solar Power";
            this.evolveTo = "Charmelion";
        
        }
    }
    public class Pikachu : Pokemon {
        public Pikachu(string name, int hp, int exp, string ability):base(name, hp, exp, ability) {
            this.name = "Pikachu";
            this.ability = "Lightning Bolt";
            this.evolveTo = "Raichu";
        }
    }

    public class Eevee : Pokemon {
        public Eevee(string name, int hp, int exp, string ability):base(name, hp, exp, ability) {
            this.name = "Eevee";
            this.ability = "Run Away";
            this.evolveTo = "Flareon";
        }
    }


Solution 1:[1]

Change pokemonlist to a List<Pokemon>.

Then change your code to the following (refactors marked with //===):

Console.Write("enter pokemon name : ");
string name = Console.ReadLine();


//enters pokemon hp
Console.Write("enter pokemon HP : ");
int hp = Convert.ToInt32(Console.ReadLine());

//enters pokemon EXP 
Console.Write("enter pokemon EXP : ");
int exp = Convert.ToInt32(Console.ReadLine());

//to make sure ability exists in current context
string ability = "";


//=== Moved below
//enter name Validation. toupper() changes name to lowercase
//if (name.ToLower() != "charmander" && name.ToLower() != "eevee" && name.ToLower() != "pikachu") {
//    Console.WriteLine("only can add Charmander, Eevee and Pikachu!!");
//}

    //enter hp Validation
if (hp <= 0)
{
    Console.WriteLine("HP cannot be below 0!!");
}

//enter EXP validation
else if (exp <= 0)
{
    Console.WriteLine("EXP cannot be below 0!!");
}

//after validating name hp and exp, will add the pokemons Name, Hp and EXP to the dictionary
else
{
    //=== Dont need these
    //pokemonlist.Add(name.ToString()); //pokemon name

    bool added = false; //=== True when a pokemon has been added to the list
    if (name.ToLower() == "pikachu")
    {
        pokemonlist.Add(new Pikachu(name, hp, exp, ability)); //=== Add the new object directly to the list
        added = true;
    }
    //=== Refactored to else if blocks
    else if (name.ToLower() == "charmander")
    {
        pokemonlist.Add(new Charmander(name, hp, exp, ability));
        added = true;
    }

    else if (name.ToLower() == "eevee")
    {
        pokemonlist.Add(new Eevee(name, hp, exp, ability));
        added = true;
    }
    else
    {
        Console.WriteLine("only can add Charmander, Eevee and Pikachu!!");
    }


    //pokemonlist.Add(hp.ToString()); //pokemon hp, converts to string :v
    //pokemonlist.Add(exp.ToString()); //pokemon exp

    if (added) //=== If a pokemon has been added, print the message
    {
        Console.WriteLine("+++++++++++++++++++++++");
        Console.WriteLine("Pokemon has been added!");
        Console.WriteLine("+++++++++++++++++++++++");
    }
}

Now when you want to loop the added pokemon, simply do the following:

foreach (Pokemon poke in pokemonlist)
{
    Console.WriteLine("==============");
    Console.WriteLine($"pokemon name: {poke.name}");
    Console.WriteLine($"pokemon hp: {poke.hp}");
    Console.WriteLine($"pokemon exp: {poke.exp}");
    Console.WriteLine("==============");
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AlphaDelta