如何使用以下代码添加运动员?

时间:2012-02-20 01:36:06

标签: java swing

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,而不是编写一个单独的方法来添加每种类型的运动员有一种方法,当一个选项是从choseAthleteMenu()菜单方法中选择使用某种检查运动员可以添加到他们的相关位置? 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我的想法是让不同类型的运动员实现一个界面,然后将其传递给该界面下的addrunner,以及要创建的运动员的用户输入。不想为你解决整个事情,但也许这会帮助你。