سؤال

I'm in the process of developing a stat tracker for Baseball season. I'd like to be able to load an entire schedule ino the program, making each game a relatively simple object.

Creating the game object isn't an issue. What I'm wondering is, what's the best way to implement the schedule? I have, in the team class, an ArrayList schedule.

I need a way to load the schedule info from a text file, loop through it and create a game object for each line. If that's not the best way to create 162 objects efficiently, please let me know.

Cheers

EDIT:

The game class is really very simple:

public class Game implements Serializable{

Date gameDate;

Team team;

public int runsScored, runsAllowed;

public ArrayList<BallPlayer> lineup = new ArrayList<BallPlayer>();
public ArrayList<Pitcher> pitchers = new ArrayList<Pitcher>();
public Pitcher starter;

String opponent;
boolean homeAway;

boolean result;

public Game(Team gTeam, Pitcher gStarter, String gOpponent, String homeOrAway, Date gDate){

    this.team = gTeam;
    this.starter = gStarter;
    this.opponent = gOpponent;
    if(homeOrAway.equalsIgnoreCase("home")){this.homeAway = true;}
    this.runsScored = 0;
    this.runsAllowed = 0;
    gameDate = gDate;

}

public String getOpponent(){return opponent;}
public void setOpponent(String o){this.opponent = o;}

public boolean getHomeAway(){return homeAway;}
public void setHomeAway(String ho){if(ho.equalsIgnoreCase("home")){this.homeAway = true;}else{this.homeAway = false;}}

}

هل كانت مفيدة؟

المحلول

This is a pretty general question but I thought I would give you some thoughts on how I would initially approach this.

For your models, you could start with something like this:

GameVO (id, home team id (TeamVO), away team id (TeamVO), location id, schedule_id)
TeamVO (id, mascot, name, hometown, etc.)
LocationVO (id, city, state, stadium name, etc.)
PlayerVO(id, position, fname, lname, number, team_id, array_of_stat_ids)
StatVO(id, game_id, player_id, base_hits, home_runs, rbi, strike_out, etc)
ScheduleVO(id, location_id, home_team_id, away_team_id, play_time)

For the text file, I would recommend becoming comfortable with Regular Expressions. Regular expressions allow you to read in strings of data and extract data based on the patterns that you specific. Your text file will either be a fixed-length text-file, comma separated values (CSV), or some other format (first make sure you understand how your data is structured). Once you have identified the patterns that you want to extract, create your regex to match on every line and extract the appropriate values. Here is a good place to practice with your Regex

Finally, when seriallizing your objects take a look at seriallizers like XStream for .NET. I liked the Java version of this library as it allows you to quickly turn java objects into XML/json and back again.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top