Doubt in CRUD, I’m having trouble reading the keyboard data

Asked

Viewed 126 times

0

I’m a beginner in Java.

I am creating a CRUD from a library, but I have one question: when I create and install a new library in the main, how do I read the author’s data typed by the keyboard? Since the ArrayList author, and author class are in different classes?

Author Class

public class Autor extends Pessoa{

    private int codigo;
    private String email;


    public Autor() {
        super();
        }


    public Autor(String nome, String nacionalidade,int codigo, String email) {
        super(nome, nacionalidade);
        this.codigo=codigo;
        this.email=email;

    }

    //Gets e Sets.....
}

Library class

ArrayList <Autor> autores= new ArrayList<Autor>();

//método pra cadastrar o autor
public void cadastarAutor(Autor autor) {
    this.autores.add(autor); 

}

Main Class

public class Principal{

    public static void main(String[] args) {

        int opc=0;

        Biblioteca biblioteca= new Biblioteca();


        switch(opc){

            case 1:

            Autor autor= new Autor();

            System.out.println("Digite o nome:");

            System.out.println("Digite o nacionalidade:");

            System.out.println("Digite o codigo:");

            System.out.println("Digite o email:");

            //A minha dúvida é nessa parte, como eu vou ler(setar) os dados do autor aqui?

            break;
    }
}

3 answers

1


To set the fields of your object by the console you can use the class Scanner

public class Principal {

    public static void main(String[] args) {
        int opc = 1;
        Biblioteca biblioteca = new Biblioteca();  
        Scanner scanner = new Scanner(System.in);  
        switch (opc) {
           case 1:  
              Autor autor = new Autor(); 

              System.out.println("Digite o nome:");
              autor.setNome(scanner.nextLine());  

              System.out.println("Digite o nacionalidade:");
              autor.setNacionalidade(scanner.nextLine()); 

              System.out.println("Digite o codigo:");
              autor.setCodigo(scanner.nextInt());  

              System.out.println("Digite o email:");
              autor.setEmail(scanner.nextLine()); 

              biblioteca.cadastrarAutor(autor);
              break;
        }
    }
}
  • I understood perfectly worth my friend!

1

At the beginning of class Principal you include Scanner import import java.util.Scanner;

After that, you need to create a scanner object, which will read the keyboard data to its author object. Your main method code should look like this:

public static void main(String[] args) {

    int opc = 0;
    Biblioteca biblioteca = new Biblioteca();
    Scanner s = new Scanner(System.in);

    switch (opc) {
        case 1:
            Autor autor = new Autor();
            System.out.println("Digite o nome: ");
            autor.setNome(s.nextLine());
            System.out.println("Digite o nacionalidade: ");
            autor.setNacionalidade(s.nextLine());
            System.out.println("Digite o codigo: ");
            autor.setCodigo(s.nextInt());
            System.out.println("Digite o email: ");
            autor.setEmail(s.nextLine());

            biblioteca.cadastarAutor(autor);
            break;
    }
}

Creating an object of type Scanner you can read various types of data from the keyboard. I suggest reading of this Devmedia article for a better understanding.

0

Alternatively, you can use console(). readline();

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Principal {

    public static void main(String[] args) {

        int opc = 0;
        Biblioteca biblioteca = new Biblioteca();
        switch (opc) {
            case 1:
                Scanner s = new Scanner(System.in);
                System.out.printl("Digite o nome:");
                String nome = System.console().readLine();
                System.out.println("Digite o nacionalidade:");
                String nacionalidade = System.console().readline();
                System.out.println("Digite o codigo:");
                int codigo = System.console().readline();
                System.out.println("Digite o email:");
                String email = System.console().readline();
                Autor autor = new Autor(nome, nacionalidade,codigo,email);
                biblioteca.cadastrarAutor(autor);
                s.close();
        }
    }
}

Browser other questions tagged

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