Reputation: 151
I want a method to obtain the second value using the first value as parameter. To place this exercise in context, Nota is mark (in spanish) and the second values would be the grading standards used here (instead of F, D-, D, C....)
As you can see, the grading standards got repeated while the int numbers. (Yes, this exercise should be made with floats, but ignore it), so the int value (numNota) would be like an identifier.
public enum Nota {
NOTA0(0,"Suspenso"),
NOTA1(1,"Suspenso"),
NOTA2(2,"Suspenso"),
NOTA3(3,"Suspenso"),
NOTA4(4,"Suspenso"),
NOTA5(5,"Suficiente"),
NOTA6(6,"Bien"),
NOTA7(7,"Notable"),
NOTA8(8,"Notable"),
NOTA9(9,"Sobresaliente"),
NOTA10(10,"Sobresaliente");
private int numNota;
private String stringNota;
Nota(int numNota, String stringNota) {
this.numNota = numNota;
this.stringNota = stringNota;
}
public int getNumNota() {
return numNota;
}
public String getStringNota() {
return stringNota;
}
public void setByNumNota(int numNota) {
this.numNota = numNota;
this.stringNota = ?????????
}
I'm assigning each nota (mark) to an alumn
public Alumno(String nombre, Nota nota) {
this.nombre = nombre;
this.nota = nota;
}
And the mark is being introduced by input scanner. That's why I need to create a Nota object from the int value of mark.
This exercise is without database yet. Our classes are full java now, all the data loads in memory for now (I wish we were using DB already lol)
Upvotes: 0
Views: 54
Reputation: 7604
Since stringNota
can be determined from numNota
(but not the other way around), it doesn't have to be a constructor parameter. You can simply compute it inside the constructor and only have the number as a parameter.
public enum Nota {
NOTA0(0),
NOTA1(1),
NOTA2(2),
NOTA3(3),
NOTA4(4),
NOTA5(5),
NOTA6(6),
NOTA7(7),
NOTA8(8),
NOTA9(9),
NOTA10(10);
private int numNota;
private String stringNota;
Nota(int numNota) {
this.numNota = numNota;
stringNota = stringFromNumNota(numNota); //see below
}
There's no need to check for negative numbers and numbers above 10 because the constructor is effectively private.
EDIT: To get stringNota
from numNota
, you can just write an extra method.
public String stringFromNumNota(int numNota) {
return numNota < 5 ? "Suspenso"
: numNota == 5 ? "Suficiente"
: numNota == 6 ? "Bien"
: numNota < 9 ? "Notable"
: "Sobresaliente";
}
Upvotes: 1