How to add objects to a repository class? And how can the user choose the amount of array in which he will add the data?

Asked

Viewed 44 times

0

Hey, here’s my question. I would like to know how to read the user data in main as follows: I have an array of up to 10 for the user to put the book data, but he can choose less. And another, after the user fills in the book data, how to add these books in the repository class? In main I have to fill the books¹, access toString of the repository class with all the books registered², get the discount and finally print the book with discount³.

book class

public double obterDesconto(int taxa) {
    return valor - (taxa/100);
}


@Override
public String toString() {
    return "Livro [titulo=" + titulo + ", codigo=" + codigo + ", editora=" + editora + ", valor=" + valor + "]";
}
public Livro(String titulo, String codigo, String editora, double valor) {
    super();
    this.titulo = titulo;
    this.codigo = codigo;
    this.editora = editora;
    this.valor = valor;
}
public Livro() {

}




public String getTitulo() {
    return titulo;
}

public void setTitulo(String titulo) {
    this.titulo = titulo;
}

public String getCodigo() {
    return codigo;
}

public void setCodigo(String codigo) {
    this.codigo = codigo;
}

public String getEditora() {
    return editora;
}

public void setEditora(String editora) {
    this.editora = editora;
}

public double getValor() {
    return valor;
}

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

}

Repository class

public Repositorio() {
    super();
    livros = new Livro[10];
    livros[0] = new Livro();
    livros[1] = new Livro();
}

@Override
public String toString() {
    return "Repositorio [livros=" + Arrays.toString(livros) + "]"+"\n";
}

public Livro obterLivro(String codigo) {
    for(int i = 0; i < 10; i++) {
        if(livros[i] != null) {
                if(livros[i].getCodigo().equals(codigo)) {
                    return livros[i];
                }
        }
    }
    return null;
}

public boolean addLivro(Livro livro) {
    for(int i=0;i<10;)
        if(livros[i] != null)
            return false;
        else {
            livros[i] = livro;
            return true;
    }
    return false;
}

}

1 answer

0

Hello! First, you can take from your constructor, in the repository class those instances of books that you do up there.

 livros[0] = new Livro();
livros[1] = new Livro();

Once done, in your main you can have a repository and a scanner:

Repositorio repo = new Repositorio();
Scanner sc = new Scanner(System.in);

With the scanner you can record what the user type in the console and assign to its variables. Having this, you can make a simple logic by using a while to some system.out.println, that will instruct your user that they can press 1 to continue buying/adding books, or 2 to finalize the purchase. In his while you can add an auxiliary variable that will count how many books have already been selected, you can use the value of this variable to know in which place in your array you will add another book. That way you can review your for, which is traversing the vector in its repository class. Then you can tell your while that if the auxiliary reaches 10, or if the value of a variable exits, is 1, for example, you end the loop. Your discount logic, for being a method and you can call her before you insert the book, so always before adding a book you calculate the discount of it, if there are.

Already the toStrings, to have the ToString of a book, you just need to give a system.out.println(repo.toString);

This is the way I would do it, in my opinion it would be simpler. I hope I helped in some way!

Browser other questions tagged

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