0

I am trying to make an erase function to delete the teams of the tournament using the team code (value c in the constructor). Firstly I want to check if that team exists in the objects I made in the main method. Is that possible to do that using an if statement?

Exercise:

Create a java application that stores data for various football teams. Each team has a name, a code and the current points in the championship. In your class create methods for registering new teams, erasing existing teams using their code and showing a list for all the teams currently active in the championship

package assignment.exercise4;

public class Data {
    private String  name = "";
    private  int code = 0;
    private static int register;
    private int erase;
    private int currentpoints = 0;

    public Data(int c, int points, String n) { //constructor
        code = c;
        this.currentpoints = points;
        name = n;
    }

    public void Erase(int c)
    {
        code = c;
        if(code != 0)
            System.out.println("Team  with Code: "+code+" has been erased" );
        else
            System.out.print("Team with code "+code+" does not exist!");
    }

    public void Register(String newTeam,int code)
    {
        name = newTeam;
        this.code = code;
        System.out.println("New Team " + name + " registered with code " + code);
    }

    public void print()
    {
        System.out.println("Team name: " + name + "\nTeam code: " + code + "\nTeam points: " + currentpoints + "\n");
    }

}

/*
    public static void main(String[] args) {
        System.out.println("\nList of Teams: \n");
        Data t1 = new Data(110,42,"Juventus");
        Data t2= new Data(105,45,"Manchester City");
        Data t3= new Data(240,50,"Barcelona");
        Data t4= new Data(122,36,"Arsenal");
        Data Team = new Data(0,0,""); //use for erase

        t1.print();
        t2.print();
        t3.print();
        t4.print();

        System.out.println("Teams erased: \n");

        Team.Erase(110);
        Team.Erase(122);
        Team.Erase(0);

        System.out.println("\n\nTeams Registered: \n");

        t1.Register("Real madrid", 11);
        t1.Register("Atletico Madric", 112);
    }

}
*/
Markus Mitterauer
  • 1,560
  • 1
  • 14
  • 28
  • If you have an array of `Team`s, you can use this `Arrays.asList(yourArray).contains(yourValue)`. – Eli Sadoff Dec 02 '16 at 15:08
  • Possible duplicate of [How can I test if an array contains a certain value?](http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value) – Eli Sadoff Dec 02 '16 at 15:08
  • 1
    Unrelated, but pick a convention for your ctor. E.g., pass in the same name as the property and always use `this.code = code`. It's not clear what you're asking--you just want to check if `c == code`? You don't have a collection of anything anywhere, we don't know what `Team` looks like, etc. – Dave Newton Dec 02 '16 at 15:08
  • I Made a function to erase teams. What I want to do is very simple. To use Team.Erase(Team code here). I want to check if the Team code exists in the objects I created in the main method. Like the example below in comments –  Dec 02 '16 at 15:10
  • what you could do is create a for loop that compares each value in the array. Because 'c' is an integer, create an integer array. – marshmellooooooos Dec 02 '16 at 15:11
  • I believe you have got it backwards. You should erase the data in the container, not in by using the data class itself. This will eventually lead to a static data storage in the data class and eventually to a lots of complicated bugs. Instead write a data storage class and let this class be responsible for adding removing, modifying, whatever_you_wanting the data. – patrik Dec 02 '16 at 15:11
  • "What I want to do is very simple." Not the way you've done it--you have four `Data` objects, completely disconnected, with nothing to erase them from. To "erase" something from a collection you need a collection. It seems like what you actually want is a `Collection` of `Teams` from which you can remove a `Team`. You do not have this yet. – Dave Newton Dec 02 '16 at 15:13
  • okay my assignment is as follows: I have to do this only with classes and objects not lists , etc. Create a java application that stores data for various football teams. Each team has a name, a code and the current points in the championship. In your class create methods for registering new teams, erasing existing teams using their code and showing a list for all the teams currently active in the championship. –  Dec 02 '16 at 15:18
  • If so, write a class called TeamDb with an array (which is not a list) of teams/Data. It is either this or writing to file/disk. All other approaches will surely fail in the end. – patrik Dec 02 '16 at 15:22
  • @patrik I made the teams in the main methods, using the constructor, I thing something is wrong in my code though –  Dec 02 '16 at 15:28
  • I would suggest, that you start by implementing something like a "Championship" class containing the teams... Registering or erasing teams from the championship are then functions there, not on the team itself. – Roland Dec 02 '16 at 15:55
  • You cannot "erase" a team from a list of teams without a list of teams. When I say "list" (which *is* an object, btw) I mean "arbitrary collection of objects". I don't care if it's a list, an array, a map, a set, whatever. The assignment explicitly states "showing a list"--it's not clear to me why you believe you can't have some sort of collection of `Team`s. – Dave Newton Dec 02 '16 at 15:58
  • 1
    @Eternal My point is about the same as the later comments. In main() you only have scattered Teams (which you btw call Data, which is makes it really hard to know what type of data we speak about). You need some kind of object which collects all your data in a structured way. As it is now you do not even have anything to add data to or erase data from. It as if you say that you want to remove some letter from a text. If you have written 4 letters in sand scattered all over the beach you do not have a text, right? Having a text requires that you have written something coherent. – patrik Dec 02 '16 at 16:32

