I have a problem with my school project. I´m new to programming and C#. I wanted to make a Pokedex. The idea was that you can search for a Pokemon via Number or Name. And then you get the Pokedex Entry. And now to my problem. I don´t know how to search in a list for a variable, if they not in the same method. And I need a method for the search. The List would be in the Main method.
namespace Pokedex
{
class Program
{
public static void Main(string[] args)
{
Console.ForegroundColor
= ConsoleColor.Gray ;
List<Pokemon> pokemonlist = new List<Pokemon>();
pokemonlist.Add(new Pokemon() { PokemonId = "001", PokemonName = "Blubasaur", PokemonType = "Grass & Poison", PokemonCategory = "Seed Pokémon", PokemonDescription = "It bears the seed of a plant on its back from birth. The seed slowly develops. Researchers are unsure whether to classify Bulbasaur as a plant or animal. Bulbasaur are extremely tough and very difficult to capture in the wild." }) ;
pokemonlist.Add(new Pokemon() { PokemonId = "002", PokemonName = "Ivysaur", PokemonType = "Grass & Poison", PokemonCategory = "Seed Pokémon", PokemonDescription = "The Seed Pokémon, Ivysaur, Bulbasaur's evolved form. The bulb on its back absorbs nourishment and blooms into a large flower." });
pokemonlist.Add(new Pokemon() { PokemonId = "003", PokemonName = "Venusaur", PokemonType = "Grass & Poison", PokemonCategory = "Seed Pokémon", PokemonDescription = "Venusaur, the final form of the Bulbasaur evolution. This Seed Pokémon soaks up the sun's rays as a source of energy." });
pokemonlist.Add(new Pokemon() { PokemonId = "004", PokemonName = "Charmander", PokemonType = "Fire", PokemonCategory = "Lizard Pokémon", PokemonDescription = "Charmander. A flame burns on the tip of its tail from birth. It is said that a Charmander dies if its flame ever goes out." });
pokemonlist.Add(new Pokemon() { PokemonId = "005", PokemonName = "Charmeleon", PokemonType = "Fire", PokemonCategory = "Flame Pokémon", PokemonDescription = "Charmeleon, the Flame Pokémon. It has razor-sharp claws and its tail is exceptionally strong." }) ;
pokemonlist.Add(new Pokemon() { PokemonId = "006", PokemonName = "Charizard", PokemonType = "Fire & Flying", PokemonCategory = "Flame Pokémon", PokemonDescription = "Charizard, the Flame Pokémon. Charizard's powerful flame can melt absolutely anything." });
pokemonlist.Add(new Pokemon() { PokemonId = "007", PokemonName = "Squirtle", PokemonType = "Water", PokemonCategory = "Tiny Turtle Pokémon", PokemonDescription = "Squirtle. This Tiny Turtle Pokémon draws its long neck into its shell to launch incredible water attacks with amazing range and accuracy. The blasts can be quite powerfu" });
pokemonlist.Add(new Pokemon() { PokemonId = "008", PokemonName = "Wartortle", PokemonType = "Water", PokemonCategory = "Turtle Pokémon", PokemonDescription = "Wartortle, the Turtle Pokémon. The evolved form of Squirtle. Its long furry tail is a symbol of its age and wisdom." });
pokemonlist.Add(new Pokemon() { PokemonId = "009", PokemonName = "Blastoise", PokemonType = "Water", PokemonCategory = "Shellfish Pokémon", PokemonDescription = "Blastoise, the Shellfish Pokémon. The evolved form of Wartortle. Blastoise's strength lies in its power, rather than its speed. Its shell is like armor and attacks from the hydro cannons on its back are virtually unstoppable." });
pokemonlist.Add(new Pokemon() { PokemonId = "010", PokemonName = "Caterpie", PokemonType = "Bug", PokemonCategory = "Worm Pokémon", PokemonDescription = "Caterpie, the Worm Pokémon. To repel enemies, Caterpie releases an unpleasant odor from its red antenna. It molts several times while growing." });
pokemonlist.Add(new Pokemon() { PokemonId = "011", PokemonName = "Metapod", PokemonType = "Bug", PokemonCategory = "Cocoon Pokémon", PokemonDescription = "Metapod, the Cocoon Pokémon. Metapod's vulnerable body is surrounded by its hard shell, which protects it while it waits to evolve." });
pokemonlist.Add(new Pokemon() { PokemonId = "012", PokemonName = "Butterfree", PokemonType = "Bug & Flying", PokemonCategory = "Butterfly Pokémon", PokemonDescription = "Butterfree, the Butterfly Pokémon. Butterfree's love of flower nectar draws it to seek out flowers, even if they don't have much pollen." });
pokemonlist.Add(new Pokemon() { PokemonId = "013", PokemonName = "Weedle", PokemonType = "Bug & Poison", PokemonCategory = "Hairy Bug Pokémon", PokemonDescription = "Weedle. The stinger on this Pokémon's head guarantees that any attacker will get the point right where it hurts." });
pokemonlist.Add(new Pokemon() { PokemonId = "014", PokemonName = "Kakuna", PokemonType = "Bug & Poison", PokemonCategory = "Cocoon Pokémon", PokemonDescription = "Kakuna, a transitional stage between Weedle and Beedrill. Kakunas remain inactive until they evolve into deadly Beedrills and hatch." });
pokemonlist.Add(new Pokemon() { PokemonId = "015", PokemonName = "Beedrill", PokemonType = "Bug & Poison", PokemonCategory = "Poison Bee Pokémon", PokemonDescription = "Beedrill, the Poison Bee Pokémon. Beedrill fly quickly and attack using poison stingers on their forelegs and tail." });
pokemonlist.Add(new Pokemon() { PokemonId = "016", PokemonName = "Pidgey", PokemonType = "Normal & Flying", PokemonCategory = "Tiny Bird Pokémon", PokemonDescription = "Pidgey is a Flying Pokémon. Among all the Flying Pokémon, it is the gentlest and easiest to capture. A perfect target for the beginning Pokémon Trainer to test his Pokémon's skills" });
pokemonlist.Add(new Pokemon() { PokemonId = "017", PokemonName = "Pidgeotto", PokemonType = "Normal & Flying", PokemonCategory = "Bird Pokémon", PokemonDescription = "Pidgeotto, an evolved form of the Pidgey. It is armed with sharp claws and dives from the sky to capture its prey. Unlike the more gentle Pidgey, Pidgeotto can be dangerous. Approach with extreme caution." });
Welcome();
AllFilterCategories();
Console.WriteLine("\n Please enter the number of the categorie you want to search in.");
string userInput = Console.ReadLine();
int userInputINT = TryParseINT(userInput);
switch (userInputINT)
{
case 0:
Console.Clear();
Environment.Exit(0);
break;
case 1:
NumberSearching();
break;
case 2:
NameSearching();
break;
default:
Console.WriteLine("Your entry was not recognized");
Console.ReadKey();
Console.Clear();
goto case 1;
}
}
This is my Main method. At the moment the pokedex functions with "if else" statement. But it´s horrible for 151 Pokemons...
Here is the NumberSearching method
public static void NumberSearching()
{
Console.WriteLine("\nEnter the Number of the Pokémon you´re looking for.");
string whichNumber = Console.ReadLine();
whichNumber = whichNumber.ToUpper();
if (whichNumber == "001")
{
Console.ForegroundColor = ConsoleColor.Green;
Pokemon Bisasam = new Pokemon() { PokemonId = "001", PokemonName = "Blubasaur", PokemonType = "Grass & Poison", PokemonCategory = "Seed Pokémon", PokemonDescription = "It bears the seed of a plant on its back from birth. The seed slowly develops. Researchers are unsure whether to classify Bulbasaur as a plant or animal. Bulbasaur are extremely tough and very difficult to capture in the wild." };
Bisasam.AllInfos();
Console.ReadKey();
Console.Clear();
Program.SecondSearch();
}
else if (whichNumber == "002")
{
Console.ForegroundColor = ConsoleColor.Green;
Pokemon Ivysaur = new Pokemon() { PokemonId = "002", PokemonName = "Ivysaur", PokemonType = "Grass & Poison", PokemonCategory = "Seed Pokémon", PokemonDescription = "The Seed Pokémon, Ivysaur, Bulbasaur's evolved form. The bulb on its back absorbs nourishment and blooms into a large flower." };
Ivysaur.AllInfos();
Console.ReadKey();
Console.Clear();
Program.SecondSearch();
}
And here the NameSearching method (almost the same..)
private static void NameSearching()
{
Console.WriteLine("\nEnter the Name of the Pokémon you´re looking for.");
string whichName = Console.ReadLine();
if (whichName == "Bulbasaur")
{
Console.ForegroundColor = ConsoleColor.Green;
Pokemon Blubasaur = new Pokemon() { PokemonId = "001", PokemonName = "Blubasaur", PokemonType = "Grass & Poison", PokemonCategory = "Seed Pokémon", PokemonDescription = "It bears the seed of a plant on its back from birth. The seed slowly develops. Researchers are unsure whether to classify Bulbasaur as a plant or animal. Bulbasaur are extremely tough and very difficult to capture in the wild." };
Blubasaur.AllInfos();
Console.ReadKey();
Console.Clear();
Program.SecondSearch();
}
else if (whichName == "Ivysaur")
{
Console.ForegroundColor = ConsoleColor.Green;
Pokemon Ivysaur = new Pokemon() { PokemonId = "002", PokemonName = "Ivysaur", PokemonType = "Grass & Poison", PokemonCategory = "Seed Pokémon", PokemonDescription = "The Seed Pokémon, Ivysaur, Bulbasaur's evolved form. The bulb on its back absorbs nourishment and blooms into a large flower." };
Ivysaur.AllInfos();
Console.ReadKey();
Console.Clear();
Program.SecondSearch();
}
And the Pokemon Class with methods
class Pokemon
{
public string PokemonName { get; set; }
public string PokemonId { get; set; }
public string PokemonType { get; set; }
public string PokemonCategory { get; set; }
public string PokemonDescription { get; set; }
public void PokemonThings(string _PokemonID, string _PokemonName, string _PokemonType, string _PokemonCategory, string _PokemonDescription)
{
PokemonId = _PokemonID;
PokemonName = _PokemonName;
PokemonType = _PokemonType;
PokemonCategory = _PokemonCategory;
PokemonDescription = _PokemonDescription;
}
public void AllInfos()
{
PokemonDescriptionM();
}
public void PokemonDescriptionM()
{
Console.WriteLine($"\n{PokemonId} | {PokemonName} | {PokemonCategory} | {PokemonType} type Pokemon \n{PokemonDescription}");
}
}
I tried to use find etc. But I dont now how to make it work within other methods.. Is this even possible? Or is there another solution ? (that does not contain 151 if else..) (The Option Menu works fine thats why I didn´t add it)
CodePudding user response:
There is still a lot to learn here, I'll try to concentrate on the essential. You have your List and want to find the correct pokemon given a search parameter.
To do that you can loop through your list until you find the item, that fits your search:
Pokemon foundPokemon = null;
// look into each pokemon object, one after another
for(int index = 0; index < pokemonlist.Count; i )
{
// test if the name matches
if(pokemonlist[index].pokemonName == wichName)
{
// save current pokemon in variable
foundPokemon = pokemonlist[index];
// stop looking because the pokemon is already found
break;
}
}
if(foundPokemon == null)
{
// do something if you did not find the pokemon
}
else
{
// in this case you have the found pokemon, output your info
foundPokemon.AllInfos();
}
There is till a lot more you can do to optimize this. I just hope this points you in the right direction.
CodePudding user response:
As several have pointed out those are not voids, they are methods. It looks like you do not know how to transfer your pokemon list to your search methods. What you need is to transfer your list to your searching methods as a parameter. It will look something like this:
private static void NameSearching(List<Pokemon> list)
And in your main put
NameSearching(pokemonlist);
Now you have access to your list in the search method. You can run through it with a for loop like JS Garcia does below or use Linq like this:
var foundPokemon = (from p in list where p.PokemonName == whichName select p).SingleOrDefault();
if (foundPokemon!=null)
{
foundPokemon.AllInfos();
}