public class AthleteManager {
private static Icon anIcon = new ImageIcon();
private static int currentSize = 0, maxSize = 10;
private Athlete[] AthleteList = new Athlete[maxSize];
///////////////////////////////////////////////////
public AthleteManager(){
Runner aRunner = new Runner("Bolt","Jamica",101);
AthleteList[currentSize]=aRunner;
currentSize++;
DuAthlete aDuAthlete = new DuAthlete("Benny", "Belgian", 102);
AthleteList[currentSize]=aDuAthlete;
currentSize++;
TriAthlete aTriAthlete = new TriAthlete("Alexander", "Irish", 103);
AthleteList[currentSize]=aTriAthlete;
currentSize++;
Coach aCoach = new Coach("Wolmer", "Britan", 104);
AthleteList[currentSize] = aCoach;
currentSize++;
}
/////////////////////////////////////////////////////////////////////////
public int mainMenu()
{
int option =0;
String opt1 = new String("1. Add an Athlete :");
String opt2 = new String("2. Register an Athlete with a Coach :");
String opt3 = new String("3. List All members of Team DS 2012 :");
String opt4 = new String("4. List all Athletes of Coach (based on ID):");
String opt5 = new String("5. Display Leader Board:");
String opt6 = new String("6. Search for an Athlete (based on a Name) :");
String opt7 = new String("7. Remove an Athlete (based on ID):");
String opt8 = new String("8. Log finishing Distances:");
String opt9 = new String("9. Exit System");
String msg = new String("Enter Option:");
JTextField opt = new JTextField("");
Object message[] = new Object[12];
message[0] = myIcon;
message[1] = opt1;
message[2] = opt2;
message[3] = opt3;
message[4] = opt4;
message[5] = opt5;
message[6] = opt6;
message[7] = opt7;
message[8] = opt8;
message[9] = opt9;
message[10] = msg;
message[11] = opt;
int response = JOptionPane.showConfirmDialog(null,message,"Athlete Data Entry",JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE ,anIcon);
if(response == JOptionPane.CANCEL_OPTION)
;
else
{
try {
option = Integer.parseInt( opt.getText());
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Data Input Error" + e + "\nPlease Try Again");
}
}
return option;
}
////////////////////////////////////////////////////////////////////////
public void addAthleteMenu(){
choseAthleteMenu();
}
////////////////////////////////////////////////////////////////////////
public int choseAthleteMenu(){
int option =0;
String inform = new String("Please Select the type of athlete");
String opt1 = new String("1. Runner:");
String opt2 = new String("2. DuAtlete :");
String opt3 = new String("3. TriAthlete :");
String opt4 = new String("4. Coach");
String msg = new String("Enter Option:");
JTextField opt = new JTextField("");
Object message[] = new Object[8];
message[0] = myIcon;
message[1] =inform;
message[2] = opt1;
message[3] = opt2;
message[4] = opt3;
message[5] = opt4;
message[6] = msg;
message[7] = opt;
int response = JOptionPane.showConfirmDialog(null,message,"Athlete Data Entry",JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE ,anIcon);
if(response == JOptionPane.CANCEL_OPTION)
;
else
{
try {
addRunner(); }
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Data Input Error" + e + "\nPlease Try Again");
}
}
return option;
}
/////////////////////////////////////
public void addRunner(){
String msgName = new String("Athlete Name :");
String msgClubName= new String("Club Name :");
Integer msgID = new Integer("Athlete ID :");
String msgDistance = new String("Athlete Distance :");
JTextField name = new JTextField("");
JTextField club = new JTextField("");
JTextField id = new JTextField();
JTextField distance = new JTextField("");
Object message[] = new Object[9];
message[0] = myIcon;
message[1] = msgName;
message[2] = name;
message[3] = msgClubName;
message[4] = club;
message[5] = msgID;
message[6] = id;
message[7] = msgDistance;
message[8] = distance;
int response = JOptionPane.showConfirmDialog(null,message,"Athlete Data Entry",JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE ,anIcon);
if(response == JOptionPane.CANCEL_OPTION)
;
else
{
try{
Runner nRunner = new Runner();
nRunner.setName(name.getText());
nRunner.setClub(club.getText());
nRunner.setId(Integer.parseInt(id.getText()));
addRunnerToList(nRunner);
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Data Input Error" + e + "\nPlease Try Again");
}
}
}
///////////////////////////////////////////////////////////////////////////////
private void addRunnerToList(Runner nRunner){
try{
AthleteList[currentSize]=nRunner;
currentSize++;
}catch(Exception sqle){
JOptionPane.showMessageDialog(null, "Can Not Add to List" +sqle);
}
}
///////////////////////////////////////////////////////////////////////////
public void menuListAthletes(){
JOptionPane.showMessageDialog(null, AthleteList);
}
////////////////////////////////////////////////
public void regAnAthleteWithCoachMenu(){
}
//////////////////////////////////////////
public void listAthletesOfCoach(){
}
///////////////////////////////////
public void leaderBoard(){
}
//////////////////////////////////////////
public void searchAthlete(){
}
//////////////////////////////////////
public void removeAthlete(){
}
//////////////////////////////////
public void logDistances(){
}
//////////////////////////////////
}
What I’m trying to do is add different types of atlete by using the JTextfield, in the above code rather than writing a separate method for adding each type of athlete is there a way that when an option is selected from the the choseAthleteMenu() menu method that using some sort of check athletes can be added to their relevant places?
Any advice?
My idea would be to have the different types of Athletes implement a interface and then pass it to addrunner under that interface with the user input of which athlete to create. Don’t want to solve the whole thing for you but maybe that will help you out.