My assignment:
develop a limited application to track the statistics of sports players. For each player, track first and last name, points scored, assists made, and penalty rate. Design and implement an object capable of recording this info. Implement this object as Player. Must allow user to enter new player info for any number of players, store all the info for each player, and allow certain queries to be made on player stats. Application must prompt user to perform operations (Add, Remove, Show, Save, Quit).
I have the most of the program completed. I have an issue with showing the players which basically prints the entire arrayList toString in a formatted manner. It looks like it should work but then again, it's a bit simplistic so why would it?
The other problem I am having is removing a player from the list. I know that arrayList.remove() should be a part of the solution but I don't know how to to find the player by the first and last name stored in each object.
If you need me to post my code for my player object, let me know.
Also if any of this looks like it sucks for whatever reason, please let me know. Although, improvements come after functionality.
This is homework. Please do not write my code for me. Any help otherwise would be greatly appreciated. If I find the answer before somebody posts I will update with my progress.
Here is my code:
package project1_playerstats;
/**
* @author 24x24
*/
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Player> playerList = new ArrayList<Player>();
int records = 0;
userPrompt(playerList, records); // User prompt - Asks for following commands:
// add player, remove player, show players, save, quit
} // end of main method
public static void userPrompt(ArrayList playerList, int records) throws FileNotFoundException {
System.out.println("What operation next? (ADD, REMOVE, SHOW, SAVE, QUIT)? ");
Scanner keyboard = new Scanner(System.in);
String choice = keyboard.next().toUpperCase();
// needs user error check of if != choices print out not recognized, try again
if (choice.equals("ADD")) {
add(playerList, records); // calls add method
} else if (choice.equals("REMOVE")) {
remove(playerList, records); // calls remove method
} else if (choice.equals("SHOW")) {
show(playerList); // calls show method
} else if (choice.equals("SAVE")) {
save(playerList, records); // calls save method
} else if (choice.equals("QUIT")) {
quit(); // calls quit method
}
} // end userPrompt method
public static void add(ArrayList playerList, int records) throws FileNotFoundException {
System.out.println("");
System.out.println("Please enter player's Last Name: ");
Scanner keyboard = new Scanner(System.in);
String lastName = keyboard.next().toUpperCase();
System.out.println("");
System.out.println("Please enter player's First Name: ");
String firstName = keyboard.next().toUpperCase();
System.out.println("");
System.out.prinsome small issuestln("Please enter player's points scored: ");
String pointsScored = keyboard.next().toUpperCase();
System.out.println("");
System.out.println("Please enter player's assists: ");
String assists = keyboard.next().toUpperCase();
System.out.println("");
System.out.println("Please enter " + firstName + " " + lastName + " penalty kick precentage (example 0.25 for 25%) : ");
String penaltyKickRate = keyboard.next().toUpperCase();
Player player = new Player(lastName, firstName, pointsScored, assists, penaltyKickRate);
// adds a new player object to playerList
playerList.add(player);
System.out.println("** PLAYER ADDED **");
// need to make toString work. currently does nothing
records++; // increments a count of the records
player.toString();
userPrompt(playerList, records);
} // end of add method
public static void remove(ArrayList playerList, int records) {
System.out.println("Please enter the last name of player to be removed: ");
System.out.println("");
Scanner keyboard = new Scanner(System.in);
String lastName = keyboard.next().toUpperCase();
System.out.println("");
System.out.println("Please enter the first name of player to be removed: ");
String firstName = keyboard.next().toUpperCase();
playerList.contains(lastName, firstName);
playerList.remove(index);
System.out.println("** REMOVED " + firstName + " " + lastName + " **");
// THIS DOESN'T WORK
// removes a player from the list
// decrements records
records--; // decrements count of records
} // end of remove method
public static void show(ArrayList playerList) {
playerList.toString();
// shows the current player list
} // end of show method
public static void save(ArrayList playerList, int records) throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter file name: ");
String fileName = keyboard.next().toUpperCase();
PrintWriter out = new PrintWriter(fileName);
out.print(playerList);
out.close();
System.out.println("** " + records + " " + " RECORD SAVED TO " + fileName);
userPrompt(playerList, records);
// save the current player list
} // end of save method
public static void quit(){
System.out.println("** GOODBYE! **");
System.exit(0); // ends the program
}
} // end of quit method
} // end of Main class
Thanks again for any help!