Write a program that declares a struct to store the data of a football player (p
ID: 3694276 • Letter: W
Question
Write a program that declares a struct to store the data of a football player (player’s name, player’s position, number of touchdowns, number of catches, number of passing yards, number of receiving yards, and the number of rushing yards). Declare an array of 10 components to store the data of 10 football players. Your program must contain a function to input data and a function to output data. Add functions to search the array to find the index of a specific player, and update the data of a player. (An input file has been provided with test data.) Before the program terminates, save the updated data in a file (you can just read in the file before the main menu and write out the file once the use has chosen to quit). Your program should contain two menus. Once which has the following options:
1. Select a player (by name)
2. Display player information
3. Update player information
4. Quit
If option 3 is selected a second menu should be displayed showing a list of what items can be updated. When the user selects an item, the program should ask the user for the new information and then update the appropriate piece of information in the player list. The user should be allowed to update statistics for that player until they decide to quit.
Explanation / Answer
package mainmenutest;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MainmenuTest extends premierLeagueClubs{
int clubChoice;
public MainmenuTest(){
//constructor
}
public static void main(String args[]){
MainmenuTest team = new MainmenuTest();
team.getInput();
} // end main method
public void getInput(){
boolean exit = false;
int option=0;
while (!exit) {
try{
Scanner in = new Scanner(System.in);
menu();
System.out.println(" ");
option = in.nextInt();
} // end try
catch(InputMismatchException e) {
} // end catch
switch (option) {
case 1:
choosePlayer();
exit = true;
break;
case 2:
createProfile();
exit = true;
break;
case 3:
DeserializeDemo load = new DeserializeDemo();
load.Deserialize(clubChoice);
exit = true;
break;
case 4:
credits();
exit = true;
break;
case 5:
exit = true;
System.out.println("Goodbye!");
break;
case 6:
SerializeDemo save = new SerializeDemo();
save.Serialize(clubChoice);
exit = true;
break;
default:
System.out.println("Invalid option");
} // end switch
} // end loop
} // end getInput
public int choosePlayer(){
boolean exit = false;
while (!exit) {
System.out.println(" ");
System.out.println("Please enter an option 1-10 to select a player, or 11 to quit : ");
System.out.println("1. Lillian Thuram");
System.out.println("2. Roberto Carlos");
System.out.println("3. David Beckham");
System.out.println("4. Oliver Kahn");
System.out.println("5. Zico");
System.out.println("6. Lev Yashin");
System.out.println("7. Ronald Koeman");
System.out.println("8. Luis Figo");
System.out.println("9. Alvaro Recoba");
System.out.println("10. Alfredo di Stefano");
System.out.println("11. Raul Gonzalez");
System.out.println("12. Michael Platini");
System.out.println("13. Carlos Alberto Torres");
System.out.println("14. Ronaldo");
System.out.println("15. Gerd Muller");
System.out.println("16. Johann Cruyff");
System.out.println("17. Franz Beckenbauer");
System.out.println("18. Diego Maradona");
System.out.println("19. Pele");
System.out.println("20. Zinedine Zidane");
System.out.println(" ");
System.out.println("21. Quit");
System.out.println("22. Mainmenu");
try{
Scanner in = new Scanner(System.in);
System.out.println(" ");
Choice = in.nextInt();
} // end try
catch(InputMismatchException e) {
} // end catch
switch(Choice){
case 1:
LillianThuram();
exit = true;
break;
case 2:
RobertoCarlos();
exit = true;
break;
case 3:
DavidBeckham();
exit = true;
break;
case 4:
OliverKahn();
exit = true;
break;
case 5:
Zico();
exit = true;
break;
case 6:
LevYashin();
exit = true;
break;
case 7:
RonaldKoeman();
exit = true;
break;
case 8:
LuisFigo();
exit = true;
break;
case 9:
AlvaroRecoba();
exit = true;
break;
case 10:
AlfredodiStefano();
exit = true;
break;
case 11:
RaulGonzalez();
exit = true;
break;
case 12:
MichaelPlatini();
exit = true;
break;
case 13:
CarlosAlbertoTorres();
exit = true;
break;
case 14:
Ronaldo();
exit = true;
break;
case 15:
GerdMuller();
exit = true;
break;
case 16:
JohannCruyff();
exit = true;
break;
case 17:
FranzBeckenbauer();
exit = true;
break;
case 18:
DiegoMaradona();
exit = true;
break;
case 19:
Pele();
exit = true;
break;
case 20:
ZinedineZidane();
exit = true;
break;
case 21:
System.out.println("Goodbye!");
exit = true;
break;
case 22:
getInput();
exit = true;
break;
} // end switch
} // end loop
return Choice;
} // end chooseTeam
public void createProfile(){
} // end createProfile
public void credits(){
} // end credits
public void menu(){
int option=0;
System.out.println(" ");
System.out.println("Please enter an option : ");
System.out.println("1. Select a player (by name)");
System.out.println("2. Display player information");
System.out.println("3. Update player information");
System.out.println("4. Quit");
} // end menu
} // end class MainmenuTest
ClubInfo.java
package mainmenutest;
/**
*
* @author Darren Estcourt
*/
//import java.util.InputMismatchException;
//import java.util.Scanner;
//import java.io.*;
//import java.util.Arrays;
public class ClubInfo implements java.io.Serializable{
public String teamName;
public String stadium;
public String division;
public transient int SSN;
public int stadiumCapacity;
} // end ClubInfo class
premierLeagueClubs.java
package mainmenutest;
public class premierLeagueClubs{
public void Lillian Thuram(){
System.out.println(" ");
System.out.println("You are the new manager of Lillian Thuram!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Lillian Thuram
public void RobertoCarlos(){
System.out.println(" ");
System.out.println("You are the new manager of Aston Villa!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Roberto Carlos
public void DavidBeckham(){
System.out.println(" ");
System.out.println("You are the new manager of David Beckham!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method David Beckham
public void OliverKahn(){
System.out.println(" ");
System.out.println("You are the new manager of Oliver Kahn!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Oliver Kahn
public void Zico(){
System.out.println(" ");
System.out.println("You are the new manager of Crystal Palace!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Zico
public void LevYashin(){
System.out.println(" ");
System.out.println("You are the new manager of LevYashin!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method LevYashin
public void RonaldKoeman(){
System.out.println(" ");
System.out.println("You are the new manager of Ronald Koeman!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Ronald Koeman
public void LuisFigo(){
System.out.println(" ");
System.out.println("You are the new manager of Luis Figo!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Luis Figo
public void AlfredodiStefano(){
System.out.println(" ");
System.out.println("You are the new manager of Manchester City!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Alfredo di Stefano
public void AlvaroRecoba(){
System.out.println(" ");
System.out.println("You are the new manager of Manchester United!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Alvaro Recoba
public void RaulGonzalez(){
System.out.println(" ");
System.out.println("You are the new manager of Newcastle United!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Raul Gonzalez
public void MichaelPlatini(){
System.out.println(" ");
System.out.println("You are the new manager of Norwich City!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Michael Platini
public void CarlosAlbertoTorres(){
System.out.println(" ");
System.out.println("You are the new manager of Carlos Alberto Torres!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Carlos Alberto Torres
public void Ronaldo(){
System.out.println(" ");
System.out.println("You are the new manager of Stoke City!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Ronaldo
public void GerdMuller(){
System.out.println(" ");
System.out.println("You are the new manager of Gerd Muller!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Gerd Muller
public void JohannCruyff(){
System.out.println(" ");
System.out.println("You are the new manager of Swansea City!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Johann Cruyff
public void FranzBeckenbauer(){
System.out.println(" ");
System.out.println("You are the new manager of Tottenham Hotspur!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Franz Beckenbauer
public void DiegoMaradona(){
System.out.println(" ");
System.out.println("You are the new manager of Diego Maradona!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Diego Maradona
public void Pele(){
System.out.println(" ");
System.out.println("You are the new manager of West Bromich Albion!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Pele
public void ZinedineZidane(){
System.out.println(" ");
System.out.println("You are the new manager of West Ham United!");
System.out.println(" ");
System.out.println("Press any key to continue...");
MainmenuTest back = new MainmenuTest();
back.getInput();
} // end method Zinedine Zidane
} // end class premierLeagueClubs
SerializeDemo.java
package mainmenutest;
import java.io.*;
public class SerializeDemo
{
public void Serialize(int clubChoice)
{
ClubInfo club = new ClubInfo();
ClubInfo club2 = new ClubInfo();
club.teamName = "Lillian Thuram";
club.stadium = "Emirates";
club.division = "Premier League";
club.SSN = 11122333;
club.stadiumCapacity = 60000;
club2.teamName = "Aston Villa";
try
{
FileOutputStream fileOut =
new FileOutputStream("C:/tmp/club.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
// if (clubChoice == 1){
out.writeObject(club);
out.writeObject(club2);
// out.close();
// fileOut.close();
// }
// else
// if (clubChoice == 2){
// out.writeObject(club2);
//out2.close();
//fileout2.close();
// }
System.out.printf("Serialized data is saved in C:/tmp/club.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
} // end class SerializeDemo
DeserializeDemo.java
package mainmenutest;
/**
*
* @author Darren Estcourt
*/
import java.io.*;
public class DeserializeDemo
{
public void Deserialize(int clubChoice)
{
ClubInfo club = null;
ClubInfo club2 = null;
try
{
FileInputStream fileIn = new FileInputStream("C:/tmp/club.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
// ObjectInputStream in2 = new ObjectInputStream(fileIn);
// if (clubChoice == 1){
club = (ClubInfo) in.readObject();
club2 = (ClubInfo) in.readObject();
// in.close();
// fileIn.close();
// } // end if
// if (clubChoice == 2){
// club2 = (ClubInfo) in2.readObject();
// in2.close();
// fileIn.close();
// } // end if
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Club class not found");
c.printStackTrace();
return;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.