Hi I am writing a program that tracks the statistics of sport players. Right now I have it where you can add players, remove players, save players, sort the list, or save it. I need to add another function that loads a file. The save function prompts the user to enter the filename for which the players stats will be saved to, then it is saved. I need a load function that prompts the user for the file name to be loaded and then it loads the stats. Here are the exact instructions.
LOAD: Prompts the user for a filename that was created by a previous SAVE operation. The players represented in the SAVE file that do not already exist in the current list of players are added to the current list of players in the application. Each player may be assumed to have a unique name. Note, neither first nor last names alone can be assumed to be unique, but the combination of both first and last name can be assumed to be unique. E.g., Jane Doe, Jane Smith, and John Doe are all valid unique names for players. 5. Each player class must implement a new constructor that takes a single formatted string as its parameter. This constructor should, in essence, create an object with the appropriate instanced information from a string produced previously by its own toString() method. Use these constructors in your LOAD implementations.
Thank you so much for your help. I really need help to get this done as this project is due on monday for me.
My code right now looks like this.
/*
A limited application that tracks the statistics of players. For each player,
the application tracks the players scores for their given sport.
*/
package project4walker;
import java.io.*;
import java.util.*;
import java.util.zip.GZIPInputStream;
/**
Cassandra Walker
Mr. Ondrasek
Project 4
Katie Timmerman
*/
public class Main {
public static void main(String[] args) throws Exception { // Scanner is created Scanner input = new Scanner(System.in); // Creates the array list, type player, name soccer ArrayList<PlayerStats> list = new ArrayList<PlayerStats>(); // the boolean is created boolean start = true; // The while loop will only work if the boolean is true while (start == true) { // Prompts the user for which operation they wish to make System.out.println("What operation next? (ADD, REMOVE, SHOW, SAVE, SORT, LOAD, QUIT)"); String userInput = input.next(); // If the user chooses add this loop is started, if (userInput.equalsIgnoreCase("ADD")) { add(list, input); } // end add if else if (userInput.equalsIgnoreCase("REMOVE")) { remove(list, input); }// end remove if else if (userInput.equalsIgnoreCase("SHOW")) { show(list); }// end show if else if (userInput.equalsIgnoreCase("SORT")) { list = sort(list); }// end sort if else if (userInput.equalsIgnoreCase("SAVE")) { save(list, input); }// end save if else if (userInput.equalsIgnoreCase("QUIT")) { // goodbye is printed out System.out.println("** Goodbye! **"); // The boolean is now false and the program is stopped start = false; // If the user chooses anything besides what is available then an error message is printed } else { System.out.println("Operation not recognized. Please choose from the available list."); } }// end of boolean }//end of method public static void add(ArrayList list, Scanner input) { boolean run = true; // beginning of the while loop and the boolean is true while (run) { System.out.println("Please enter Sport (Soccer, Basketball, Baseball)"); String charInput = input.next(); // if, if the user inputs soccer as the sport if (charInput.equalsIgnoreCase("SOCCER")) { PlayerStats newPlayer = new SoccerPlayerStats(); // gets the values that are prompted for in the players stats class newPlayer.getsValuesFromConsole(); System.out.println(newPlayer + "\n"); // adds the new player to the array list list.add(newPlayer); // the boolean is now false so that loop can be exited run = false; }// end soccer add if // if, if the user inputs basketball for the sport else if (charInput.equalsIgnoreCase("Basketball")) { PlayerStats newPlayer = new BasketballPlayerStats(); // gets the values that the user is prompted for in the player stats class newPlayer.getsValuesFromConsole(); System.out.println(newPlayer + "\n"); // add the new player to the array list list.add(newPlayer); //boolean is now false so that the loop can be exited run = false; }// end basketball add if // if, if the user inputs baseball for the sport else if (charInput.equalsIgnoreCase("BASEBALL")) { PlayerStats newPlayer = new BaseballPlayerStats(); // the user is prompted for the values newPlayer.getsValuesFromConsole(); System.out.println(newPlayer + "\n"); //the player is added to the array list list.add(newPlayer); // the boolean is now false so that the loop can be exited run = false; }// end baseball add if // if the user did not choose a correct sport an error message is printed else { System.out.println("Please choose a sport"); }// end of else loop if the user inputs the wrong sport or nothing }// end of while loop }// end of add method public static void remove(ArrayList<PlayerStats> list, Scanner input) { // the user is prompted for the players last name to be removed System.out.println("Please enter last name of player to remove"); String lastName = input.next(); // the user is prompted for the players first name to be removed System.out.println("Please enter first name of player to remove"); String firstName = input.next(); // if the list is empty then an error message is printed if (list.isEmpty()) { System.out.println("No players in the list"); }// end list empty if // the player is being removed for (PlayerStats removeStats : list) { if (lastName.equalsIgnoreCase(removeStats.getslastName()) && firstName.equalsIgnoreCase(removeStats.getsfirstName())) { list.remove(removeStats); System.out.println("** REMOVED " + firstName + " " + lastName + " **"); break; // if the player does not exist then an error message is printed } else { System.out.println("Player not found"); }// end remove player if }// end remove for loop }// end remove method public static void show(ArrayList<PlayerStats> list) { System.out.println("** SHOW ALL PLAYERS **"); // shows all the players in the list for (PlayerStats show : list) { System.out.println(show); }// end show for loop // gives the numbers of records that are in the array System.out.println("Total Records: " + list.size()); //creates the variables for all of the sports int pointsScored = 0; int assists = 0; double freeThrows = 0; int basketballCounter = 0; int pointsScored2 = 0; int assists2 = 0; double penaltyKickRate = 0; int soccerCounter = 0; int hits = 0; int homeruns = 0; double hitRate = 0.0; int baseballCounter = 0; // shows the averages for the basketball stats for (PlayerStats showAverages : list) { if (showAverages instanceof BasketballPlayerStats) { BasketballPlayerStats showBasketball = (BasketballPlayerStats) showAverages; pointsScored += showBasketball.getsPointsScored(); assists += showBasketball.getsAssists(); freeThrows += showBasketball.getFreeThrows(); basketballCounter++; }// end basketball instance if // shows the averages for the basketball stats if (showAverages instanceof SoccerPlayerStats) { SoccerPlayerStats showSoccer = (SoccerPlayerStats) showAverages; pointsScored2 += showSoccer.getsPointsScored(); assists2 += showSoccer.getsAssists(); penaltyKickRate += showSoccer.getPenaltyKickRate(); soccerCounter++; }// end basketball instance if // shows the averages for the baseball stats if (showAverages instanceof BaseballPlayerStats) { BaseballPlayerStats showBaseball = (BaseballPlayerStats) showAverages; hits += showBaseball.getHits(); homeruns += showBaseball.getHomeruns(); hitRate += showBaseball.getHitRate(); baseballCounter++; }// end basketball instance if }// end averages for loop //starts the if loop if the basketballCounter that was created in the earlier code and prints out the averages if (basketballCounter > 0) { System.out.printf("Basketball Averages for %d players\t points: %d " + " assists: %d free throw percentage: %.2f\n", (basketballCounter), (pointsScored / basketballCounter), (assists / basketballCounter), (freeThrows / basketballCounter)); }// end basketball print if //starts the if loop if the soccerCounter that was created in the earlier code and prints out the averages if (soccerCounter > 0) { System.out.printf("Soccer Averages for %d players\t points: %d " + " assists: %d kick rate: %.2f\n", (soccerCounter), (pointsScored2 / soccerCounter), (assists2 / soccerCounter), (penaltyKickRate / soccerCounter)); }// end soccer print if //starts the if loop if the baseballCounter that was created in the earlier code and prints out the averages if (baseballCounter > 0) { System.out.printf("Baseball Averages for %d players\t hits: %d " + " homeruns: %d bating average: %.2f\n", (baseballCounter), (hits / baseballCounter), (homeruns / baseballCounter), (hitRate / baseballCounter)); }// end basketball print if }// end show method public static ArrayList sort(ArrayList<PlayerStats> list) { // creates a new array for the new sorted list ArrayList<PlayerStats> sortedList = new ArrayList<PlayerStats>(); // if the list is empty then an error message is printed if (list.isEmpty()) { System.out.println("There are no players in the list"); } // if the list is not empty then the while loop is started while (!list.isEmpty()) { // creates the variable for the indexValue when the list is sorted int indexValues = 0; // creates the first player PlayerStats player1 = (PlayerStats) list.get(0); for (int i = 1; i < list.size(); i++) { // creates the second player PlayerStats player2 = (PlayerStats) list.get(i); // creates the variable that will compare the last names of the players int compareLast = (player1.getslastName().compareToIgnoreCase(player2.getslastName())); // if loop that compares the last names of the players if (compareLast > 0) { player1 = player2; indexValues = i; // else if that compares the first names of the players if the last name is the same } else if (compareLast == 0) { // creates the variable that compares the first name of the players int compareFirst = (player1.getsfirstName().compareToIgnoreCase(player2.getsfirstName())); // if that compares the first names if (compareFirst > 0) { player1 = player2; indexValues = 0; }//end of if for compare first names }//end of else if last names are the same }// end of for that goes through the list of players // add the players to the new sorted list sortedList.add(player1); // removes the indexValue for the list list.remove(indexValues); }// end of while is the list is not empty // message printed if the sort was successfule System.out.println("The sort was successful."); // the sortedList is returned to the main method return sortedList; }// end of sort method public static void save(ArrayList<PlayerStats> list, Scanner input) throws Exception { // prompts the user to enter the file name System.out.println("Please enter file name"); String fileName = input.next(); // creates the file File file = new File(fileName); // creates the printwriter that will write to the file PrintWriter pw = new PrintWriter(file); // for loop that will save the list to the file for (PlayerStats save : list) { pw.print(save); }// end show for loop // prints the total number of records to the file pw.print("Total Records: " + list.size()); // creates the variables int pointsScored = 0; int assists = 0; double freeThrows = 0; int basketballCounter = 0; int pointsScored2 = 0; int assists2 = 0; double penaltyKickRate = 0; int soccerCounter = 0; int hits = 0; int homeruns = 0; double hitRate = 0.0; int baseballCounter = 0; // for loop that saves all of the averages to the file for (PlayerStats saveAverages : list) { // if that gets all of the values for the variables if (saveAverages instanceof BasketballPlayerStats) { BasketballPlayerStats showBasketball = (BasketballPlayerStats) saveAverages; // gets the values for the pointsScored pointsScored += showBasketball.getsPointsScored(); // gest the values for the assists assists += showBasketball.getsAssists(); // gets the values for the free throws freeThrows += showBasketball.getFreeThrows(); //basketballCounter is incremented basketballCounter++; }// end basketball instance if // if that saves the averages of the soccer to the file if (saveAverages instanceof SoccerPlayerStats) { SoccerPlayerStats showSoccer = (SoccerPlayerStats) saveAverages; // gets the values for the points socred pointsScored2 += showSoccer.getsPointsScored(); // gets the values for the assists assists2 += showSoccer.getsAssists(); // gets the values for the penalty kick rate penaltyKickRate += showSoccer.getPenaltyKickRate(); // soccerCounter is incremented soccerCounter++; }// end basketball instance if // if the saves all of the baseball average to the file if (saveAverages instanceof BaseballPlayerStats) { BaseballPlayerStats showBaseball = (BaseballPlayerStats) saveAverages; //gets the values for the hits hits += showBaseball.getHits(); // gets the values for the home runs homeruns += showBaseball.getHomeruns(); // gets the values for the hit rate hitRate += showBaseball.getHitRate(); // baseballCounter is incremented baseballCounter++; }// end basketball instance if }// end averages for loop // if the basketballCounter is greater than 0 then the averages is saved to the file if (basketballCounter > 0) { pw.printf("\nBasketball Averages for %d players\t points: %d " + " assists: %d free throw percentage: %.2f\n", (basketballCounter), (pointsScored / basketballCounter), (assists / basketballCounter), (freeThrows / basketballCounter)); }// end basketball print if // if the soccerCounter is greater than 0 then the averages is saved to the file if (soccerCounter > 0) { pw.printf("\nSoccer Averages for %d players\t points: %d " + " assists: %d kick rate: %.2f\n", (soccerCounter), (pointsScored2 / soccerCounter), (assists2 / soccerCounter), (penaltyKickRate / soccerCounter)); }// end soccer print if // if the baseballCounter is greater than 0 than the averages is saved to the file if (baseballCounter > 0) { pw.printf("\nBaseball Averages for %d players\t hits: %d " + " homeruns: %d bating average: %.2f\n", (baseballCounter), (hits / baseballCounter), (homeruns / baseballCounter), (hitRate / baseballCounter)); }// end basketball print if // the file is closed pw.close(); // message is printed that the record was saved System.out.println("**" + list.size() + " RECORD(S) SAVED TO " + fileName + " **/n"); }// end save method
}//end of class
Ads