public class Project1Cadenhead {
public static void main(String[] args) throws IOException {
boolean loop = true;
Scanner keyboard = new Scanner(System.in);
ArrayList<Player> list = new ArrayList<Player>();
Player newPlayer = null;
while (loop == true) {
System.out.println("What operation next? (ADD, REMOVE, SHOW, SAVE, QUIT)?");
String entry = keyboard.next();
if (entry.equalsIgnoreCase("add")) {
newPlayer = new Player();
System.out.println("Please enter player’s Last name:");
String lastName = keyboard.next();
newPlayer.setLastName(lastName);
System.out.println("Please enter player’s First name:");
String firstName = keyboard.next();
newPlayer.setFirstName(firstName);
System.out.println("Please enter number of points scored for " + firstName + " " + lastName + ":");
Integer pointsScored = keyboard.nextInt();
newPlayer.setPointsScored(pointsScored);
System.out.println("Please enter number of assists for " + firstName + " " + lastName + ":");
Integer assists = keyboard.nextInt();
newPlayer.setAssists(assists);
System.out.println("Please enter penalty kick percentage (example 0.25 for 25%) for " +firstName+ " " + lastName + ":");
double kickRate = keyboard.nextDouble();
newPlayer.setPenaltyKickRate(kickRate);
list.add(newPlayer);
System.out.println("** PLAYER ADDED **\n");
System.out.println(newPlayer.toString());
} else if (entry.equalsIgnoreCase("remove")) {
System.out.println("Please enter last name of player to remove:");
String lastName = keyboard.next();
System.out.println("Please enter first name of player to remove");
String firstName = keyboard.next();
for (int i = 0; i < list.size(); i++) {
list.remove(i);
}
System.out.println("**REMOVED " + firstName + " " + lastName + "**");
} else if (entry.equalsIgnoreCase("show")) {
System.out.println(" ** SHOW ALL PLAYERS **: ");
if (list.isEmpty()) {
System.out.println("There are currently no players in the list. Please add players to the list.");
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
System.out.println("Averages\t" + "Points: " + averagePointsScored(list) + " Assists: " + averageAssists(list)
+ " Penalty Kick %: " + averagePenaltyKicks(list));
System.out.println("Totals:" + list.size());
}
} else if (entry.equalsIgnoreCase("save")) {
File file = new File("PlayerData.txt");
PrintWriter output = new PrintWriter(file);
for (int i = 0; i < list.size(); i++) {
output.println(list.get(i).toString());
}
output.println("Totals:" + list.size());
output.println("Averages:\t" + "Points: " + averagePointsScored(list) + " Assists: " + averageAssists(list)
+ " Penalty Kick %: " + averagePenaltyKicks(list));
output.close();
System.out.println("** " + list.size() + " RECORD SAVED TO Playerdata.txt **");
} else if (entry.equalsIgnoreCase("quit")) {
loop = false;
System.out.println("** GOODBYE! **");
} else {
System.out.println("Operation not recognized. Please choose from the available list.\n");
loop = true;
}
}
}
/** This method will get the points scored from the list and calculate the
* average between them
* Precondition: The points scored will be sent down.
* Post Condition: The average will have been calculated for the points scored.
* @param list
* @return will return the average for the points scored
*/
private static Integer averagePointsScored(ArrayList<Player> list) {
int average = 0;
int totalValue = 0;
for (int i = 0; i < list.size(); i++) {
totalValue = list.get(i).getPointsScored() + totalValue;
}
average = totalValue / list.size();
return average;
}//end of averaePointsScored method
/** This method will get the assists from the list and calculate the
* average between them
* Precondition: The assists will be sent down.
* Post Condition: The average will have been calculated for the assists.
* @param list
* @return will return the average for the assists.
*/
private static Integer averageAssists(ArrayList<Player> list) {
int average = 0;
int totalValue = 0;
for (int i = 0; i < list.size(); i++) {
totalValue = list.get(i).getAssists() + totalValue;
}
average = totalValue / list.size();
return average;
}//end of averageAssists method
/** This method will get the penalty kick % from the list and calculate the
* average between them.
* Precondition: The Penalty kick % will be sent down.
* Post Condition: The average will have been calculated for the penalty kick %.
* @param list
* @return will return the average for the penalty kick %.
*/
private static double averagePenaltyKicks(ArrayList<Player> list) {
double average = 0;
double totalValue = 0;
for (int i = 0; i < list.size(); i++) {
totalValue = list.get(i).getPenaltyKickRate() + totalValue;
}
average = totalValue / list.size();
return average;
}//end of averagePenaltyKicks method
}
This post has been edited by redman8442: 12 April 2012 - 12:56 PM

New Topic/Question
Reply



MultiQuote




|