I have this code and my problem is that it gives me an error in the methdod CrearProductoExtranjero
in ProductoExtranjeroPe = new ProductoExtranjero();
that says that the constructor isn't suitable, please help
import java.util.* ;
import java.util.Scanner;
public class ProductoExtranjero extends Producto {
private int PaisOrigen;
public ProductoExtranjero(int PaisOrigen, String UnNombre, String UnRubro) {
super(UnNombre, UnRubro);
this.PaisOrigen = PaisOrigen;
}
public ProductoExtranjero(int PaísOrigen) {
this.PaisOrigen = PaisOrigen;
}
public int getPaisOrigen() {
return PaisOrigen;
}
public void setPaisOrigen(int PaísOrigen) {
this.PaisOrigen = PaísOrigen;
}
@Override
public String toString() {
return "ProductoExtranjero{" + "PaiedsOrigen=" + PaisOrigen + "nombre=" + this.getNombre() + "rubro=" + this.getRubro() ;
}
public static Producto CrearProductoExtranjero() {
ProductoExtranjero Pe = new ProductoExtranjero();
Scanner in = new Scanner(System.in);
System.out.println("ingrese País de origen (1-120)");
Pe.setPaisOrigen(in.nextInt());
boolean val= true;
while(val){
System.out.println("Ingrese un nombre del producto: ");
Pe.setNombre(in.nextLine());
int nomlargo;
nomlargo=Pe.getNombre().length();
if (nomlargo<=0){
System.out.println("No ha ingresado un nombre producto valido. tiene que ser mayor a TRES!! caracteres.");
}
else{
val=false;
}
}
int opcion = 0;
boolean entrar = true;
while (entrar) {
System.out.println("Ingrese el rubro del producto. Tomando en cuenta que: \n1.Limpieza \n2.Cosmetica "
+ "\n3.Computacion \n4.Educacion \n5.Electrodomesticos \n6.Varios");
opcion = in.nextInt();
switch (opcion) {
case 1:
Pe.setRubro("Limpieza");
entrar = false;
;
case 2:
Pe.setRubro("Cosmetica");
entrar = false;
case 3:
Pe.setRubro("Computacion");
entrar = false;
case 4:
Pe.setRubro("Educacion");
entrar = false;
case 5:
Pe.setRubro("Electrodomesticos");
entrar = false;
case 6:
Pe.setRubro("Varios");
entrar = false;
default:
System.out.println("Ha ingresado un rubro no existente!!");
} break;
}
return Pe;
}
}
Here are your two constructors:
public ProductoExtranjero(int PaisOrigen, String UnNombre, String UnRubro) {
super(UnNombre, UnRubro);
this.PaisOrigen = PaisOrigen;
}
public ProductoExtranjero(int PaísOrigen) {
this.PaisOrigen = PaisOrigen;
}
Both of them have parameters. But this line:
ProductoExtranjeroPe = new ProductoExtranjero();
... is string to call a constructor without specifying any arguments.
You either need to specify a parameterless constructor, or specify arguments in your constructor call.
(I would also strongly recommend that you start following Java naming conventions.)
See more on this question at Stackoverflow