They ask me to design a program in which I have to enter the parameters of an object through the setter. I have created a separate class with the attributes (private) and the methods. In the Main class I have to enter the parameters of the keyboard options. My problem is that I can't use the setter if I don't have an instance yet, and through the Scanner class I can't create an instance. I accompany a part of the program that I have written:
public class Principal {
public static void main(String[] args) {
boolean salir =false;//declaro la variable que nos permitirá salir del programa
Scanner teclado = new Scanner(System.in);
while(!salir){//cuando !salir=false => saldremos del bucle
System.out.println("Introduzca el número correspondiente a la acción que desea hacer:\n 1. Nuevo Empleado.\n 2. Ver DNI Empleado.\n 3. Ver Sueldo.\n 4. Salir.\n");
int opcion = teclado.nextInt();
switch (opcion) {
case 1:
//necesito esta linea y no sé donde colocarla:
Empleado empleado_1 = new Empleado();
System.out.println("Introduzca el nombre del empleado: ");
String nombre = teclado.next();
empleado_1.setNombre(nombre);
System.out.println("Introduzca DNI del empleado: ");
String DNI = teclado.next();
empleado_1.setNumDNI(DNI);
As you have noticed, it is not possible to read an object from a user class (classes you create, eg your "Employee" class) in the standard Java Scanner. The options you have left are: