Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package roulette;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- *
- * @author Southern Trendkiller
- */
- public class RouletteGameRecordMapper {
- private Scanner scan;
- private FileReader freader;
- private FileWriter fwriter;
- public RouletteGameRecordMapper()
- {
- }
- public ArrayList<RouletteGameRecord> readRouletteGameRecordsFromFile(String file)
- {
- ArrayList<RouletteGameRecord> ret = new ArrayList<RouletteGameRecord>();
- try {
- freader = new FileReader(file);
- // setup a scanner, use \ as delimiter
- this.scan = new Scanner(freader).useDelimiter("\\s*\\\\s*");
- String date;
- boolean outcome;
- int bet, gain;
- // if the file is empty, we return an empty list.
- if(!scan.hasNext())
- return ret;
- while(true)
- {
- // get all ctor args
- date = scan.next();
- if(scan.next().equals("true"))
- outcome = true;
- else
- outcome = false;
- bet = scan.nextInt();
- gain = scan.nextInt();
- // construct a GameRecord and add it to our Record list
- RouletteGameRecord temp = new RouletteGameRecord(date, outcome, bet, gain);
- ret.add(temp);
- // if we reached EOF, break.
- if(!scan.hasNext())
- break;
- }
- freader.close();
- // catch them Exceptions
- } catch (FileNotFoundException e) {
- // in case the file was not found, we assume there are no records
- // associated with the profile that is being loaded, and thus return null.
- return ret;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return ret;
- }
- public void writeRouletteGameRecordsToFile(String file, ArrayList<RouletteGameRecord> reclist)
- {
- try {
- this.fwriter = new FileWriter(file);
- // ctor niceness
- // public RouletteGameRecord(String date, boolean outcome, int bet, int gain)
- String temp;
- for(RouletteGameRecord rec : reclist)
- {
- temp = rec.getDate() + "\\";
- temp += Boolean.toString(rec.isOutcome()) + "\\";
- temp += Integer.toString(rec.getBet()) + "\\";
- temp += Integer.toString(rec.getGain()) + "\\";
- fwriter.write(temp);
- }
- fwriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("Fatal I/O Exception.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement