I´ve dealing with Checkstyle for a while and know what Magic Numbers are, however I´ve never encountered it this way, its just a Switch for assigning data, but the cases are marked as magic numbers, and I cant make those final or anything like that (that I know). The program I created gets a random number (int from 0 to 199) and uses It in the switch, not only that but the 200 in the ra.nextInt(200); is also "magic".
My questions is how do i prevent the cases from beeing magic numbers without having to declare 200 new variables?
This is my code:
public Basura selectB(final Basura b) {
private Random ra = new Random();
ran = ra.nextInt(200);
switch (ran) {
case 0: b.setName("Funko Roto");
b.setClasification("PL"); break;
case 1: b.setName("Bateria de Laptop");
b.setClasification("B"); break;
case 2: b.setName("Sobras de Comida");
b.setClasification("O"); break;
case 3: b.setName("Clips de Oficina");
b.setClasification("M"); break;
case 4: b.setName("Colcha Deshilachada");
b.setClasification("T"); break;
case 5: b.setName("Disco DVD");
b.setClasification("B"); break;
...
default:b.setName("ERROR");
b.setClasification("E"); break;
}
return b;
}