Encapsulation and Get and Set methods

0

I'm continuing my studies on object-oriented Java programming.

I'm currently studying encapsulation and get and set methods and came across the following exercise:

  

Encapsulate the value attribute of the Ticket class.

     

This attribute should not be set directly. The user of the class informs the value and the student. If the student is approved a 10% discount on the ticket amount is granted and the get method must display the amount with the discount granted.

And I created this code:

Student Class:

package Academico;

public class Aluno {

    public String nome;
    private String matricula;
    private String curso;
    protected Disciplina disciplina[];

}

Class Discipline:

package Academico;

public class Disciplina {

    String nome;
    double nota1, nota2, nota3, nota4;

}

Ticket Class:

package Financeiro;

import Academico.*;

public class Boleto {

    private Aluno aluno;
    private String referencia;
    private double valor;

    public Boleto(Aluno aluno, String referencia, double valor){
        this.aluno = aluno;
        this.referencia = referencia;
        this.valor = valor;
    }

    public double desconto() {
        disciplina = new Disciplina[3];
        if(disciplina == true){
            this.valor = this.valor-(this.valor*0.1);
            return this.valor;
        }
    }

    public double getValorDesconto() {
        return this.valor;
    }

}

Is there any error in constructing the code, in the encapsulation and in the methods Get and Set ?

    
asked by anonymous 27.03.2016 / 21:01

1 answer

1
  

The question was edited, I did based on the original code posted

The exercise already seems to be very poorly defined. So it's hard to even say what's right. No more accurate information can not say or help to do right, does not say what is right.

In order not to be left unanswered, I'll make a slight improvement and ask questions:

package Financeiro;

import Academico.Disciplina;

public class Boleto {
    private Aluno aluno;
    private String referencia; //o exercício não fala disto
    private double valor; //seria melhor usar um BigDecimal, mas para exercício ok

    public Boleto(Aluno aluno, String referencia, double valor){
        this.aluno = aluno;
        this.referencia = referencia;
        this.valor = valor;
    }

    public Aluno getAluno() { return aluno; }

    public String getReferencia() { return referencia; }

    public double getValor() { return valor; }

    public double desconto() { //isto faz e retorna o desconto, o nome não é bom
        disciplina = new Disciplina[3]; //isto não parece fazer o menor sentido,nem compila
        if (disciplina) { //onde está a verificação de aprovação?
            valor -=  valor * 0.1;
            return valor;
        }
    }

    public double getValorDesconto() { //qual o intuito disto? O requisito não pede
        return valor; //isto parece estar errado se o método faz sentido existir
    }
}

Possibly there are missing methods set , but I do not know if I was to do this. The fact that the attribute should not be set directly is that it should not be public and, in theory, can be indirectly set via a setXXX() method.

What prevents you from calling the discount multiple times and even zeroing the value? I know that for an exercise this should not be very important, but it's something to think about.

I think the rebate calculation should be done in the constructor and there is no desconto method, so I think it works better.

Possibly a little better code:

public class Boleto {
    private Aluno aluno;
    private String referencia;
    private double valor;

    public Boleto(Aluno aluno, String referencia, double valor) {
        this.aluno = aluno;
        this.referencia = referencia;
        this.valor = valor * (aluno.ÉAprovado() ? 0.9 : 1;
    }
    public double getValor() { return valor; }
}

Obviously the ÉAprovado() method is part of the Aluno class, after all this is a student property and should be defined there. The ticket is only a consumer of student information. Each class with its responsibility. It will have an algorithm looking at the student's grades in the subjects and will return a boolean indicating the student's condition. The approval could even be defined by a policy class, but that is not required for this algorithm.

    
27.03.2016 / 21:29