How to store information about the date when player was added to team

Viewed 26

I have a problem. I dont know how to store current LocalDate in my program.

I have 2 classes:

public class Footballer extends Person {

    private int age;
    private List<Team> teams = new ArrayList<>();
    
    public Footballer(int age) {
        this.age = age;
    }
    
    public void addTeam (Team team1) throws IOException {
        teams.add(team1);
        team1.addFootballer(this);
    }

}

And similar class Team. I want to store LocalDate when method addTeam was called. How to do this?

1 Answers

Since this is a property of Footballer, you can add a LocalDate property to the class, then set it when the addTeam is invoked, and finally add a getter for that field. E.g.

public class Footballer extends Person {

   private int age;
   private List<Team> teams = new ArrayList<>();
   private LocalDate dateWhenPlayerAdd = null;

   public Footballer(int age) {
       this.age = age;
   }

   public void addTeam (Team team1) throws IOException {
       teams.add(team1);
       this.dateWhenPlayerAdd=LocalDate.now();
       team1.addFootballer(this);
   }

   public LocalDate getDateWhenPlayerAdd() {
       return this.dateWhenPlayerAdd;
   }
}
Related