Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The objective right now: Finally, you will design a third class (e.g., GamesDatabase) that will handle user input, including the name of the input file, what team to get information about, and which
- information to get about that team. There needs to be a method in one of the classes that will compute the winning
- percentage of all the teams in the database and write them out in a text file called
- winning_pct.txt. When you exit the program by typing QUIT, your program will compute these percentages, write them to winning_pct.txt and
- then quit.
- package project2;
- import java.util.Scanner;
- public class GameResult implements Comparable <GameResult>{
- protected String date;
- protected String awayTeam;
- protected int awayTeamScore;
- protected String homeTeam;
- protected int homeTeamScore;
- protected int neutralCourtFlag;
- protected int overtimes;
- /**
- * Constructor for my instance variables
- *
- *
- */
- public GameResult(String date, String awayTeam, int awayTeamScore, String homeTeam, int homeTeamScore, int neutralCourtFlag, int overtimes) {
- this.date = date;
- this.awayTeam = awayTeam;
- this.awayTeamScore = awayTeamScore;
- this.homeTeam = homeTeam;
- this.homeTeamScore = homeTeamScore;
- this.neutralCourtFlag = neutralCourtFlag;
- this.overtimes = overtimes;
- }
- /**
- * Getter methods for instance variables
- */
- public String getDate(){
- return date;
- }
- public String getAwayTeam() {
- return awayTeam;
- }
- public String getHomeTeam() {
- return homeTeam;
- }
- public int getAwayTeamScore() {
- return awayTeamScore;
- }
- public int getHomeTeamScore() {
- return homeTeamScore;
- }
- public int getNeutralCourtFlag() {
- return neutralCourtFlag;
- }
- public int getOvertimes()
- {
- return overtimes;
- }
- public int compareTo(GameResult arg0) {
- return 0;
- }
- /**
- * ToString method for a game's information
- */
- public String toString() {
- String gameStats;
- gameStats = "";
- gameStats = "Date: " + getDate() + " " + getAwayTeam() + ": " + getAwayTeamScore() + " " + getHomeTeam() + ": "
- + getHomeTeamScore() + " Neutral Court Flags: " + getNeutralCourtFlag() + " " + "Overtimes: " + getOvertimes() ;
- return gameStats;
- }
- }
- //beginning of new class
- package project2;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class GameList {
- // neutral court flag, and the number of overtimes.
- private ArrayList<GameResult> _gameList;
- // Default Constructor
- public GameList() {
- _gameList = new ArrayList<GameResult>();
- }
- public void addGame(String date, String awayTeam, int aScore, String homeTeam, int hScore, int nFlag, int ot) {
- GameResult addGame = new GameResult(date, awayTeam, aScore, homeTeam, hScore, nFlag, ot);
- _gameList.add(addGame);
- }
- public double computeWinningPercentage(String team) {
- double gameCount = 0;
- double winCount = 0;
- for (GameResult gameInfo : _gameList) {
- // if the currrent gameInfo Object has the team in its homeTeam or awayTem
- if (team.equals(gameInfo.getHomeTeam()) || team.equals(gameInfo.getAwayTeam())) {
- gameCount++;
- if (team.equals(gameInfo.getHomeTeam()) && gameInfo.getHomeTeamScore() > gameInfo.getAwayTeamScore()) {
- winCount++;
- } else if (team.equals(gameInfo.getAwayTeam())
- && gameInfo.getAwayTeamScore() > gameInfo.getHomeTeamScore()) {
- winCount++;
- }
- else {
- continue;
- }
- }
- }
- System.out.println((winCount/gameCount) *100);
- return (winCount / gameCount) * 100;
- }
- public ArrayList<String> getOpponentList(String team) {
- ArrayList<String> opponentList = new ArrayList<String>();
- // readFromTextFile();
- for (GameResult gameInfo : _gameList) {
- // if (gameInfo.equals(team) && team.equals(gameInfo.getHomeTeam())){ bad code
- if (gameInfo.getAwayTeam().equals(team)) {
- opponentList.add(gameInfo.getHomeTeam() );
- }
- else if (gameInfo.getHomeTeam().equals(team)) {
- opponentList.add(gameInfo.getAwayTeam() );
- } else {
- continue;
- }
- }
- if (opponentList.size() == 0) {
- System.out.print("The team does not exist");
- }
- team.split(",");
- return opponentList;
- }
- public ArrayList <GameResult>getArrayList(){
- return _gameList;
- }
- public static String getOutputFileName() {
- Scanner scan = new Scanner(System.in);
- System.out.println("Please enter the name of the FILE TO WRITE OUTPUT TO");
- scan.close();
- return scan.nextLine();
- }
- /**public void addWinRateToFile(String team) {
- String outFileName = getOutputFileName();
- GameResult newList;
- ArrayList<String>oppList;
- try {
- // Create a PrintWriter object using the FileOutputStream and File
- PrintWriter pw = new PrintWriter(new FileOutputStream(new File(outFileName)));
- System.out.println("The Data that is getting written in file " + outFileName);
- for (GameResult gameInfoObj : newList) {
- pw.println( ); // the presInfoObj.toString() will get invoked
- System.out.println( );
- }
- pw.close(); // close the file resources
- System.out.println("PresidentInfo list has been successfully written to:" + outFileName);
- } catch (FileNotFoundException fnfe) {
- fnfe.printStackTrace();
- }
- /*}
- }
- //beginning of a new class
- package project2;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class GameList {
- // neutral court flag, and the number of overtimes.
- private ArrayList<GameResult> _gameList;
- // Default Constructor
- public GameList() {
- _gameList = new ArrayList<GameResult>();
- }
- public void addGame(String date, String awayTeam, int aScore, String homeTeam, int hScore, int nFlag, int ot) {
- GameResult addGame = new GameResult(date, awayTeam, aScore, homeTeam, hScore, nFlag, ot);
- _gameList.add(addGame);
- }
- public double computeWinningPercentage(String team) {
- double gameCount = 0;
- double winCount = 0;
- for (GameResult gameInfo : _gameList) {
- // if the currrent gameInfo Object has the team in its homeTeam or awayTem
- if (team.equals(gameInfo.getHomeTeam()) || team.equals(gameInfo.getAwayTeam())) {
- gameCount++;
- if (team.equals(gameInfo.getHomeTeam()) && gameInfo.getHomeTeamScore() > gameInfo.getAwayTeamScore()) {
- winCount++;
- } else if (team.equals(gameInfo.getAwayTeam())
- && gameInfo.getAwayTeamScore() > gameInfo.getHomeTeamScore()) {
- winCount++;
- }
- else {
- continue;
- }
- }
- }
- System.out.println((winCount/gameCount) *100);
- return (winCount / gameCount) * 100;
- }
- public ArrayList<String> getOpponentList(String team) {
- ArrayList<String> opponentList = new ArrayList<String>();
- // readFromTextFile();
- for (GameResult gameInfo : _gameList) {
- // if (gameInfo.equals(team) && team.equals(gameInfo.getHomeTeam())){ bad code
- if (gameInfo.getAwayTeam().equals(team)) {
- opponentList.add(gameInfo.getHomeTeam() );
- }
- else if (gameInfo.getHomeTeam().equals(team)) {
- opponentList.add(gameInfo.getAwayTeam() );
- } else {
- continue;
- }
- }
- if (opponentList.size() == 0) {
- System.out.print("The team does not exist");
- }
- team.split(",");
- return opponentList;
- }
- public ArrayList <GameResult>getArrayList(){
- return _gameList;
- }
- public static String getOutputFileName() {
- Scanner scan = new Scanner(System.in);
- System.out.println("Please enter the name of the FILE TO WRITE OUTPUT TO");
- scan.close();
- return scan.nextLine();
- }
- public void addWinRateToFile(String team) {
- String outFileName = getOutputFileName();
- GameResult newList;
- ArrayList<String>oppList;
- try {
- // Create a PrintWriter object using the FileOutputStream and File
- PrintWriter pw = new PrintWriter(new FileOutputStream(new File(outFileName)));
- System.out.println("The Data that is getting written in file " + outFileName);
- for (GameResult gameInfoObj : newList) {
- pw.println( ); // the presInfoObj.toString() will get invoked
- System.out.println( );
- }
- pw.close(); // close the file resources
- System.out.println("PresidentInfo list has been successfully written to:" + outFileName);
- } catch (FileNotFoundException fnfe) {
- fnfe.printStackTrace();
- }
- }
- }
- //Beginning of a new class
- package project2;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.StringTokenizer;
- import textfile.PresidentInfo;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- public class GamesDatabase {
- public static GameList gList;
- public static ArrayList<GameResult> infoList;
- /**
- *
- private ArrayList<GameResult> _gameList;
- // Default Constructor
- public GameList() {
- _gameList = new ArrayList<GameResult>();
- }
- * @param args
- * @throws FileNotFoundException
- */
- public static void main(String args[]) throws FileNotFoundException {
- readFromTextFile();
- userAsk();
- writeIntoTextFile();
- }
- //This method will tokenize the variables needed the scores list file
- public static void processOneGameInfo(String strGameInfo, GameList myList) throws Exception {
- //declaring a new tokenizer object that tokenizes string with a delimiter of commas
- StringTokenizer tokenizer = new StringTokenizer(strGameInfo, ",");
- //if the file does not even have the same amount of tokens required for the information of a game
- if (tokenizer.countTokens() != 7) {
- System.out.println(strGameInfo);
- throw new Exception("Unexpected number of fields in a record: " + tokenizer.countTokens());
- }
- //converting Strings into tokens and parsing Integer type data into regular integers and tokenizing them
- String date = tokenizer.nextToken();
- String awayTeam = tokenizer.nextToken();
- int awayTeamScore = Integer.parseInt(tokenizer.nextToken());
- String homeTeam = tokenizer.nextToken();
- int homeTeamScore = Integer.parseInt(tokenizer.nextToken());
- int neutralCourtFlag = Integer.parseInt(tokenizer.nextToken());
- int overtimes = Integer.parseInt(tokenizer.nextToken());
- myList.addGame(date, awayTeam, awayTeamScore, homeTeam, homeTeamScore, neutralCourtFlag, overtimes);
- }
- public static void readFromTextFile() {
- String fileName = getInputFileName();
- GameList newGameList = new GameList();
- try {
- BufferedReader br;
- FileReader fr;
- File f = new File(fileName);
- fr = new FileReader(f);
- br = new BufferedReader(fr);
- String oneGameData;
- do {
- oneGameData = br.readLine();
- if (oneGameData == null) {
- break;
- } else {
- processOneGameInfo(oneGameData, newGameList);
- }
- } while (oneGameData != null);
- br.close();
- // catching filenotfound exception
- } catch (Exception fnfe) {
- System.out.println(fnfe.getMessage());
- // catching IOException
- }
- gList = newGameList;
- }
- private static String getInputFileName() {
- Scanner scan = new Scanner(System.in);
- System.out.println("Please enter the name of the file to read from");
- return scan.next();
- }
- /*
- * Asks user for the name of the output file and returns it as a string
- */
- public static String getOutputFileName() {
- Scanner scan = new Scanner(System.in);
- System.out.println("Please enter the name of the FILE TO WRITE OUTPUT TO");
- return scan.nextLine();
- }
- public static void writeIntoTextFile() {
- //calling the getOutputFileName method as a string in order to
- String outFileName = getOutputFileName();
- GameResult gameInfo;
- try {
- //Creating a PrintWriter object with fileoutputsteam and file
- FileWriter fw = new FileWriter(outFileName, true);
- BufferedWriter bw = new BufferedWriter(fw);
- PrintWriter out = new PrintWriter(bw);
- //enhanced for loop to walk through my GameResult list
- for (GameResult gameInfo1 : infoList) {
- gameInfo1.getAwayTeam();
- //out.println("A string");
- //gameInfoOBj toString will be invoked, this is defined shortly after
- }
- out.println("the text");
- //more code
- out.println("more text");
- //more code
- out.close();
- } catch (IOException fnfe) {
- fnfe.printStackTrace();
- //exception handling left as an exercise for the reader
- }
- // closes file from being edited any further
- System.out.println("The gameInfo list has been successfully written to: " + outFileName);
- }
- /**
- * Appends ArrayList<GameList> into a text file using file writer
- */
- /**public static void appendToTextFile() {
- //calling the getOutputFileName method in order to ask the user what file is needed to edit
- String outFileName = getOutputFileName();
- try {
- FileWriter fw = new FileWriter(outFileName, true); //this basically declares a filewriter that appends
- //to the file
- //tells the user what file they are outputting to.
- System.out.println("The content is being appended to file: " + outFileName);
- for (GameList gameInfo : _writeList) {
- //appends the data in _writeList into toString
- fw.append(gameInfo.toString() + "\n");
- fw.add(getOpponentList(team));
- System.out.println(gameInfo);
- }
- fw.close(); //close in order to flush content to the file
- System.out.println("The content has successfuly been appended to: " +outFileName);
- //catching IO exception and file not found exception
- } catch (FileNotFoundException fnfe) {
- fnfe.printStackTrace();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- * @throws FileNotFoundException
- */
- public static void userAsk() throws FileNotFoundException {
- Scanner scan;
- scan = new Scanner(System.in);
- System.out.print("Enter team name or QUIT");
- String team = scan.next();
- ArrayList<String> oppList = null;
- while (!team.equals("QUIT")) {
- System.out.println(team);
- System.out.println("Enter 1 for opponent list or 2 for winning percentage");
- int choice = scan.nextInt();
- if (choice == 1) {
- oppList = gList.getOpponentList(team);
- System.out.println(team + " has played against: " + oppList);
- } else if (choice == 2) {
- gList.computeWinningPercentage(team);
- // appendToTextFile();
- } else {
- System.out.println("Please enter 1 or 2");
- }
- System.out.print("Enter team name or QUIT");
- team = scan.next();
- }
- }
- /**public void addWinRateToFile(String team) {
- String outFileName = getOutputFileName();
- winRate = new ArrayList<GameList>();
- try {
- // Create a PrintWriter object using the FileOutputStream and File
- PrintWriter pw = new PrintWriter(new FileOutputStream(new File(outFileName)));
- System.out.println("The Data that is getting written in file " + outFileName);
- for (GameList gameInfoObj : winRate ) {
- pw.println( ); // the presInfoObj.toString() will get invoked
- System.out.println( );
- }
- pw.close(); // close the file resources
- System.out.println("PresidentInfo list has been successfully written to:" + outFileName);
- } catch (FileNotFoundException fnfe) {
- fnfe.printStackTrace();
- }
- }
- // }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement