Advertisement
DennisBoone2020

project3

Nov 16th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.57 KB | None | 0 0
  1. 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
  2. information to get about that team. There needs to be a method in one of the classes that will compute the winning
  3. percentage of all the teams in the database and write them out in a text file called
  4. winning_pct.txt. When you exit the program by typing QUIT, your program will compute these percentages, write them to winning_pct.txt and
  5. then quit.
  6.  
  7.  
  8. package project2;
  9.  
  10. import java.util.Scanner;
  11.  
  12.  
  13. public class GameResult implements Comparable <GameResult>{
  14. protected String date;
  15. protected String awayTeam;
  16. protected int awayTeamScore;
  17. protected String homeTeam;
  18. protected int homeTeamScore;
  19. protected int neutralCourtFlag;
  20. protected int overtimes;
  21. /**
  22. * Constructor for my instance variables
  23. *
  24. *
  25. */
  26. public GameResult(String date, String awayTeam, int awayTeamScore, String homeTeam, int homeTeamScore, int neutralCourtFlag, int overtimes) {
  27. this.date = date;
  28. this.awayTeam = awayTeam;
  29. this.awayTeamScore = awayTeamScore;
  30. this.homeTeam = homeTeam;
  31. this.homeTeamScore = homeTeamScore;
  32. this.neutralCourtFlag = neutralCourtFlag;
  33. this.overtimes = overtimes;
  34.  
  35. }
  36.  
  37. /**
  38. * Getter methods for instance variables
  39. */
  40.  
  41. public String getDate(){
  42. return date;
  43. }
  44. public String getAwayTeam() {
  45. return awayTeam;
  46. }
  47. public String getHomeTeam() {
  48. return homeTeam;
  49. }
  50. public int getAwayTeamScore() {
  51. return awayTeamScore;
  52. }
  53. public int getHomeTeamScore() {
  54. return homeTeamScore;
  55. }
  56. public int getNeutralCourtFlag() {
  57. return neutralCourtFlag;
  58. }
  59. public int getOvertimes()
  60. {
  61. return overtimes;
  62. }
  63.  
  64.  
  65. public int compareTo(GameResult arg0) {
  66. return 0;
  67. }
  68.  
  69.  
  70. /**
  71. * ToString method for a game's information
  72. */
  73.  
  74. public String toString() {
  75. String gameStats;
  76. gameStats = "";
  77. gameStats = "Date: " + getDate() + " " + getAwayTeam() + ": " + getAwayTeamScore() + " " + getHomeTeam() + ": "
  78. + getHomeTeamScore() + " Neutral Court Flags: " + getNeutralCourtFlag() + " " + "Overtimes: " + getOvertimes() ;
  79. return gameStats;
  80.  
  81. }
  82.  
  83. }
  84.  
  85.  
  86. //beginning of new class
  87.  
  88.  
  89. package project2;
  90.  
  91. import java.io.File;
  92. import java.io.FileNotFoundException;
  93. import java.io.FileOutputStream;
  94. import java.io.PrintWriter;
  95. import java.util.ArrayList;
  96. import java.util.Scanner;
  97.  
  98.  
  99. public class GameList {
  100. // neutral court flag, and the number of overtimes.
  101.  
  102. private ArrayList<GameResult> _gameList;
  103.  
  104. // Default Constructor
  105.  
  106. public GameList() {
  107.  
  108. _gameList = new ArrayList<GameResult>();
  109. }
  110.  
  111. public void addGame(String date, String awayTeam, int aScore, String homeTeam, int hScore, int nFlag, int ot) {
  112. GameResult addGame = new GameResult(date, awayTeam, aScore, homeTeam, hScore, nFlag, ot);
  113. _gameList.add(addGame);
  114. }
  115.  
  116. public double computeWinningPercentage(String team) {
  117.  
  118. double gameCount = 0;
  119. double winCount = 0;
  120.  
  121. for (GameResult gameInfo : _gameList) {
  122. // if the currrent gameInfo Object has the team in its homeTeam or awayTem
  123. if (team.equals(gameInfo.getHomeTeam()) || team.equals(gameInfo.getAwayTeam())) {
  124. gameCount++;
  125. if (team.equals(gameInfo.getHomeTeam()) && gameInfo.getHomeTeamScore() > gameInfo.getAwayTeamScore()) {
  126. winCount++;
  127. } else if (team.equals(gameInfo.getAwayTeam())
  128. && gameInfo.getAwayTeamScore() > gameInfo.getHomeTeamScore()) {
  129. winCount++;
  130. }
  131. else {
  132. continue;
  133. }
  134. }
  135.  
  136. }
  137.  
  138.  
  139. System.out.println((winCount/gameCount) *100);
  140. return (winCount / gameCount) * 100;
  141. }
  142.  
  143. public ArrayList<String> getOpponentList(String team) {
  144.  
  145. ArrayList<String> opponentList = new ArrayList<String>();
  146.  
  147. // readFromTextFile();
  148.  
  149. for (GameResult gameInfo : _gameList) {
  150.  
  151. // if (gameInfo.equals(team) && team.equals(gameInfo.getHomeTeam())){ bad code
  152.  
  153. if (gameInfo.getAwayTeam().equals(team)) {
  154.  
  155. opponentList.add(gameInfo.getHomeTeam() );
  156. }
  157.  
  158. else if (gameInfo.getHomeTeam().equals(team)) {
  159. opponentList.add(gameInfo.getAwayTeam() );
  160.  
  161. } else {
  162. continue;
  163. }
  164.  
  165. }
  166.  
  167. if (opponentList.size() == 0) {
  168. System.out.print("The team does not exist");
  169.  
  170. }
  171. team.split(",");
  172.  
  173. return opponentList;
  174. }
  175.  
  176. public ArrayList <GameResult>getArrayList(){
  177. return _gameList;
  178. }
  179.  
  180. public static String getOutputFileName() {
  181. Scanner scan = new Scanner(System.in);
  182. System.out.println("Please enter the name of the FILE TO WRITE OUTPUT TO");
  183. scan.close();
  184. return scan.nextLine();
  185.  
  186. }
  187.  
  188. /**public void addWinRateToFile(String team) {
  189. String outFileName = getOutputFileName();
  190. GameResult newList;
  191. ArrayList<String>oppList;
  192. try {
  193. // Create a PrintWriter object using the FileOutputStream and File
  194. PrintWriter pw = new PrintWriter(new FileOutputStream(new File(outFileName)));
  195.  
  196. System.out.println("The Data that is getting written in file " + outFileName);
  197. for (GameResult gameInfoObj : newList) {
  198. pw.println( ); // the presInfoObj.toString() will get invoked
  199. System.out.println( );
  200. }
  201. pw.close(); // close the file resources
  202. System.out.println("PresidentInfo list has been successfully written to:" + outFileName);
  203. } catch (FileNotFoundException fnfe) {
  204.  
  205. fnfe.printStackTrace();
  206.  
  207. }
  208.  
  209. /*}
  210. }
  211.  
  212.  
  213. //beginning of a new class
  214.  
  215.  
  216. package project2;
  217.  
  218. import java.io.File;
  219. import java.io.FileNotFoundException;
  220. import java.io.FileOutputStream;
  221. import java.io.PrintWriter;
  222. import java.util.ArrayList;
  223. import java.util.Scanner;
  224.  
  225.  
  226. public class GameList {
  227. // neutral court flag, and the number of overtimes.
  228.  
  229. private ArrayList<GameResult> _gameList;
  230.  
  231. // Default Constructor
  232.  
  233. public GameList() {
  234.  
  235. _gameList = new ArrayList<GameResult>();
  236. }
  237.  
  238. public void addGame(String date, String awayTeam, int aScore, String homeTeam, int hScore, int nFlag, int ot) {
  239. GameResult addGame = new GameResult(date, awayTeam, aScore, homeTeam, hScore, nFlag, ot);
  240. _gameList.add(addGame);
  241. }
  242.  
  243. public double computeWinningPercentage(String team) {
  244.  
  245. double gameCount = 0;
  246. double winCount = 0;
  247.  
  248. for (GameResult gameInfo : _gameList) {
  249. // if the currrent gameInfo Object has the team in its homeTeam or awayTem
  250. if (team.equals(gameInfo.getHomeTeam()) || team.equals(gameInfo.getAwayTeam())) {
  251. gameCount++;
  252. if (team.equals(gameInfo.getHomeTeam()) && gameInfo.getHomeTeamScore() > gameInfo.getAwayTeamScore()) {
  253. winCount++;
  254. } else if (team.equals(gameInfo.getAwayTeam())
  255. && gameInfo.getAwayTeamScore() > gameInfo.getHomeTeamScore()) {
  256. winCount++;
  257. }
  258. else {
  259. continue;
  260. }
  261. }
  262.  
  263. }
  264.  
  265.  
  266. System.out.println((winCount/gameCount) *100);
  267. return (winCount / gameCount) * 100;
  268. }
  269.  
  270. public ArrayList<String> getOpponentList(String team) {
  271.  
  272. ArrayList<String> opponentList = new ArrayList<String>();
  273.  
  274. // readFromTextFile();
  275.  
  276. for (GameResult gameInfo : _gameList) {
  277.  
  278. // if (gameInfo.equals(team) && team.equals(gameInfo.getHomeTeam())){ bad code
  279.  
  280. if (gameInfo.getAwayTeam().equals(team)) {
  281.  
  282. opponentList.add(gameInfo.getHomeTeam() );
  283. }
  284.  
  285. else if (gameInfo.getHomeTeam().equals(team)) {
  286. opponentList.add(gameInfo.getAwayTeam() );
  287.  
  288. } else {
  289. continue;
  290. }
  291.  
  292. }
  293.  
  294. if (opponentList.size() == 0) {
  295. System.out.print("The team does not exist");
  296.  
  297. }
  298. team.split(",");
  299.  
  300. return opponentList;
  301. }
  302.  
  303. public ArrayList <GameResult>getArrayList(){
  304. return _gameList;
  305. }
  306.  
  307. public static String getOutputFileName() {
  308. Scanner scan = new Scanner(System.in);
  309. System.out.println("Please enter the name of the FILE TO WRITE OUTPUT TO");
  310. scan.close();
  311. return scan.nextLine();
  312.  
  313. }
  314.  
  315. public void addWinRateToFile(String team) {
  316. String outFileName = getOutputFileName();
  317. GameResult newList;
  318. ArrayList<String>oppList;
  319. try {
  320. // Create a PrintWriter object using the FileOutputStream and File
  321. PrintWriter pw = new PrintWriter(new FileOutputStream(new File(outFileName)));
  322.  
  323. System.out.println("The Data that is getting written in file " + outFileName);
  324. for (GameResult gameInfoObj : newList) {
  325. pw.println( ); // the presInfoObj.toString() will get invoked
  326. System.out.println( );
  327. }
  328. pw.close(); // close the file resources
  329. System.out.println("PresidentInfo list has been successfully written to:" + outFileName);
  330. } catch (FileNotFoundException fnfe) {
  331.  
  332. fnfe.printStackTrace();
  333.  
  334. }
  335.  
  336. }
  337. }
  338.  
  339. //Beginning of a new class
  340.  
  341.  
  342. package project2;
  343.  
  344. import java.util.ArrayList;
  345. import java.util.Scanner;
  346.  
  347. import java.util.StringTokenizer;
  348.  
  349. import textfile.PresidentInfo;
  350.  
  351. import java.io.BufferedReader;
  352. import java.io.BufferedWriter;
  353. import java.io.File;
  354. import java.io.FileNotFoundException;
  355. import java.io.FileOutputStream;
  356. import java.io.FileReader;
  357. import java.io.FileWriter;
  358. import java.io.IOException;
  359. import java.io.PrintWriter;
  360.  
  361. public class GamesDatabase {
  362. public static GameList gList;
  363. public static ArrayList<GameResult> infoList;
  364.  
  365. /**
  366. *
  367. private ArrayList<GameResult> _gameList;
  368.  
  369. // Default Constructor
  370.  
  371. public GameList() {
  372.  
  373. _gameList = new ArrayList<GameResult>();
  374. }
  375. * @param args
  376. * @throws FileNotFoundException
  377. */
  378.  
  379. public static void main(String args[]) throws FileNotFoundException {
  380. readFromTextFile();
  381. userAsk();
  382. writeIntoTextFile();
  383.  
  384.  
  385. }
  386. //This method will tokenize the variables needed the scores list file
  387. public static void processOneGameInfo(String strGameInfo, GameList myList) throws Exception {
  388.  
  389. //declaring a new tokenizer object that tokenizes string with a delimiter of commas
  390. StringTokenizer tokenizer = new StringTokenizer(strGameInfo, ",");
  391.  
  392. //if the file does not even have the same amount of tokens required for the information of a game
  393. if (tokenizer.countTokens() != 7) {
  394. System.out.println(strGameInfo);
  395. throw new Exception("Unexpected number of fields in a record: " + tokenizer.countTokens());
  396. }
  397. //converting Strings into tokens and parsing Integer type data into regular integers and tokenizing them
  398. String date = tokenizer.nextToken();
  399. String awayTeam = tokenizer.nextToken();
  400. int awayTeamScore = Integer.parseInt(tokenizer.nextToken());
  401. String homeTeam = tokenizer.nextToken();
  402. int homeTeamScore = Integer.parseInt(tokenizer.nextToken());
  403. int neutralCourtFlag = Integer.parseInt(tokenizer.nextToken());
  404. int overtimes = Integer.parseInt(tokenizer.nextToken());
  405.  
  406. myList.addGame(date, awayTeam, awayTeamScore, homeTeam, homeTeamScore, neutralCourtFlag, overtimes);
  407.  
  408. }
  409.  
  410. public static void readFromTextFile() {
  411. String fileName = getInputFileName();
  412. GameList newGameList = new GameList();
  413. try {
  414.  
  415. BufferedReader br;
  416. FileReader fr;
  417. File f = new File(fileName);
  418. fr = new FileReader(f);
  419. br = new BufferedReader(fr);
  420. String oneGameData;
  421.  
  422. do {
  423. oneGameData = br.readLine();
  424. if (oneGameData == null) {
  425. break;
  426. } else {
  427. processOneGameInfo(oneGameData, newGameList);
  428. }
  429. } while (oneGameData != null);
  430. br.close();
  431.  
  432. // catching filenotfound exception
  433.  
  434. } catch (Exception fnfe) {
  435. System.out.println(fnfe.getMessage());
  436. // catching IOException
  437.  
  438. }
  439. gList = newGameList;
  440.  
  441. }
  442.  
  443. private static String getInputFileName() {
  444. Scanner scan = new Scanner(System.in);
  445. System.out.println("Please enter the name of the file to read from");
  446. return scan.next();
  447.  
  448. }
  449.  
  450. /*
  451. * Asks user for the name of the output file and returns it as a string
  452. */
  453. public static String getOutputFileName() {
  454. Scanner scan = new Scanner(System.in);
  455. System.out.println("Please enter the name of the FILE TO WRITE OUTPUT TO");
  456. return scan.nextLine();
  457. }
  458.  
  459. public static void writeIntoTextFile() {
  460. //calling the getOutputFileName method as a string in order to
  461. String outFileName = getOutputFileName();
  462. GameResult gameInfo;
  463.  
  464.  
  465. try {
  466. //Creating a PrintWriter object with fileoutputsteam and file
  467. FileWriter fw = new FileWriter(outFileName, true);
  468. BufferedWriter bw = new BufferedWriter(fw);
  469. PrintWriter out = new PrintWriter(bw);
  470. //enhanced for loop to walk through my GameResult list
  471. for (GameResult gameInfo1 : infoList) {
  472. gameInfo1.getAwayTeam();
  473. //out.println("A string");
  474. //gameInfoOBj toString will be invoked, this is defined shortly after
  475. }
  476. out.println("the text");
  477. //more code
  478. out.println("more text");
  479. //more code
  480. out.close();
  481. } catch (IOException fnfe) {
  482. fnfe.printStackTrace();
  483. //exception handling left as an exercise for the reader
  484. }
  485.  
  486.  
  487. // closes file from being edited any further
  488. System.out.println("The gameInfo list has been successfully written to: " + outFileName);
  489.  
  490.  
  491. }
  492.  
  493. /**
  494. * Appends ArrayList<GameList> into a text file using file writer
  495. */
  496. /**public static void appendToTextFile() {
  497. //calling the getOutputFileName method in order to ask the user what file is needed to edit
  498. String outFileName = getOutputFileName();
  499.  
  500. try {
  501.  
  502. FileWriter fw = new FileWriter(outFileName, true); //this basically declares a filewriter that appends
  503. //to the file
  504.  
  505. //tells the user what file they are outputting to.
  506. System.out.println("The content is being appended to file: " + outFileName);
  507.  
  508. for (GameList gameInfo : _writeList) {
  509. //appends the data in _writeList into toString
  510. fw.append(gameInfo.toString() + "\n");
  511. fw.add(getOpponentList(team));
  512. System.out.println(gameInfo);
  513. }
  514. fw.close(); //close in order to flush content to the file
  515. System.out.println("The content has successfuly been appended to: " +outFileName);
  516.  
  517. //catching IO exception and file not found exception
  518. } catch (FileNotFoundException fnfe) {
  519. fnfe.printStackTrace();
  520.  
  521. } catch (IOException ioe) {
  522. ioe.printStackTrace();
  523. }
  524. }
  525. * @throws FileNotFoundException
  526. */
  527.  
  528. public static void userAsk() throws FileNotFoundException {
  529.  
  530. Scanner scan;
  531. scan = new Scanner(System.in);
  532. System.out.print("Enter team name or QUIT");
  533. String team = scan.next();
  534. ArrayList<String> oppList = null;
  535.  
  536. while (!team.equals("QUIT")) {
  537. System.out.println(team);
  538. System.out.println("Enter 1 for opponent list or 2 for winning percentage");
  539. int choice = scan.nextInt();
  540.  
  541. if (choice == 1) {
  542. oppList = gList.getOpponentList(team);
  543. System.out.println(team + " has played against: " + oppList);
  544.  
  545. } else if (choice == 2) {
  546. gList.computeWinningPercentage(team);
  547.  
  548. // appendToTextFile();
  549. } else {
  550. System.out.println("Please enter 1 or 2");
  551.  
  552. }
  553.  
  554. System.out.print("Enter team name or QUIT");
  555. team = scan.next();
  556.  
  557. }
  558.  
  559.  
  560. }
  561.  
  562.  
  563. /**public void addWinRateToFile(String team) {
  564. String outFileName = getOutputFileName();
  565.  
  566. winRate = new ArrayList<GameList>();
  567. try {
  568. // Create a PrintWriter object using the FileOutputStream and File
  569. PrintWriter pw = new PrintWriter(new FileOutputStream(new File(outFileName)));
  570.  
  571. System.out.println("The Data that is getting written in file " + outFileName);
  572. for (GameList gameInfoObj : winRate ) {
  573. pw.println( ); // the presInfoObj.toString() will get invoked
  574. System.out.println( );
  575. }
  576. pw.close(); // close the file resources
  577. System.out.println("PresidentInfo list has been successfully written to:" + outFileName);
  578. } catch (FileNotFoundException fnfe) {
  579.  
  580. fnfe.printStackTrace();
  581.  
  582. }
  583.  
  584. }
  585.  
  586. // }
  587. */
  588.  
  589. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement