In: Computer Science
Java OOP - how do I avoid using getter and setter method in player class for better privacy? I am told to use regular method instead. but I dont know how
Thank you
-----------------------------------------------------------------------------------------------------------
package MainPackage;
import java.util.ArrayList;
public class SoccerTeam {
private ArrayList<Player> list;
public SoccerTeam(int maxSubmission) {
list = new ArrayList<>(maxSubmission);
}
public boolean addPlayer(String newPlayer) {
if(newPlayer == null || newPlayer.isEmpty() == true) {
return false;
}
for(Player player : list) {
if(player.getName() == newPlayer) {
return false;
}
}
Player player = new Player(newPlayer);
list.add(player);
return true;
}
public int numOfTeamMembers() {
return list.size();
}
public boolean addFile(String name, int[] goal) {
boolean isPlayerPresent = false;
for(Player player : list) {
if(player.getName() == name) {
if(player.getNumOfSubmissions() <= 10) {
isPlayerPresent = true;
player.increaseNumOfSubmissions();
int goals = 0;
for(int i : goal) {
goals = goals + i;
}
if(player.getGoals() < goals ) {
player.setGoals(goals);
}
}
}
}
if(!isPlayerPresent) {
return false;
}
return true;
}
public int goals(String name) {
if(name == null || name.isEmpty() == true) {
return -1;
}
for(Player player : list) {
if(player.getName() == name) {
return player.getGoals();
}
}
return -1;
}
public int numFile(String name) {
for(Player player : list) {
if(player.getName() == name) {
return player.getNumOfSubmissions();
}
}
return -1;
}
}
public class Player {
private int numOfSubmissions;
private int goals;
private String name;
public Player(String name) {
numOfSubmissions = 0;
goals = 0;
this.name = name;
}
public int getNumOfSubmissions() {
return numOfSubmissions;
}
public void increaseNumOfSubmissions() {
this.numOfSubmissions = numOfSubmissions + 1;
}
public int getGoals() {
return goals;
}
public void setGoals(int goals) {
this.goals = goals;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
import java.util.ArrayList;
public class SoccerTeam {
private ArrayList<Player> list;
public SoccerTeam(int maxSubmission)
{
list = new
ArrayList<>(maxSubmission);
}
public boolean addPlayer(String newPlayer)
{
if (newPlayer == null ||
newPlayer.isEmpty() == true) {
return
false;
}
for (Player player : list) {
if (player.name
== newPlayer) {
return false;
}
}
Player player = new
Player(newPlayer);
list.add(player);
return true;
}
public int numOfTeamMembers() {
return list.size();
}
public boolean addFile(String name, int[]
goal) {
boolean isPlayerPresent =
false;
for (Player player : list) {
if
(player.name== name) {
if (player.numOfSubmissions <= 10) {
isPlayerPresent = true;
player.increaseNumOfSubmissions();
int goals = 0;
for (int i : goal) {
goals =
goals + i;
}
if (player.goals < goals)
{
player.goals=goals;
}
}
}
}
if (!isPlayerPresent) {
return
false;
}
return true;
}
public int goals(String name) {
if (name == null || name.isEmpty()
== true) {
return -1;
}
for (Player player : list) {
if (player.name
== name) {
return player.goals;
}
}
return -1;
}
public int numFile(String name) {
for (Player player : list) {
if (player.name
== name) {
return player.numOfSubmissions;
}
}
return -1;
}
}
class Player {
int numOfSubmissions;
int goals;
String name;
public Player(String name) {
numOfSubmissions = 0;
goals = 0;
this.name = name;
}
public void increaseNumOfSubmissions() {
this.numOfSubmissions =
numOfSubmissions + 1;
}
}
Changed, remove getters and setters and removed the private access specifiers