import java.io.*; import java.util.HashMap; /** * PastGames Class * - Saves and gets back data from past matches * @author Akbar Sherwani * @date 22nd Decemeber * @version 0.1 */ public class PastGames { private HashMap games; // Hashmap of last games public PastGames() throws FileNotFoundException { games = new HashMap(); checkSaved(); } public static void main(String[] args) throws FileNotFoundException { new PastGames(); } /** * checkSaved * Checks if file with past games is there otherwise creates it * @throws FileNotFoundException */ public void checkSaved() throws FileNotFoundException { try { File dir = new File("Past Games"); dir.mkdir(); File file = new File(dir, "gamedata.dat"); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line = ""; while((line = br.readLine()) != null) { // match result save to arraylist getMatch(line); // System.out.println(line); } } // Catches any error conditions catch (IOException e) { // // No File exists // File dir = new File("Past Games"); // dir.mkdir(); // File file = new File(dir, "gamedata.dat"); // PrintWriter out = new PrintWriter(new FileOutputStream(file)); // out.println(""); // out.close(); } } /** * getMatch * gets the indivudual match details * @param line String from file */ public void getMatch(String line) { int split = line.indexOf(","); String wteam = line.substring(0, split); line = line.substring(split + 1); split = line.indexOf(","); String lteam = line.substring(0, split); line = line.substring(split + 1); split = line.indexOf("$"); String smargin = line.substring(0, split); int margin = Integer.parseInt(smargin); // System.out.println(wteam + " won by " + margin + " against " + lteam); Matches a = new Matches(wteam,lteam, margin); String name = wteam + lteam; games.put(name, a); } /** * saveMatch * Saves the indivudual match details * @param wteam winning team string * @param lteam losing team string * @param margin int margin won by */ public void saveMatch(String wteam, String lteam, int margin) { PrintWriter out; try { File dir = new File("Past Games"); dir.mkdir(); File file = new File(dir, "gamedata.dat"); out = new PrintWriter(new FileOutputStream(file)); out.println(wteam +"," + lteam + "," + margin + "$"); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * getMacthes * @param team * @return Matches */ public Matches getResult(String teams) { return games.get(teams); } }