import java.util.Scanner;
public class Work
{
public static void main(String[] args)
{
House house = new House(getRoofNumber());
System.out.println(house.getRoof());
}
public static int getRoofNumber()
{
Scanner s = new Scanner(System.in);
int numberOfRoof = 0;
System.out.println("Choose your roof material:\n"+
"1)Default roof\n" + "2)Tile roof\n" + "3)Slate roof");
while(true)
{
if (s.hasNextInt())
{
numberOfRoof = s.nextInt();
break;
}
else
{
System.out.println("Try again");
}
}
return numberOfRoof;
}
}
class House
{
private static final int DEFAULT_ROOF_NUMBER = 1;
private static final int TILE_ROOF_NUMBER = 2;
private static final int SLATE_ROOF_NUMBER = 3;
private Roof roof;
public House()
{
}
public House(Roof roof)
{
this.roof = roof;
}
public House(int numOfRoof)
{
switch(numOfRoof)
{
case DEFAULT_ROOF_NUMBER:
this.roof = Roof.DEFAULT_ROOF;
break;
case TILE_ROOF_NUMBER:
this.roof = Roof.TILE_ROOF;
break;
case SLATE_ROOF_NUMBER:
this.roof = Roof.SLATE_ROOF;
break;
default:
throw new RoofNotFoundException("Root of number " + numOfRoof + " not found");
}
}
public Roof getRoof()
{
return roof;
}
public void setRoof(Roof roof)
{
this.roof = roof;
}
}
enum Roof
{
DEFAULT_ROOF, TILE_ROOF, SLATE_ROOF
}
class RoofNotFoundException extends RuntimeException
{
public RoofNotFoundException (String message)
{
super(message);
}
}
И использовать в шаблоне другой код. Должно быть по полям объекта, который в форме указан. Вот пример: