I am not managing to modify the value of the Book class that is a Book

Asked

Viewed 100 times

0

I’ve done everything and nothing of the value be modified... how do you do? I used inheritance in the physical book, the class is like this:

class Livrofisico extends Livro{

    public Livrofisico (Autor autor){
        super(autor)
    }

    public double getTaxaImpressao(){
        return this.getValor() + 5.0
    }
}

Book class:

package crud

class Livro {

    String nome
    String descricao
    double valor
    String isbn
    Autor autor


    void mostrarDetalhes() {
        println "mostrando detalhes do livro: "
        println "Nome: " + nome
        println "Descrição: " + descricao
        println "ISBN " + isbn
        println "Valor: "+ valor
        autor.mostrarDetalhes()
        println "-----------------------------"
    }
        public Livro(Autor autor) {
        this()
        this.autor = autor
        }

        public Livro(){
            this.isbn = "0000-0000000-000000-000000-00"
            this.nome = ""
        }
}


package crud

class Start {
    static main(args) {
        Autor autor = new Autor()
        autor.nome = "Aline Gonzaga"
        autor.email ='[email protected]'
        autor.cpf =' 8695649864496'

        Livro livro = new Livro(autor)
        livro.nome='Java: desvendando o segredo para ser mestre em Java'
        livro.descricao= 'Trata de um guia para aperfeiçoar em java'
        livro.valor =65.65
        //livro.isbn= "8975849-54-5665-34-3-324-656-32-34-123"
        //livro.autor = autor

        Autor autor2 = new Autor()
        autor2.nome = "Gonzaga"

        Livrofisico fisico = new Livrofisico(autor2)
        fisico.nome =" javaScript"
        fisico.valor =39.99
        fisico.getTaxaImpressao()
        fisico.mostrarDetalhes()


        livro.mostrarDetalhes()

        Ebook ebook = new Ebook()

        Autor outroAutor = new Autor()
        outroAutor.cpf ='754.548.545-34'
        outroAutor.email='[email protected]'
        outroAutor.nome='Jesus Cristo'

        Livro outroLivro = new Livro(outroAutor)
        outroLivro.descricao =' Como fazer?'
        outroLivro.isbn = ' 8754868596845986946'
    //  outroLivro.nome =''
        outroLivro.valor = 467.99
    //  outroLivro.autor = outroAutor
        outroLivro.mostrarDetalhes()

    }
}
  • Renan I’m not getting the value of the physical book. I’m not able to change since it has a printing fee, the ebook doesn’t need the fee. I want to include the rate in the printed book... : /

  • That’s groovy, don’t worry.

  • Help me? Yes?

  • @Alinegonz Explain better what is going wrong, which line is causing problems, what error is displayed etc.

  • The value it has in the book needs to be added to what it has in getTaxAmpresso(){} of the Librofisico class, but it is not what is occurring...

  • No error in the console, the problem is that it does not sum...

  • If you can edit the question with the output obtained and the desired output. It will help to understand your problem.

Show 2 more comments

2 answers

2


Some initial details that do not cause major problems, but get used to do right:

  • Try to indent and space correctly, make it easy to understand what is happening
  • Avoid mixing methods that present data with those that manipulate the object. I understand this is just an example, but it’s common for people to learn this way and then continue to do the same for the rest of their lives.
  • I don’t know Groovy, but you sure need to call this() within the builder this way? This construction seems strange to me.
  • You are holding a monetary value in double that gives calculation errors, no one will lose money in an example to learn, but if you keep doing this will cause harm.
  • I wonder if Livro should not be an abstract class and even call LivroAbstract (some people use this convention). It seems strange to me to be able to create a book and a physical book. If you change this, the concrete class can only be called Livro, what would make more sense.

Other than that I had no problem, as it may be seen on the ideone. I had to make adaptations because the code was not compileable in the state it was. And I gave one organized in style. If you add information, I try to improve the response.

I also put in the Github for future reference.

  • I need the physical book because it has the printed version and the ebook book version. What happens is that the physical book is charged a printing fee which does not occur cm ebook book that does not have this printing value. It was clear?

  • how best to save money?

  • This I understood. But I talked about something else. I don’t know if Groovy has something different, Java uses BigDecimal.

  • But you don’t have a solution for me? :(

  • Um, I’m just gonna comment because I put the this() inside the constructor. If the class had constructor without arguments the value of ISBN would only be initialized if the constructor received an author. So to solve this I put this to dazzle the call to the other constructor.

  • As I saw no problem, I see no solution. It’s all working. These builders seem all wrong. But I could be wrong. So I Linkei another question on the subject.

  • I will read yes. You are adding up the print rate there??

  • Click up there and see running right.

  • Click on top?

  • http://ideone.com/oJqy8n

  • I solved the problem!!!!!!

  • So tell me which one it is, because I showed that it doesn’t exist in the code posted.

  • code Here it is @bigown

  • This is conceptually wrong. There is a method that says it will take a value and inside it modifies another value. This is very wrong, misleads who will use the method. It may not even make a difference in this example of learning that won’t actually be used somewhere important, but wrong training will produce wrong results. That is, it was all right, now it’s wrong. If you want to change the value, create something that explicitly changes the value. Although this whole logic doesn’t make sense. Either printing is isolated or it’s part of the price. You’re coding right but structuring wrong

  • This is already beyond the scope of this question. It would take another to not mess up all the content.

  • Not how it works here. We are a question and answer site and not individual consulting.

Show 11 more comments

1

If the value is not being added up try this:

public double getTaxaImpressao(Livro livro){ return livro.getValor() + 5.0; } }

Or

public double getTaxaImpressao(Livro livro){ Double valor = livro.getValor(); return valor + 5.0; } }

If the value is not being displayed try this:

Double valor = fisico.getTaxaImpressao(); System.out.println(valor);

  • But friend, this is not right. : / Author has no value, only the book...

Browser other questions tagged

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