Results 1 to 6 of 6
  1. #1
    ezrabm21 is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default add player to team

    Hi I am having difficulty creating a program that allows user to enter name and position ad adding that player to an array, it seems that if I add the first player it turns out fine but when I add the second player the addplayer method somehow resets player count to zero.

    import java.util.ArrayList;
    /**The BobSleighTeam class holds methods to addplayers to team, print team, and get number of members of team
    *
    * @author ezrabm21
    *
    */
    public class BobSleighTeam
    {
    private static int numPilot;
    private int numPusher;
    private int numMembers;
    private static int numBrakeman;
    private String countryName;
    private String teamType;
    private static int totalMembers;
    String str="";
    ArrayList<BobSleighPlayer> array=new ArrayList<BobSleighPlayer>();
    /**Constructor that sets country name, type of team, and number of members on team
    *
    * @param cn name of country in string
    * @param typeTeam type of team men or womens string
    * @param numMem number of members on team 2 or 4
    */
    public BobSleighTeam(String cn, String typeTeam, int numMem)
    {
    countryName=cn;
    teamType=typeTeam;
    numMembers=numMem;
    numPilot=0;
    numPusher=0;
    numBrakeman=0;
    totalMembers=0;


    }
    /**The getNumMembers method returns the number of members on a team
    *
    * @return returns the number of players on the team
    */
    public int getNumMembers()
    {
    return numMembers;
    }
    /**The addPlayer method adds players to a team, validates adding a player, and returns a string if any errors and null if none
    *
    * @param fname first name of player
    * @param lname last name of player
    * @param pos position of player
    * @return returns string if any error or null if none
    */
    public String addPlayer(String fname, String lname, String pos)
    {


    if(numMembers==2)
    {
    if(pos.equals("pilot") && numPilot==0)
    {

    array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.pilot));
    numPilot++;
    totalMembers++;
    str=null;
    }
    else if(pos.equals("pilot") && numPilot!=0)
    {
    str="There is already a pilot on this team.\nPlayer not added.";
    }
    else if(pos.equals("brakeman") && numBrakeman==0)
    {

    array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.brakeman));
    numBrakeman++;
    totalMembers++;
    str=null;
    }
    else if(pos.equals("brakeman") && numBrakeman!=0)
    {
    str="There is already a brakeman on this team.\nPlayer not added.";
    }
    } else if(numMembers==4)
    {if(pos=="pilot" && numPilot==0)
    {

    array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.pilot));
    numPilot++;
    totalMembers++;
    str=null;
    }
    else if(pos.equals("pilot") && numPilot!=0)
    {
    str="There is already a pilot on this team.\nPlayer not added.";
    }
    else if(pos.equals("brakeman") && numBrakeman==0)
    {

    array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.brakeman));
    numBrakeman++;
    totalMembers++;
    str=null;
    }
    else if(pos.equals("brakeman") && numBrakeman!=0)
    {
    str="There is already a brakeman on this team.\nPlayer not added.";
    }
    else if(pos.equals("pusher") && numPusher==0)
    {

    array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.pusher));
    numPusher++;
    totalMembers++;
    str=null;
    }
    else if(pos.equals("pusher") && numPusher==1)
    {

    array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.pusher));
    numPusher++;
    totalMembers++;
    str=null;
    }
    else if(pos.equals("pusher") && numPusher==2)
    {
    str="There are already two pushers on this team.";
    }
    }System.out.println(lname+fname+pos+numMembers+num Pilot+numBrakeman);
    return str;
    }
    /**The printTeam method creates a string of the components of a team
    *
    * @return returns the string that represents the team players, positions,country, and type
    */
    public String printTeam()
    {
    String str1="";
    if(numMembers==2)
    {
    str1=countryName+"\n"+teamType+" 2 man team\n"+array.get(0).toString()+"\n"+array.get(1). toString();
    }
    else if(numMembers==4)
    {
    str1=countryName+"\n"+teamType+" 4 man team\n"+array.get(0).toString()+"\n"+array.get(1). toString()+"\n"+array.get(2).toString()+"\n"+array .get(3).toString();
    }

    return str1;
    }
    /**The toString method creates a string of players, positions, type, and country
    *
    */
    public String toString()
    {
    String str="";
    String str1="";
    String str2="";
    str="Country Name: "+countryName+" Team Type:"+teamType+" Number of Members: "+numMembers+"\n"+
    "Total Members added: "+ totalMembers;
    for(int index=0;index<array.size();index++)
    {
    str1=array.get(index).toString()+"\n";
    }
    str2=str+str1;
    return str2;
    }

    }



    my gui


    import javax.swing.*; //import swing
    import java.awt.*; //import awt
    import java.awt.event.*; //import awt for events
    import java.util.ArrayList;
    /**The GUIDriver class creates panels, buttons, labels,textfields and handles action events
    *
    * @author ezrabm21
    *
    */
    public class GUIDriver extends JFrame
    {
    private JPanel topPanel;
    private JPanel bottomPanel;
    private JPanel buttonPanel;
    private JLabel heading;
    private JLabel countrytext;
    private JTextField usercountry;
    private JRadioButton typemens;
    private JRadioButton typewomens;
    private JRadioButton twoteam;
    private JRadioButton fourteam;
    private ButtonGroup typegroup;
    private ButtonGroup numteamgroup;
    private JButton create;
    private JLabel fname;
    private JLabel lname;
    private JTextField userfname;
    private JTextField userlname;
    private JRadioButton pilot;
    private JRadioButton brakeman;
    private JRadioButton pusher;
    private ButtonGroup posgroup;
    private JButton addplayerbutton;
    private JButton printteambutton;
    private JButton newteambutton;
    private JButton exitbutton;
    private JPanel typepanel;
    private JPanel numteampanel;
    private JPanel pospanel;
    String countryname;
    String menorwomen="";
    int numofmem=0;
    /**Constructor that sets content pane attributes and builds/adds panels
    *
    */
    public GUIDriver()
    {
    setTitle("Create Bob Sleigh Team");
    setSize(400,600);
    setLayout(new GridLayout(3,1));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    buildTopPanel();
    buildBottomPanel();
    buildButtonPanel();

    add(topPanel, BorderLayout.NORTH);
    add(bottomPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);

    setVisible(true);
    topPanel.setVisible(true);
    bottomPanel.setVisible(false);
    buttonPanel.setVisible(false);

    }
    /**The buildButtonPanel builds the button panel that holds the addplayer, printteam, newteam, and exit buttons
    *
    */
    public void buildButtonPanel()
    {
    buttonPanel=new JPanel();
    addplayerbutton=new JButton("Add Player");
    printteambutton=new JButton("Print Team");
    newteambutton=new JButton("New Team");
    exitbutton=new JButton("Exit");
    addplayerbutton.addActionListener(new ButtonListener());
    printteambutton.addActionListener(new ButtonListener());
    newteambutton.addActionListener(new ButtonListener());
    exitbutton.addActionListener(new ButtonListener());
    addplayerbutton.setActionCommand("a");
    printteambutton.setActionCommand("p");
    newteambutton.setActionCommand("n");
    exitbutton.setActionCommand("e");
    buttonPanel.add(addplayerbutton);
    buttonPanel.add(printteambutton);
    buttonPanel.add(newteambutton);
    buttonPanel.add(exitbutton);
    }
    /**The buildTypePanel builds a panel that holds the team type radiobutton section
    *
    */
    public void buildTypePanel()
    {
    typepanel=new JPanel();
    typemens=new JRadioButton("Men's");
    typewomens=new JRadioButton("Women's");
    typegroup=new ButtonGroup();
    typegroup.add(typemens);
    typegroup.add(typewomens);
    typepanel.add(typemens);
    typepanel.add(typewomens);
    typemens.addActionListener(new TypeRadioButtonListener());
    typewomens.addActionListener(new TypeRadioButtonListener());
    typepanel.setBorder(BorderFactory.createTitledBord er("Type"));
    }
    /**The buildNumTeamPanel builds the panel that holds the number of team members section with radiobuttons
    *
    */
    public void buildNumTeamPanel()
    {
    numteampanel=new JPanel();
    twoteam=new JRadioButton("2 member team");
    fourteam=new JRadioButton("4 member team");
    numteamgroup=new ButtonGroup();
    numteamgroup.add(twoteam);
    numteamgroup.add(fourteam);
    numteampanel.add(twoteam);
    numteampanel.add(fourteam);
    numteampanel.setBorder(BorderFactory.createTitledB order("Number of members"));
    }
    /**The buildTopPanel builds the top panel that holds the type panel, numteampanel, heading, country label and textfield and creat button
    *
    */
    public void buildTopPanel()
    {
    topPanel=new JPanel();
    topPanel.setLayout(new FlowLayout());
    topPanel.setSize(400,300);
    heading=new JLabel(".......................................... ......................................2014 Olympic BobSleigh Teams............................................. ..................................");
    countrytext=new JLabel("Country");
    usercountry=new JTextField(25);
    create=new JButton("Create");
    create.addActionListener(new ButtonListener1());
    create.setActionCommand("c");
    buildTypePanel();
    buildNumTeamPanel();
    topPanel.add(heading);

    topPanel.add(countrytext);
    topPanel.add(usercountry);
    topPanel.add(typepanel);
    topPanel.add(numteampanel);
    topPanel.add(create);

    }
    /**The buildPosPanel builds the panel that holds the radio buttons for position of player
    *
    */
    public void buildPosPanel()
    {
    pospanel=new JPanel();
    pilot=new JRadioButton("pilot");
    brakeman=new JRadioButton("brakeman");
    pusher=new JRadioButton("pusher");
    posgroup=new ButtonGroup();
    posgroup.add(pilot);
    posgroup.add(brakeman);
    posgroup.add(pusher);
    pospanel.add(pilot);
    pospanel.add(brakeman);
    pospanel.add(pusher);
    pospanel.setBorder(BorderFactory.createTitledBorde r("Position"));
    }
    /**The buildBottomPanel builds the panel that holds the pospanel, first/last name labels and textfields
    *
    */
    public void buildBottomPanel()
    {
    bottomPanel=new JPanel();

    lname=new JLabel("Last name");
    fname=new JLabel("First name");
    userfname=new JTextField(25);
    userlname=new JTextField(25);
    bottomPanel.add(fname);
    bottomPanel.add(userfname);
    bottomPanel.add(lname);
    bottomPanel.add(userlname);
    buildPosPanel();
    bottomPanel.add(pospanel);

    }
    /**The class typeradiobuttonlistener controls action of type of team radio buttons
    *
    * @author ezrabm21
    *
    */
    private class TypeRadioButtonListener implements ActionListener
    {
    /**This method controls action of type radiobuttons, if type women is selected then only 2 member radio button is set visible
    *
    */
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==typewomens)
    fourteam.setVisible(false);
    else if(e.getSource()==typemens)
    fourteam.setVisible(true);
    }
    }
    /**The buttonlistener1 class controls actions when create button is pressed
    *
    * @author ezrabm21
    *
    */
    private class ButtonListener1 implements ActionListener
    {
    /**Method controls when create method is pressed bottom and button panel are visible and countryname, team type, and number of members are set
    *
    */
    public void actionPerformed(ActionEvent e)
    {

    String actionCommand=e.getActionCommand();
    if(actionCommand.equals("c"))
    {
    countryname=usercountry.getText();
    if(typemens.isSelected())
    menorwomen="men's";
    else if(typewomens.isSelected())
    menorwomen="women's";
    if(twoteam.isSelected())
    numofmem=2;
    else if(fourteam.isSelected())
    numofmem=4;
    bottomPanel.setVisible(true);
    buttonPanel.setVisible(true);
    usercountry.setEditable(false);
    create.setEnabled(false);
    if(typewomens.isSelected() || twoteam.isSelected())
    pusher.setVisible(false);
    }}}
    /**The buttonlistenerclass controls actions if addplayer, printteam, newplayer, and exit buttons are pressed
    *
    * @author ezrabm21
    *
    */
    private class ButtonListener implements ActionListener
    {
    /**This method controls actions of buttons on button panel, creates bobsleighteam object
    *
    */
    public void actionPerformed(ActionEvent e)
    {
    String actionCommand=e.getActionCommand();
    String firstname;
    String lastname;
    String position="";
    String string1;
    BobSleighTeam team=new BobSleighTeam(countryname, menorwomen, numofmem);
    if(actionCommand.equals("a"))
    {
    firstname=userfname.getText();
    lastname=userlname.getText();
    if(pilot.isSelected())
    position="pilot";
    else if(brakeman.isSelected())
    position="brakeman";
    else if(pusher.isSelected())
    position="pusher";
    string1=team.addPlayer(firstname, lastname, position);
    if(string1!=null)
    JOptionPane.showMessageDialog(null, string1);

    userfname.setText("");
    userlname.setText(null);
    }
    else if(actionCommand.equals("p"))
    {
    JOptionPane.showMessageDialog(null, team.printTeam());
    }
    else if(actionCommand.equals("e"))
    {
    System.exit(0);
    }
    else if(actionCommand.equals("n"))
    {
    usercountry.setEditable(true);
    usercountry.setText(null);
    buttonPanel.setVisible(false);
    bottomPanel.setVisible(false);
    create.setEnabled(true);

    }


    }
    }

    /**The main method of program
    *
    * @param args
    */
    public static void main(String[] args) //main method
    {
    new GUIDriver();
    }
    }

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: add player to team

    Please use [code] tags.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: add player to team

    Problems:

    Unformatted code
    A gazillion lines of code

    Most people are not going to read that.

    Solutions:
    Use code tags
    Post a SSCCE (google it) or
    Narrow the problem down and only post relevant code

  4. #4
    ezrabm21 is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: add player to team

    ok the problem I have is basically I will call the addplayer method to add a player but if I add another player of the same position the player position counter seems to go back to zero

    public String addPlayer(String fname, String lname, String pos)
    {


    if(numMembers==2)
    {
    if(pos.equals("pilot") && numPilot==0)
    {

    array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.pilot));
    numPilot++;
    totalMembers++;
    str=null;
    }
    else if(pos.equals("pilot") && numPilot!=0)
    {
    str="There is already a pilot on this team.\nPlayer not added.";
    }

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: add player to team

    See how all your code is left aligned and hard to read. That is why we asked you use code tags. If you do not know how to use code tags ask or click the BB code link near the bottom of the page to learn.

    It seems all your code is inside the if(numMembers==2) statement so what happens if numMembers is != 2?

  6. #6
    ezrabm21 is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: add player to team

    Java Code:
    import java.util.ArrayList;
    /**The BobSleighTeam class holds methods to addplayers to team, print team, and get number of members of team
     * 
     * @author ezrabm21
     *
     */
    
    public class BobSleighTeam
    {
    	private int numPilot;
    	private int numPusher;
    	private int numMembers;
    	private int numBrakeman;
    	private String countryName;
    	private String teamType;
    	private int totalMembers;
    	String str="";
    	static ArrayList<BobSleighPlayer> array=new ArrayList<BobSleighPlayer>();
    	/**Constructor that sets country name, type of team, and number of members on team
    	 * 
    	 * @param cn name of country in string
    	 * @param typeTeam type of team men or womens string
    	 * @param numMem number of members on team 2 or 4
    	 */
    	public BobSleighTeam(String cn, String typeTeam, int numMem)
    	{
    		countryName=cn;
    		teamType=typeTeam;
    		numMembers=numMem;
    		numPilot=0;
    		numPusher=0;
    		numBrakeman=0;
    		totalMembers=0;
    		
    		
    	}
    	/**The getNumMembers method returns the number of members on a team
    	 * 
    	 * @return returns the number of players on the team
    	 */
    	public int getNumMembers()
    	{
    		return numMembers;
    	}
    	/**The addPlayer method adds players to a team, validates adding a player, and returns a string if any errors and null if none
    	 * 
    	 * @param fname first name of player 
    	 * @param lname last name of player
    	 * @param pos position of player
    	 * @return returns string if any error or null if none
    	 */
    	public String addPlayer(String fname, String lname, String pos)
    	{
    		
    		
    		if(numMembers==2)
    		{
    				            if(pos.equals("pilot") && numPilot==0)
    							{
    				            	
    								array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.pilot));
    								numPilot++;
    								totalMembers++;
    								str=null;
    							}
    		                else if(pos.equals("pilot") && numPilot!=0)
    		                {
    		                	str="There is already a pilot on this team.\nPlayer not added.";
    		                }
    		                else if(pos.equals("brakeman") && numBrakeman==0)
    		                {
    		                	
    		                    array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.brakeman));
    		                    numBrakeman++;
    		                    totalMembers++;
    		                    str=null;
    		       
    		                }
    		                else if(pos.equals("brakeman") && numBrakeman!=0)
    		                {
    		                	str="There is already a brakeman on this team.\nPlayer not added.";
    		                }
    		}        else if(numMembers==4)
    		         		{if(pos=="pilot" && numPilot==0)
    		                {
    			                
    			                array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.pilot));
    			                numPilot++;
    			                totalMembers++;
    			                str=null;
    	                 	}
                            else if(pos.equals("pilot") && numPilot!=0)
                            {
                            	str="There is already a pilot on this team.\nPlayer not added.";
                            }
                            else if(pos.equals("brakeman") && numBrakeman==0)
                            {
        	                    
                                array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.brakeman));
                                numBrakeman++;
                                totalMembers++;
                                str=null;
                            }
                            else if(pos.equals("brakeman") && numBrakeman!=0)
                            {
                            	str="There is already a brakeman on this team.\nPlayer not added.";
                            }
                            else if(pos.equals("pusher") && numPusher==0)
                            {
                            	
                            	array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.pusher));
                            	numPusher++;
                            	totalMembers++;
                            	str=null;
                            }
                            else if(pos.equals("pusher") && numPusher==1)
                            {
                            	
                            	array.add(new BobSleighPlayer(lname, fname, BobSleighPosition.pusher));
                            	numPusher++;
                            	totalMembers++;
                            	str=null;
                            }
                            else if(pos.equals("pusher") && numPusher==2)
                            {
                            	str="There are already two pushers on this team.";
                            }
    		}
    		return str;
    	}
    	/**The printTeam method creates a string of the components of a team
    	 * 
    	 * @return returns the string that represents the team players, positions,country, and type
    	 */
    	public String printTeam()
    	{
    		String str1;
    		if(numMembers==2)
    		{
    
    			str1=countryName+"\n"+teamType+" "+numMembers+" member team\n"+array.get(0)+"\n"+array.get(1);
    		}
    		else if(numMembers==4)
    		{
    			str1=countryName+"\n"+teamType+" "+numMembers+" member team\n"+array.get(0)+"\n"+array.get(1)+"\n"+array.get(2)+"\n"+array.get(3);
    		}
    		else
    			str1="Please finish entering your team";
    			
    		
    		return str1;
    	}
    	/**The toString method creates a string of players, positions, type, and country
    	 * 
    	 */
    	public String toString()
    	{
    		String str="";
    		String str1="";
    		String str2="";
    		str="Country Name: "+countryName+" Team Type:"+teamType+" Number of Members: "+numMembers+"\n"+
    		"Total Members added: "+ totalMembers;
    		for(int index=0;index<array.size();index++)
    		{
    			str1=array.get(index).toString()+"\n";
    		}
    		str2=str+str1;
    		return str2;
    	}
    	
    	
    }

Similar Threads

  1. Team member PACS & RIS team
    By bizihtech in forum Jobs Offered
    Replies: 0
    Last Post: 12-14-2009, 10:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •