Solved Comparing an object

Discussion in 'Plugin Development' started by Irantwomiles, Feb 21, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Irantwomiles

    I have an Object which has Team1, Team2, String, String. I have a manager class that creates/deletes or does anything related to the object itself.

    Code:
    
    if(cmd.equals(create)) {
    
      ObjectManager.getManager().create(Team1, Team2, String, String);
    
    }
    
    
    How would I check in a command that Team1 object equals a certain team and Team2 Object equals another team? Do I need to use the .equals() or == ? I found this online but I dont understand what it means by == checks reference point and .equals() checks if the objects are actually equal and which one do I need to use in this case as Team1 or Team2 could lose/gain members or change the team name.
     
  2. Offline

    mine-care

    You may use == if and only if the two objects you are comparing are the same whereby both pointers point to the same Object:

    Code:
            String one = new String("Hi");
            String two = one;
            System.out.println(one == two);
            System.out.println(one.equals(two));
    //Prints:
    //true
    //true
    
    Still .equals() would check if the pointers point to the same Object AND if the two Objects are 'equal'

    Code:
            String one = new String("Hi");
            String two = new String("Hi"); 
            System.out.println(one == two);
            System.out.println(one.equals(two));
    //prints:
    //false
    // true
    
    I would say that to be on the safe side use .equals making sure there are no null values involved ;)
     
  3. Offline

    Irantwomiles

    @mine-care Let me show you my class and see if I'm doing anything wrong.

    Code:
    public class TeamInvite {
    
        private Team team1;
        private Team team2;
        private String type;
        private GameType gametype;
       
        public TeamInvite(Team team1, Team team2, GameType gametype, String type) {
            this.team1 = team1;
            this.team2 = team2;
            this.type = type;
            this.gametype = gametype;
        }
       
        public void setTeam1(Team team1) {
            this.team1 = team1;
        }
       
        public void setTeam2(Team team2) {
            this.team2 = team2;
        }
       
        public void setType(String type) {
            this.type = type;
        }
       
        public void setGameType(GameType gametype) {
            this.gametype = gametype;
        }
       
        public Team getTeam1() {
            return this.team1;
        }
    
        public Team getTeam2() {
            return this.team2;
        }
       
        public String getType() {
            return this.type;
        }
       
        public GameType getGameType() {
            return this.gametype;
        }
    }
    
    Manager Class

    Code:
    public class TeamInviteManager {
    
        public static ArrayList<TeamInvite> invites = new ArrayList<>();
       
        private static TeamInviteManager ti;
    
        private TeamInviteManager() {}
    
        public static TeamInviteManager getManager() {
            if (ti == null)
                ti = new TeamInviteManager();
    
            return ti;
        }
       
        public void createInvite(Team team1, Team team2, GameType gametype, String type) {
           
            if(invites.contains(getTeamInvite(team1, team2))) {
                return;
            }
           
            TeamInvite invite = new TeamInvite(team1, team2, gametype, type);
           
            invites.add(invite);
           
        }
       
        public TeamInvite getTeamInvite(Team team1, Team team2) {
            for(TeamInvite invite : invites) {
                if(invite.getTeam1() == team1 && invite.getTeam2() == team2) {
                    return invite;
                }
            }
            return null;
        }
    
        public void deleteInvites(Team team) {
       
            Iterator<TeamInvite> iterator = invites.iterator();
           
            while(iterator.hasNext()) {
                TeamInvite invite = (TeamInvite) iterator.next();
           
                if(invite.getTeam1() == team || invite.getTeam2() == team) {
                    invite.setTeam1(null);
                    invite.setTeam2(null);
                    invite.setGameType(null);
                    invite.setType(null);
                    invites.remove(invite);
                }
            }
        }
    
    }
    Am I deleting the invite correctly?
     
  4. Offline

    MrGeneralQ

    Wel this is what you should do:

    Since you have a class team I would also create a class Member. This would have a variable private Team team;

    I would then create a public methode in there to get the team.

    public Team getTeam()
    {
    return this.team;
    }


    Now to compare :


    if(member.getTeam.equals(teamRed)
    {
    // member is on team red
    }

    I Hope you understand
     
  5. Offline

    Irantwomiles

    I don't understand :p. my team class already contains team members, leader, etc.
     
  6. Offline

    MrGeneralQ

    What is the part you don't understand?
     
  7. Offline

    Irantwomiles

    All of it to be frank. I just don't get the point and the reasoning behind it.
     
  8. Offline

    MrGeneralQ

    Well I'm not on my Computer right now so a detailed explenation would be for tomorrow if you don't mind.

    But bassicly you create classes so You can have them in each other.
     
  9. Offline

    Irantwomiles

    I think im going to wait for that detailed explanation :p Btw if you dont mind looking at the manager class I posted and see if I'm deleting the object correctly, if its not too much to ask.
     
  10. Offline

    MrGeneralQ

    Sure post it
     
  11. Offline

    Irantwomiles

    If you scroll up its the last code snippet I posted.
     
  12. Offline

    MrGeneralQ

    Well I would create other classes. There are a few things that doesn't make any sense.

    It's midnight here but tomorrow I'll give you a detailed explenation and now I would do it.
     
  13. Offline

    Irantwomiles

    Ok thanks, good night :)
     
    mine-care and MrGeneralQ like this.
  14. Offline

    mine-care

  15. Offline

    Irantwomiles

    Could you look at the code snippet and tell me if it looks right?
     
  16. Offline

    MrGeneralQ

    As I promised yesterday here is the detailed explenation:


    Bassicly you are working with custom classes. These are the Classes I would make in your case:

    • Team
    • TeamManager
    • Member

    Now how you must see the link between these classes:

    [​IMG]

    So with that being said we can know that Every Team can have 0 or more members.
    That means our class will have a List<Member>

    Now I asssume you know how constuctors work? So our member class will actually represent a player. But since we don't have access to create new player methods we created another class called "Member" and we give it a Player parameter using our constructor.

    We do the same thing for our Team class: We provide a parameter of the type String to pre-define a team name.

    as Being displayed above, Every member has sometimes a team. So in our class we know we can define a field Team.
    It would ook something like this:

    Now our final class is the TeamManager. We are going to use this class to store every Team and do stuff with it. We initialize the class from our main Class.

    In there we will save the list of all our Teams. So it will have a list Team.

    Now it's up to you. You have to create methods to get all the data from the classes. For ex:


    Code:
    public class TeamManager()
    {
       private List<Team> teamList;
    
    
    public List<Team> getTeams()
    {
      return this.teamList;
    }
    }
    That's an example of a method to get all Teams. Ofcourse all the other classes need methods too.
    Please note that this is basic Java knowledge if and you don't understand you should start by learning the basics of Java first.

    I'm not going to provide every code for you. You would simply just copy paste it, but carefully watch the picture and try to link them in your mind.

    Hope it helps
     
    mine-care likes this.
  17. Offline

    Irantwomiles

    Ok that makes sense. Also last night you said something on myself code doesn't make sense, I assume it was mg deleting method. Is there a better way of doing it?
     
  18. Offline

    MrGeneralQ


    for example: deleting a team;

    1. create a method in the TeamManager class removeTeamByName(String teamName); (remember every 'Team' class has a teamName String)
    2. for loop trough the teamList variable and get the name from the team. If the name is equal to your teamName parameter , then teamList.remove(teamFromForLoopVar);
    That should clear things up.
     
  19. Offline

    Irantwomiles

    So I guess my question is this. Do I need to reset the attributes of the team object? So when I'm about to remove the team from my list (i was already putting each team in the list and pretty much everything you explained). So do I need to do like team.setLeader(null) or does removing it from the teamList take care of that.
     
  20. Offline

    MrGeneralQ

    Well you can do that. However, then you should create a method (boolean) to check if the team has a leader. If not , then don't try to get the leader.

    Hope it makes sense. Just to avoid NPE
     
    Irantwomiles likes this.
  21. Offline

    Irantwomiles

    My problem was mmainly for the TeamInvite class I posted above. Whenever I would remove a team from a list and the same 2 teams created an invite it would pretty much make the same duel even if it had different game modes. But the nd for your help it definitely helped! Marked solved
     
  22. Offline

    MrGeneralQ

    Glad I could help You :)
     
Thread Status:
Not open for further replies.

Share This Page