Method does not correctly show expected result

Asked

Viewed 174 times

1

I have this interface

public interface Promocional {

    boolean aplicaDesconto(double porcentagem);
    
    default boolean aplicaDescontoDe10Porcento() {
        return aplicaDesconto(0.1);
    }
}

The class LivroFisico that extends the superclass Livro and implements the interface Promocional

public class LivroFisico  extends Livro implements Promocional{
    public LivroFisico(Autor autor) {
        super(autor);
    }

    @Override
    public boolean aplicaDesconto(double porcentagem) {
        // TODO Auto-generated method stub
        return true;
    }

}

And here in the method main it does not display properly.

main()

LivroFisico fisico = new LivroFisico(autor);
fisico.setValor(59.90);
if(fisico.aplicaDescontoDe10Porcento()) {
        System.out.println("Valor agora e: " + fisico.getValor());
    }

It displays 59.90 and not 53.91 which is the discounted amount.

I believe that in class LivroFisico which is mandatory the use of the method, is giving error.

2 answers

2


You have two problems.

Your problem is recurring these days. Most people learn to program by methods I don’t understand. It seems to be a thing of following recipes and not learning the concepts, the functioning of what you are using. Even being full of information about it in books, tutorials, blogs, and even here in various questions. I myself I answered a few hours ago about C#, but the problem applies to all languages. Just follow all the links to learn about the subject.

The most specific answer in Java is here.

In short: the processor for better performance treats data types float and double binary form and therefore cannot represent all numbers with decimal places, there is the so-called rounding problem and there is nothing you can do about it other than to use the right type of data when dealing with monetary values or representing something else that needs accuracy. This guy is the Bigdecimal.

In addition your discount method (aplicaDesconto()) is not doing any operation. It does not calculate any discount.

Behold What is the correct way to use the float, double and decimal types?.

  • Sorry for the inconvenience.

  • +1 for "It seems to be a thing of following recipes and not learning the concepts" =D

  • The guy is BigDecimal.

  • @Victorstafusa is true, confusing me with C# ;)

  • I just found a lot of conversation for a few questions. The use of the type is not related to the issue itself.

  • 1

    @Andréhenriques either you solve the existing problems or you will continue with the problem. It is evident that people make this mistake a lot. The other answer, for example, teaches you to do the wrong thing. I think you think this is good. I don’t think it and in general people here don’t think so. Teaching wrong is always a bad thing. But you can respond in your own way and see if the AP changes acceptance to yours. It seems to me that he has learned something from this. Some like to learn right, others prefer to do it anyway.

  • @Bigown, that type information was very good, but it applies to the comment and not the answer.

Show 2 more comments

1

A quick and simple way to solve this is to change your method of applicationso:

public boolean aplicaDesconto(double porcentagem) {
        setValor(getValor() - (getValor()*porcentagem))
        return true;
    }

This will cause him to actually multiply the value. Remember to create the gets and value sets.

public Double getValor() {
    return valor;
}

public void setValor(Double valor) {
    this.valor = valor;
}

Browser other questions tagged

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