I am currently building a small project that is supposed to keep track of recipes. Each recipe has a bunch of information, including a rating for said recipe. Since the rating has some additional logic i put it in it's own class. Now i am thinking about how to call a rating vote on my recipe object:
public class Recipe {
private Rating rating = new Rating();
public Rating getRating() {
return this.rating;
}
public void submitVote(int score) {
this.rating.vote(score);
}
}
public class Rating {
private int numberVotes;
private int totalScore;
public double getAverage() {
return totalScore / (double) numberVotes;
}
public void vote(int score) {
// Assure that score is within defined boundaries, for example 0-5
totalScore += score;
numberVotes++;
}
}
public class Main {
public static void main(String[] args) {
Recipe r = new Recipe();
// Option 1
r.getRating().vote(5);
// Option 2
r.submitVote(5);
}
}
Which of these is preferable?