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

2

I want to do a calculation of average use of a football team in a certain championship, for example, my team played two matches and won one. He would have 3 points from 6 points and a 50% advantage.

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 + '}';
    }





}
    
asked by anonymous 11.08.2017 / 00:02

1 answer

2

The calculation is less, basic math. The logic is a bit complicated. In fact I would take most of the methods. There is a learning school that has getters / setters set up for everything. I and many people consider this wrong. You should only create it if it is absolutely necessary. I'd take them all away. I took away what made no sense. The queries to the fields can be useful, love changing them looks like design error. Lets the methods that set the result of a match do that.

It seems to me that the exploit should not be modified, so I created a private method to calculate it and called it whenever information changes. 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 did not succeed.

I have not used getters / setters internally because they are generally only useful for external access.

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

It was cool to have final , people usually do not put it and almost always is the best thing to do.

I hope the answer goes beyond the specific problem since the project structure is very much based on the "good practices" out there that are forming bad programmers because they are not programmers, they only create revenue copiers. In fact there are other things that could be done better, but for a simple exercise is not the case.

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 + '}';
    }
}

See running on ideone . And no Coding Ground . Also I placed in GitHub for future reference .

I would probably do this:

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 + '}';
    }
}
    
11.08.2017 / 00:43