1

I have hit a brick wall and the revision book isn't much help as it provides flat examples, my revision assignment requires me to declare a local variable of a type to reference a list, Teams, then if should get the list of teams for the key value division, and assign it to the local variable.

It then ask me to increment the number of wins for teamA if it has a higher score than teamB and vice versa, if it is a draw then increment the number of draws.

I have managed to write the method to iterate over the list and an if-else statement to check he given arguments using a .get() method which works

The problem I am having is accessing in the incWon() method in the Team class to change the value of won for each map value. The material though gives quite flat examples of how map values are to be changed which don't really explain how I can change a value using a dynamic input.

any help would be greatly appreciated as if I can get won to work the rest will fall into place.

This is my class

{
   private SortedMap<String, Set<Team>> teams;
   /**
    * Constructor for objects of class LeagueAdmin
    */
   public LeagueAdmin()
   {
      // Create the HashMap
      //Map<String, Team> teams = new HashMap<>();
      super();
      this.teams = new TreeMap<>();

   }

   public void addTeam(String division, Team team)
   {
      boolean changed;

      if (!this.teams.containsKey(division)) // checks if the key division doesn't contain the value of divsioin  
      {
         HashSet<Team> teamSet = new HashSet<>(); // instantiates a list of objects called Team and assigns them to local variable teamSet
         teamSet.add(team); // adds a new team to the list

         this.teams.put(division, teamSet);
         changed = true;
      }
      else
      {
         Set<Team> teamSet = this.teams.get(division); // if list exists already adds new team to existing list
         changed = teamSet.add(team);
      }

   }     

   public void recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)
   {
      Set<String> teamKeys = teams.keySet();
      for (String eachDivision: teamKeys)
      {
         if(teamAScore > teamBScore)
         {
            teams.put(); // updates wins for teamA
            // System.out.println(teamA);
         }
         else if (teamAScore < teamBScore)
         {
            teams.get(teamB); // updates wins for teamB
            // System.out.println(teamB);
         }
         else
         {
            // teams.put(); //updates draws for both teams
         }

         // System.out.println(eachDivision + " teams are " + teams.get(eachDivision));
      }
   }
}

and this the TEAM class I am have to access to increment values.

public class Team
{  
   private String name;
   private String division;
   private int won;
   private int drew;
   private int lost;
// no need to record points as = 3*won + drew   


   /**
    * Constructor for objects of class Team
    */
   public Team(String aName, String aDivision)
   {
      name = aName;
      division = aDivision;
      // no need to set won, drew and lost to 0
   }


   /**
    * getter for attribute points
    */
   public int getPoints()
   {
      return 3 * won + drew;
   }

   /**
    * getter for name
    */
   public String getName()
   {
      return name;
   }

   /**
    * getter for division
    */
   public String getDivision()
   {
      return division;
   }
   /**
    * getter for won
    */
   public int getWon()
   {
      return won;
   }   

   /**
    * getter for drew
    */
   public int getDrew()
   {
      return drew;
   }

   /**
    * getter for lost
    */
   public int getLost()
   {
      return lost;
   }   

   /**
    * increments the number of games won
    */   
   public void incWon()
   {
      won = won + 1;
   }

   /**
    * increments the number of games drawn
    */    
   public void incDrew()
   {
      drew = drew + 1;
   }

   /**
    * increments the number of games lost
    */    
   public void incLost()
   {
      lost = lost + 1;
   }

   /**
    * setter for division
    */
   public void setDivision(String aDivision)
   {
      division = aDivision;
   }

   public String toString()
   {
      return ("Team " + name + ", division: " + division + " stats: Won: " + won
       + ", drew: " + drew + ", lost: " + lost + ", points: " + getPoints());
   }


}
Matthew
  • 35
  • 5
  • in **recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)** what is division for and what is teamA,teamB string..it is name of team ? – Hasnain Ali Bohra Apr 19 '19 at 14:45
  • I suppose this: `teams.get(teamB); // updates wins for teamB` should actually be: `teams.get(division)` to get the set of teams, and then you will need an iterator to get the team you want, and apply the method on it. check this post: https://stackoverflow.com/questions/7283338/getting-an-element-from-a-set – Laguh Apr 19 '19 at 15:04
  • the test code we were given was ```leagueA.recordResult("Premier","Arsenal","Chelsea",2,1);``` as the values for the string. – Matthew Apr 20 '19 at 15:42

1 Answers1

1

I think you misunderstood the data structure requirement. You're looping through a list of teams, and never updating the loop variable. I believe the data structure should be something more like this:

private Map<String, Map<String, Team>> teams;

Using the division for the outer map key, and the team name for the inner map key. This separates div1's teamA from div2's teamA

This simplifies your recordResult method to pulling the specific team and updating them accorrdingly

public void recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)
   {
      Map<String, Team> teamKeys = teams.get(division);
      if(teamAScore > teamBScore)
      {
         teams.get(teamA).incWon(); // updates wins for teamA
      }
      else if (teamAScore < teamBScore)
      {
         teams.get(teamB).incWon(); // updates wins for teamB
      }
      else
      {
         teams.get(teamA).incDrew();
         teams.get(teamB).incDrew();
      }
   }
Jason Warner
  • 2,469
  • 1
  • 11
  • 15
  • thanks for the help, but when I compile the code above within the existing class, I get compiler errors of incompatible type within the addTeam() method.. specifically the ```this.teams.put(division, teamset); ``` what would i need to change in this method? – Matthew Apr 20 '19 at 16:51
  • the `teamSet` would need to be a Map, not a set. Essentially, you would have a Map of divisions to teams Maps - which would map the Team name to the Team object. – Jason Warner Apr 23 '19 at 05:47