I am fairly new to java (my first college course for it) and with everything being online, I'm having a hard time understanding things - so I apologize if this is stupid or obvious. In this project, I have two classes named "Basketball" and "BasketballDemo". I am using a variety of methods to keep the score of a game between two basketball teams. The methods used are all in 'Basketball', while a 'while loop' lives in the other method (BasketballDemo) to keep running the methods when specified until the game ends. I am trying to use 'getters' and 'setters' to transfer the values between the two, but I am severely confused - no matter what I do, the values in 'BasketballDemo' are always 0 after running no matter how I slice it. I have done a profuse amount of research on how exactly it works, but my head is having a very hard time with what I'm sure for many is an easy subject. I apologize in advance for the brevity of this code - because I don't know where the issue is I don't know exactly where in the code the problem is situated. Here are the two classes:
Basketball.java:
import java.util.Scanner;
public class Basketball {
private final String teamOne = "Lions";
private final String teamTwo = "Bears";
private int teamOneScore = 0;
private int teamTwoScore = 0;
private String gameState = "ongoing";
public int scoreOnePoint(String whatTeam) {
if (whatTeam.equalsIgnoreCase("Lions")) {
Basketball newScore = new Basketball();
newScore.setTeamOneScore(newScore.getTeamOneScore() + 1);
System.out.println("The Lions' score is now: " + newScore.getTeamOneScore() + ".");
return newScore.getTeamOneScore();
}
else if (whatTeam.equalsIgnoreCase("Bears")) {
Basketball newScore = new Basketball();
newScore.setTeamTwoScore(newScore.getTeamTwoScore() + 1);
System.out.println("The Lions' score is now: " + newScore.getTeamTwoScore() + ".");
return newScore.getTeamTwoScore();
}
else {
System.out.println("ERROR: Entered invalid team name. Please try again.");
return 0;
}
}
public int scoreTwoPoints(String whatTeam) {
if (whatTeam.equalsIgnoreCase("Lions")) {
Basketball newScore = new Basketball();
newScore.setTeamOneScore(newScore.getTeamOneScore() + 2);
System.out.println("The Lions' score is now: " + newScore.getTeamOneScore() + ".");
return newScore.getTeamOneScore();
}
else if (whatTeam.equalsIgnoreCase("Bears")) {
Basketball newScore = new Basketball();
newScore.setTeamTwoScore(newScore.getTeamTwoScore() + 2);
System.out.println("The Lions' score is now: " + newScore.getTeamTwoScore() + ".");
return newScore.getTeamTwoScore();
}
else {
System.out.println("ERROR: Entered invalid team name. Please try again.");
return 0;
}
}
public int scoreThreePoints(String whatTeam) {
if (whatTeam.equalsIgnoreCase("Lions")) {
Basketball newScore = new Basketball();
newScore.setTeamOneScore(newScore.getTeamOneScore() + 3);
System.out.println("The Lions' score is now: " + newScore.getTeamOneScore() + ".");
return newScore.getTeamOneScore();
}
else if (whatTeam.equalsIgnoreCase("Bears")) {
Basketball newScore = new Basketball();
newScore.setTeamTwoScore(newScore.getTeamTwoScore() + 3);
System.out.println("The Bears' score is now: " + newScore.getTeamTwoScore() + ".");
return newScore.getTeamTwoScore();
}
else {
System.out.println("ERROR: Entered invalid team name. Please try again.");
return 0;
}
}
public void finished() {
Scanner isFinished = new Scanner(System.in);
System.out.println("Are you sure the game finished? (Y/N):");
String finished = isFinished.nextLine();
if (finished.equalsIgnoreCase("Y")) {
Basketball isFinishedNow = new Basketball();
isFinishedNow.setGameState(finished);
if (isFinishedNow.getTeamOneScore() > isFinishedNow.getTeamTwoScore()) {
System.out.println("The Lions win!");
}
else if (isFinishedNow.getTeamOneScore() < isFinishedNow.getTeamTwoScore()) {
System.out.println("The Bears win!");
}
else {
System.out.println("It's a tie!");
}
System.out.println("The game is finished!");
}
else {
System.out.println("We aren't done here yet!");
}
}
public int scoreCheck(String whatTeam) {
if (whatTeam.equalsIgnoreCase("Lions")) {
Basketball checkScore = new Basketball();
System.out.println("The Lions are currently at " + checkScore.getTeamOneScore() + " points.");
return checkScore.getTeamOneScore();
}
else if (whatTeam.equalsIgnoreCase("Bears")) {
Basketball checkScore = new Basketball();
System.out.println("The Bears are currently at " + checkScore.getTeamTwoScore() + " points.");
return checkScore.getTeamTwoScore();
}
else {
System.out.println("ERROR: You entered an invalid team name, please try again.");
return 0;
}
}
public void winningTeam() {
Basketball whoIsWinning = new Basketball();
if (whoIsWinning.getTeamOneScore() > whoIsWinning.getTeamTwoScore()) {
System.out.println("The Lions are winning!");
}
else if (whoIsWinning.getTeamOneScore() < whoIsWinning.getTeamTwoScore()) {
System.out.println("The Bears are winning!");
}
else {
System.out.println("It's currently a tie!");
}
}
public int getTeamOneScore() {
return teamOneScore;
}
public void setTeamOneScore(int newScore) {
this.teamOneScore = newScore;
}
public int getTeamTwoScore() {
return teamTwoScore;
}
public void setTeamTwoScore(int newScore) {
this.teamTwoScore = newScore;
}
public String getGameState() {
return gameState;
}
public void setGameState(String newGameState) {
this.gameState = newGameState;
}
public static void main(String[] args) {
}
}
BasketBallDemo.java:
import java.util.Scanner;
public class BasketballDemo {
public static void main(String[] args) {
Basketball game = new Basketball();
while (game.getGameState() == "ongoing") {
Scanner getUpdateOne = new Scanner(System.in);
System.out.println("Enter the team that scored, or 'finished' if the game is over: ");
String gameUpdateOne = getUpdateOne.nextLine();
Scanner getUpdateTwo = new Scanner(System.in);
System.out.println("Enter the amount scored (1/2/3): ");
int gameUpdateTwo = getUpdateTwo.nextInt();
if (gameUpdateOne.equalsIgnoreCase("Finished")) {
game.finished();
}
else if (gameUpdateOne.equalsIgnoreCase("Lions")) {
if (gameUpdateTwo == 1) {
game.scoreOnePoint("Lions");
game.scoreCheck("Lions");
game.scoreCheck("Bears");
game.winningTeam();
}
else if (gameUpdateTwo == 2) {
game.scoreTwoPoints("Lions");
game.scoreCheck("Lions");
game.scoreCheck("Bears");
game.winningTeam();
}
else if (gameUpdateTwo == 3) {
game.scoreThreePoints("Lions");
game.scoreCheck("Lions");
game.scoreCheck("Bears");
game.winningTeam();
}
}
else if (gameUpdateOne.equalsIgnoreCase("Bears")) {
if (gameUpdateTwo == 1) {
game.scoreOnePoint("Bears");
game.scoreCheck("Lions");
game.scoreCheck("Bears");;
game.winningTeam();
}
else if (gameUpdateTwo == 2) {
game.scoreTwoPoints("Bears");
game.scoreCheck("Lions");
game.scoreCheck("Bears");
game.winningTeam();
}
else if (gameUpdateTwo == 3) {
game.scoreThreePoints("Bears");
game.scoreCheck("Lions");
game.scoreCheck("Bears");
game.winningTeam();
}
}
else {
System.out.println("ERROR: invalid entry, please try again.");
}
}
}
}