3 Answers3

0

What are you trying to erase the teams from?

If they were in a list, for example...

       Data t1 = new Data(110,42,"Juventus");
       Data t2= new Data(105,45,"Manchester City");
       Data t3= new Data(240,50,"Barcelona");
       Data t4= new Data(122,36,"Arsenal");

       List<Data> teams = Arrays.asList(t1, t2, t3, t4);

...you could create a list with a team erased like this...

public List<Data> erase(List<Data> team, int id) {
    return team.stream()
               .filter(t -> t.getId() != id)
               .collect(Collectors.toList());
}

So...

List<Data> remainingTeam = erase(team, 122); // Removes Arsenal

...would remove the first element from the list

BretC
  • 4,141
  • 13
  • 22
0

I will not answer this to elaborately since it is homework. I will try to give you a hint though.

If you have a team and want to do something with it. Otherwise you just have a team which just stays there in a particular scope (if you do not know what scope is, look it up!). If you have a team you most likely want do do something with it. In this case you seem to want to store information about the teams to use in a championship. Important to note here is that the teams are not the focus here. The real focus is the Championship. The teams are just a part of the championship. There can still be a championship even if all teams does not choose to participate. But you want all teams choosing to participate to be registered to this particular championship (eg UEFA Champions League).

This leads to something called aggregate or association depending on how hard you want to tie the object to the championship. However you do probably not need to pursue these terms any further at this point. What is important to remember is that there is an "has a" relation between the championship and the teams. The championship "has a" collection of participating teams. This is normally reflected in this way in code,

public class Championship {
   private Team[] teams; // Or List<Team>, Collection<Team>, HashMap<Team>, ...
}

The Championship can then have methods for registering a team, removing a team, updating status, etc...

public void register(Team t) {
    if (numberOfTeams < teams.length) {
        teams[numberOfTeams] = t; // Index starts at zero
        numberOfTeams++;
    } else {
        throw new IndexOutOfBoundsException("The list is full. " + 
            "No more teams may be registered!")
    }
}

Even though the function erasing a team was requested, I believe I will not write it down. This design is so different from your original intent, so that writing the erase function will likely solve your complete homework. However, you do actually not have to erase the team it is perfectly possible to just overwrite the position with the next team as,

teams[i] = teams[i+1];

Hope this helps!

Community
  • 1
  • 1
patrik
  • 4,506
  • 6
  • 24
  • 48
-1

Short answer:

public void erase(int id) {
  // who needs an if statement, if we can use predicates?
  teams.removeIf(team -> team.getId() == id); 
}

But this will not work with your current code. Your current code misses the container for your teams.

Longer answer:

For the fun of it. Solving your homework:

class Team {
  int id;
  String name;
  int points;
  Team(int id, String name, int points) {
    this.id = id;
    this.name = name;
    this.points = points;
  }

  @Override
  public String toString() {
     // ugly formatted... another homework? ;-)
     return "Team '" + name + "' (" + id + "): " + points;
  }
}

Note, that I will not add any getter or setter, nor will I care about visibility here. I will leave that as another homework for you.

class Championship {
  List<Team> teams = new ArrayList<>();

  void register(Team team) {
    teams.add(team);
  }

  void erase(int id) {
     teams.removeIf(team -> team.id == id);
  }

  @Override
  public String toString() {
     // for additional fun... sorted by descending points
     return "=== Championship table ===\n"
             + teams.stream()
             .sorted((o1, o2) -> Integer.compare(o2.points, o1.points))
             .map(Objects::toString)
             .collect(Collectors.joining("\n"));
  }
}

Somewhere else:

public static void main(String[] args) {
  Championship championship = new Championship();
  championship.register(new Team(1, "not the best ones", 3));
  championship.register(new Team(2, "The better ones", 7));
  championship.register(new Team(3, "The winners", 11));
  System.out.println(championship);

  championship.erase(3);
  System.out.println(championship);
}

Output:

=== Championship table ===
Team 'The winners' (3): 11
Team 'The better ones' (2): 7
Team 'not the best ones' (1): 3
=== Championship table ===
Team 'The better ones' (2): 7
Team 'not the best ones' (1): 3

Too much of information? Just start with something like a championship-class or at least use a collection of Teams (e.g. List<Team>).

By the way... Do not deliver this solution as your homework, except you understand what is going on and you can explain it with your own words. Otherwise you are only betraying yourself.

Roland
  • 22,259
  • 4
  • 57
  • 84
  • 1
    first, I don't think it is a good idea to give solutions to homework-questions, see [How do I ask and answer homework questions?](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) on Meta. – Markus Mitterauer Dec 02 '16 at 16:53
  • second, I think the class *Eternal* is attending is not yet on a level where the could handle *Streams* and *Lambdas*... – Markus Mitterauer Dec 02 '16 at 16:55
  • Thanks for the link. Didn't know that. However even if the solution is complete now, it will not necessarily be understandable unless some basics are understood. I hope that the hint at the bottom leads the right way nonetheless. – Roland Dec 02 '16 at 16:59