Doubt in CRUD, I am having difficulty reading the keyboard data

0

I'm a beginner in Java.

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

Class Author

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;
    }
}
    
asked by anonymous 23.06.2017 / 20:55

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;
        }
    }
}
    
23.06.2017 / 21:13
1

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

After that, you need to create an object of type Scanner, which will read the keyboard data for your 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;
    }
}

By creating an object of type Scanner you can read various types of keyboard data. I suggest reading this article from DevMedia for a better understanding.

    
23.06.2017 / 21:20
0

As an alternative to another answer 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();
        }
    }
}
    
23.06.2017 / 21:16