I have started my first foray into object oriented programming and I wanted to find out if what I have done here makes sense:
Pokedex.cs
using PokedexLibrary;
using System.ComponentModel;
using System.Diagnostics;
namespace Pokedex
{
public partial class Pokedex : Form
{
BindingList<string> list = new BindingList<string>();
List<string> pokemonNames = new List<string>();
List<Pokemon.PokemonInfo> pokemonData = new List<Pokemon.PokemonInfo>();
Pokemon p = new Pokemon();
public Pokedex()
{
pokemonData = LoadPokemonData();
pokemonNames = GetPokemonNames(pokemonData);
InitializeComponent();
}
private void WireUpList()
{
foreach (var item in filterResults(SearchBox.Text))
{
list.Add(item);
}
ResultsWindow.DataSource = list;
}
private void SearchBox_TextChanged(object sender, EventArgs e)
{
WireUpList();
}
private List<string> GetPokemonNames(List<Pokemon.PokemonInfo> pokemonData)
{
var list = new List<string>();
foreach (var item in pokemonData)
{
list.Add(item.name);
}
return list;
}
private List<Pokemon.PokemonInfo> LoadPokemonData()
{
return p.LoadPokemonData();
}
private List<string> filterResults(string s)
{
List<string> match = new List<string>();
list.Clear();
if (SearchBox.Text.Length >= 1)
{
foreach (var item in pokemonNames)
{
if (item.ToLower().Contains(s.ToLower()))
{
match.Add(item);
}
}
return match;
}
return match;
}
private void ResultsWindow_DoubleClick(object sender, EventArgs e)
{
Pokemon.PokemonInfo result = p.GetSelectedPokemonData(ResultsWindow.SelectedItem.ToString(), pokemonData);
flowLayoutPanel1.Visible = true;
TypeBox.Text = String.Join(Environment.NewLine, result.type);
PokemonStats.Text = String.Join(Environment.NewLine, p.GetStatsList(result));
}
}
}
Pokemon.cs
using Newtonsoft.Json;
using System.ComponentModel;
namespace PokedexLibrary
{
public class Pokemon
{
public class Stats
{
public int HP { get; set; }
public int Attack { get; set; }
public int Defense { get; set; }
public int SpAttack { get; set; }
public int SpDefense { get; set; }
public int Speed { get; set; }
}
public class PokemonInfo
{
public int id { get; set; }
public string name { get; set; }
public List<string> type { get; set; }
public Stats @stats { get; set; }
}
public List<PokemonInfo> LoadPokemonData()
{
List<PokemonInfo> items;
using (StreamReader r = new StreamReader("file.json"))
{
string json = r.ReadToEnd();
items = JsonConvert.DeserializeObject<List<PokemonInfo>>(json);
}
return items;
}
public PokemonInfo GetSelectedPokemonData(string pokemon, List<PokemonInfo> p)
{
foreach (var item in p)
{
if (item.name == pokemon)
{
return item;
}
}
return null;
}
public List<string> GetStatsList(PokemonInfo p)
{
List<string> pStats = new List<string>();
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(p.stats))
{
string name = descriptor.Name;
object value = descriptor.GetValue(p.stats);
pStats.Add(value.ToString() + " = " +name);
}
return pStats;
}
}
}
The basic idea is that it loads up some information that you can then click on to drill down further.
I am wondering if there is a better way of doing the two following things:
In
Pokemon.csI have a Method that accepts an instance ofPokemonInfo, this feels a bit odd as thePokemonInfoclass is defined within the same class as the Method - Is this an indication that the class is doing too much?A lot of what is done is working with a list of classes, what means I am often passing the list of classes, or the classes themselves around as parameters:
pokemonData = LoadPokemonData();
pokemonNames = GetPokemonNames(pokemonData);
Is there a better way of doing this?
Any other comments would also be great!