How to calculate the use of a football team in a championship?

Asked

Viewed 6,600 times

2

I’m looking to do a media calculation of a football team in a certain league, for example, my team played two matches and won one. He would have 3 points from 6 points played and a 50%.

public final class Time {
    private String nome;
    private int vitorias = 0;
    private int empates = 0;
    private int derrotas = 0;
    private int numPartidas = 0;
    private int pontos;
    private float aproveitamento;

    public Time(String nome) {
        this.setNome(nome);
    }
    public void ganharPartida(){
        this.setVitorias(this.getVitorias() + 1);
        this.setPontos(this.getPontos() + 3);
        this.setNumPartidas(this.getNumPartidas() + 1);
    }
    public void empatarPartida(){
        this.setEmpates(this.getEmpates() + 1);
        this.setPontos(this.getPontos() + 1);
        this.setNumPartidas(this.getNumPartidas() + 1);
    }
    public void perderPartida(){
        this.setDerrotas(this.getDerrotas() + 1);
        this.setNumPartidas(this.getNumPartidas() + 1);
    }


    //GETTER E SETTER

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getVitorias() {
        return vitorias;
    }

    public void setVitorias(int vitorias) {
        this.vitorias++;
    }

    public int getEmpates() {
        return empates;
    }

    public void setEmpates(int empates) {
        this.empates = empates;
    }

    public int getDerrotas() {
        return derrotas;
    }

    public void setDerrotas(int derrotas) {
        this.derrotas = derrotas;
    }

    public int getNumPartidas() {
        return numPartidas;
    }

    public void setNumPartidas(int numPartidas) {
        this.numPartidas = numPartidas;
    }

    public float getAproveitamento() {
        return aproveitamento;
    }

    public void setAproveitamento(float aproveitamento) {
        this.aproveitamento = pontos;
    }

    public int getPontos() {
        return pontos;
    }

    public void setPontos(int pontos) {
        this.pontos = pontos;
    }

    @Override
    public String toString() {
        return "Time{" + "nome=" + nome + ", vitorias=" + vitorias + ", empates=" + empates + ", derrotas=" + derrotas + ", numPartidas=" + numPartidas + ", pontos=" + pontos + ", aproveitamento=" + aproveitamento + '}';
    }





}
  • 1

    And what is your doubt?

  • How I apply these rules in the harness

  • Apply where? What in the code has some problem, some difficulty?

  • 1

    It seems to me that he is having a hard time creating the logic just for the percentage of points. It would be something like (vitorias*3 + empates*1) / numPartidas*3

  • That’s right, thank you

1 answer

2


The calculation is the least, basic math. The logic is a bit complicated. I would actually take out most of the methods. There is a school of learning that has to create getters/setters for everything. I and many people consider this wrong. You should only create if it is absolutely necessary. I would take them all out. I took what made no sense. The queries to the fields can be useful, you love to change them seems project error. Let the methods that establish the result of a prank do this.

It seems to me that the use should not be modified, so I created a private method to calculate it and called it whenever information is changed. Actually I prefer not to do so, I find it more interesting to calculate when I need the data, so it decreases the size of the structure and facilitates the code, but I know if this was a requirement. I tried to get more information about what I needed but was unsuccessful.

I didn’t use the methods getters/setters internally because they are usually only useful for external access.

I changed the name that gives the result since the toString() is not fit for this.

It was nice to put the final, in general people do not and almost always is the best to do.

I hope that the answer serves beyond the specific problem since the structure of the project is very based on such "good practices" spread around and that is forming bad programmers because they do not form programmers, they create only copiers of recipes. Actually there are other things that could be done better, but for a simple exercise it doesn’t come to the point.

class HelloWorld {
    public static void main (String[] args) {
        Time time = new Time("Meu time");
        time.ganharPartida();
        time.empatarPartida();
        time.perderPartida();
        time.ganharPartida();
        System.out.println(time.Estatisticas());
    }
}

public final class Time {
    private String nome;
    private int vitorias;
    private int empates;
    private int derrotas;
    private int numPartidas;
    private int pontos;
    private float aproveitamento;

    public Time(String nome) {
        this.nome = nome;
    }
    public void ganharPartida() {
        vitorias++;
        pontos += 3;
        numPartidas++;
        CalcAproveitamento();
    }
    public void empatarPartida() {
        empates++;
        pontos++;
        numPartidas++;
        CalcAproveitamento();
    }
    public void perderPartida() {
        derrotas++;
        numPartidas++;
        CalcAproveitamento();
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public int getVitorias() {
        return vitorias;
    }
    public void setVitorias(int vitorias) {
        this.vitorias = vitorias;
        CalcAproveitamento();
    }
    public int getEmpates() {
        return empates;
    }
    public void setEmpates(int empates) {
        this.empates = empates;
        CalcAproveitamento();
    }
    public int getDerrotas() {
        return derrotas;
    }
    public void setDerrotas(int derrotas) {
        this.derrotas = derrotas;
        CalcAproveitamento();
    }
    public int getNumPartidas() {
        return numPartidas;
    }
    public void setNumPartidas(int numPartidas) {
        this.numPartidas = numPartidas;
        CalcAproveitamento();
    }
    public float getAproveitamento() {
        return aproveitamento;
    }
    private void CalcAproveitamento() {
        this.aproveitamento = (float)pontos / (numPartidas * 3) * 100;
    }
    public int getPontos() {
        return pontos;
    }
    public void setPontos(int pontos) {
        this.pontos = pontos;
    }
    public String Estatisticas() {
        return "Time{" + "nome=" + nome + ", vitorias=" + vitorias + ", empates=" + empates + ", derrotas=" + derrotas + ", numPartidas=" + numPartidas + ", pontos=" + pontos + ", aproveitamento=" + aproveitamento + '}';
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Probably I would do so:

public final class Time {
    private String nome;
    private int vitorias;
    private int empates;
    private int derrotas;
    public Time(String nome) {
        this.nome = nome;
    }
    public void ganharPartida() {
        vitorias++;
    }
    public void empatarPartida() {
        empates++;
    }
    public void perderPartida() {
        derrotas++;
    }
    public String Estatisticas() {
        return "Time{" + "nome=" + nome + ", vitorias=" + vitorias + ", empates=" + empates + ", derrotas=" + derrotas + ", numPartidas=" + (vitorias + empates + derrotas) + ", pontos=" + (vitorias * 3 +  empates) + ", aproveitamento=" + (float)(vitorias * 3 +  empates) / ((vitorias + empates + derrotas) * 3) * 100 + '}';
    }
}
  • "I have not used getters/setters methods internally because they are usually only useful for external access."

  • External use would mean what?

  • @Brunooliveira out of class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.