I'm working on a program that allows a user to choose a home and away football team between four teams. I created a generic superclass team that defines the points assigned per safety/field goal/touchdown. A random number is generated and then based on that number the program steps through a conditional if/else statement to determine action and points.
This is in the SuperClass:
public void possessionPoints()
{
if(points<lowNopoints){
score = noPoints;
totalScore = totalScore + score;
System.out.println("No points, plus " + score);
}
else if(points<lowSafetypoint){
score = safetyPoint;
totalScore = totalScore + score;
System.out.println("Safety, plus" + score);
}
else if(points<lowFieldgoal){
score = fieldGoal;
totalScore = totalScore + fieldGoal;
System.out.println("Field goal, plus" + score);
}
else{
score = touchDown;
totalScore = totalScore + touchDown;
System.out.println("Touchdown, plus" + score);
}
ArrayList<Integer> totalScore;
totalScore = new ArrayList<>();
totalScore.add(score);
//the sum score
int sum = totalScore.stream().mapToInt(Integer::intValue).sum();
System.out.println("Current score is: " + sum);
}
Note: above totalScore
is intialized as public static int totalScore = 0;
Throughout it all, I want to keep track of totalScore
. I have this setup in my superclass, however, when the program is run it adds up the score through the entire game and does not differentiate between teams.
Output:
Home team action. No points, plus 0 Current score: 0
Away team action. Field goal, plus3 Current score: 3
Home team action. Field goal, plus3 Current score: 6
Away team action. Field goal, plus3 Current score: 9
Home team action. Safety, plus2 Current score: 11
Also, if it helps, this is all that I set in the each subclass for the other teams below. I do not do anything with totalScore
.
public class PackersSub extends GenericSuper{
public PackersSub()
{
lowNopoints = 4;
lowSafetypoint = 5;
lowFieldgoal = 7;
}
Any ideas on how to fix this issue? I want to keep track of totalScore
per team. Thank you!