0
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(){
}
//////////////////////////////////
}
我想要做的是通過在上面的代碼中使用JTextfield
添加不同類型的atlete,而不是編寫一個單獨的方法來添加每種類型的運動員是否有一種方法,當從中選擇一個選項時使用某種檢查運動員的菜單方法可以添加到相關的地方嗎? 有什麼建議嗎?如何用下面的代碼添加運動員?
他們都有sperate接口RunnerInterface DuAthleteinterface等.... ?? – user1220007 2012-02-20 02:09:32
嘗試使用for循環爲運動員設置變量,您當前的方法非常難以閱讀。 – Jimmt 2012-02-20 02:54:32