I am not able to modify the value of the class PhysicalBook which is a Book

0

I've done everything and nothing of the value has been modified ... How do? I used inheritance in the physical book, the class looks 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()

    }
}
    
asked by anonymous 26.12.2015 / 05:24

2 answers

2

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

  • Try indenting and spacing correctly, make it easier to understand what is happening
  • Avoid mixing methods that present data with those that manipulate the object. I understand that this is just an example, but it is common for people to learn this way and then continue to do the same for the rest of their lives.
  • I do not know Groovy, but are you sure you need to call this() within the builder in this way? This construction looks strange to me.
  • You are saving a monetary value in double , which gives calculation errors, nobody will lose money in an example to learn, but if you continue doing this, it will cause damages.
  • 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 , which would make more sense.

Otherwise, I had no problem, such as seen in ideone . I had to make adaptions because the code was not buildable in the state it was. And I gave one organized in style. If you add information, I try to improve the answer.

    
26.12.2015 / 12:52
1

If the value is not being added 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);

    
26.12.2015 / 17:35