I’m a self-taught beginning programmer. I’ve designed a horse racing program that reads in a file that has information on each horse in each race, including information on the horse’s previous 10 races. Then outputs the information into a readable format, kind of like a horse racing form or program.
For example, on Saturday August 10th at Churchill Downs there are 9 races and 8 horses in each race. So, the csv file for that day would contain 72 horses. Each of those horses would also have statistics on their last 10 races.
What I have currently done is read in the csv file to a List. Then I transfer the information contained in the List to a List of Horse objects, which basically stores all the information for each horse.
I feel that the only benefit I’m getting from using a class to store all the information is that it’s easier to manipulate that data as I don’t have to remember indices.
I’m wondering if there’s a better way to design this program? Any insight?
If you think about what’s really going on here, you have horses, races, and a particular horse’s performance in a particular race.
So, if you allow your class design to follow this, you might have something like this:
Once you have a class design you like, you can start thinking about how to store your data to make it easiest to get it into your classes. Instead of one giant csv file with a lot of repeated data, you could have a separate file for each class, so Horses.csv, Races.csv, HorseRaces.csv. Eventually, you may want to upgrade from CSV to a small file-based database.
Ultimately, you’d like to have three lists:
Horses,Races, andHorseRaces. To display these lists, you could loop through yourHorseRaces, and easily grab all the data you need simply by using the properties ofHorseRaceand, subsequently, properties ofRaceandHorse. For example,horseRace.Horse.Name.