I have solved the following exercise but I have a problem, I don't know how to differentiate the possible values that a key of a TreeMap represents in the following code, for example in the method darDeBaja
I have to look for a key
and if that key
has at least 2 values assigned, I have to analyze which of the 2 values works for me; How can I know how many values are assigned to that key
?
Exercise: Do the following problem using Map and TreeMap.
You have the information of a student (name, registration number -key of 5 whole digits-, final grades of 5 subjects). This information must be defined in a class and must be private attributes. Make a program that has the following menu:
[high]
[low
[c]onsults
[modifications
[leave
Each of the following options will have you perform the following tasks:
high . The data of a student will be registered, if you want to enter more, you must return to the menu. A message must be sent confirming the registration of data. you go down A student will be dropped from enrollment (and name if necessary). A message must be sent confirming the withdrawal of the student, as well as the error if it cannot be done. Consultations. Only the data of the student to be searched will be displayed, their enrollment (and name if necessary), if the student is not found, indicate it with a message. Modifications. From the registration (and the name if necessary) of the student, some of the qualifications can be modified. A message should be sent confirming the successful modification no.
Leave. The program will end.
class Alumnado {
private String nombre;
private int matricula;
private double calificaciones[];
Map<Integer, String> datos = new TreeMap<>();
Alumnado() {
}
Alumnado(String nombre, int matricula, double calificaciones[]) {
datos.put(matricula, nombre);
this.calificaciones = calificaciones;
}
String getNombre() {
return nombre;
}
int getMat() {
return matricula;
}
double[] getCalif() {
return calificaciones;
}
void setCalif(double calif[]) {
calificaciones = calif;
}
}
public class Ejercicio {
public static boolean darDeAlta() {
Scanner y = new Scanner(System.in);
boolean b;
double d[] = null;
Alumnado alum;
System.out.println("Introduzca el nombre del estudiante");
String n = y.nextLine();
System.out.println("Introduzca su matricula (de 5 digitos)");
int m = y.nextInt();
System.out.println("Introduzca las calificaciones \n");
for (int i = 0; i < 5; i++) {
System.out.println("Introduzca la calificacion");
d[i] = y.nextDouble();
}
alum = new Alumnado(n, m, d);
System.out.println("Se han dado de alta los datos :)");
System.out.println("Desea ingresar mas alumnos?");
String s = y.nextLine();
if (s.equals("si")) {
b = true;
} else {
b = false;
}
return b;
}
public static void darDeBaja(String b) {
Scanner y = new Scanner(System.in);
boolean c;
Alumnado al = new Alumnado();
System.out.println("Introduzca la matricula del alumno");
int m = y.nextInt();
c = al.datos.containsKey(m);
if (c) {
al.datos.remove(m);
System.out.println("Se ha dado de baja al alumno");
} else {
System.out.println("No existe esa matricula");
}
}
public static void consultas() {
Scanner y = new Scanner(System.in);
boolean c;
Alumnado al = new Alumnado();
System.out.println("Introduzca la matricula del estudiante a
buscar");
int k = y.nextInt();
c = al.datos.containsKey(k);
if (c) {
System.out.println("Los datos son:" + al.datos.get(k) + " " +
al.getCalif());
} else {
System.out.println("El alumno no se encuentra");
}
}
public static void modificaciones() {
Scanner y = new Scanner(System.in);
boolean c;
Alumnado al = new Alumnado();
System.out.println("Introduzca la matricula del estudiante");
int k = y.nextInt();
c = al.datos.containsKey(k);
if (c) {
double calificacionNva[] = null;
System.out.println("Cuales son las nuevas calificaciones?");
for (int i = 0; i < 4; i++) {
double calif = y.nextDouble();
calificacionNva[i] = calif;
}
al.setCalif(calificacionNva);
System.out.println("Se han modificado las calificaciones
correctamente");
} else {
System.out.println("No existe esa matricula");
}
}
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
boolean bandera;
if (bandera = true) {
System.out.println("[a]ltas" + "\n" + "[b]ajas" + "\n" + "
[c]onsultas" + "\n" + "[m]odificaciones" + "\n" + "[s]alir");
System.out.println("\n Seleccione una opcion");
String opcion = x.next();
if (opcion.compareTo("a") == 0) {
bandera = darDeAlta();
}
if (opcion.compareTo("b") == 0) {
darDeBaja("b");
if (opcion.compareTo("c") == 0) {
consultas();
if (opcion.compareTo("m") == 0) {
modificaciones();
if (opcion.compareTo("s") == 0) {
bandera = false;
}
}
}
}
}
}
}
Note: The exercise is already solved :), although it fails in the darDeAlta
.. method.
Any suggestion/help is welcome/appreciated.
TreeMap
andMap
, are collections that do not allow repeated values in their keys, for a key there can only be one value. The difference is that itTreeMap
is a class that implements the interfaceMap
but works with the natural order of the keys instead of usingequals
andhashCode
as they doHashMap
andLinkedHashMap
. In order for it toTreeMap
know the natural ordering of the keys, the key class must either implement the interfaceComparable
or be assigned an instance ofComparator
in the constructor that tells it how to compute the order.In addition
Map
, they accept all types of objects, so I would change yourMap<Integer, String> datos = new TreeMap<>();
toMap<Integer, Alumnado> datos = new TreeMap<>();
y in your methoddarDeAlta()
is when I would put the object in the collection using the registration (which I understand will be unique for each object) as a key for each student.Also mention that your
Map
, should not be instantiated in the classAlumnado
, the classAlumnado
is a class that represents an object of type student, but in a student, you do not want information from more students, only theirs, that is why the collection should be in the classEjercicio
, which is the one that is going to be in charge of doing all the high/low/modification operations.I have made a couple of modifications, see if you see it well.
To start with, from the definition of
Map
in the documentation (translated and highlighted by me):That means your key cannot have two values assigned to it . That is impossible by design. I think that in your case, the key is the enrollment and the multiple values are the characteristics of a specific student... Doesn't it sound more like a mapping of the style
matricula -> alumno
?Having said that, I think you have a planning error... You have your Students map in the student class, what is the one you instantiate every time you create a new student?
Think about how it would be in real life: You don't want a group of students for each student there is, you want a single group, in which to put each new student that is created.
Refactor the student class
Your student class has a couple of design errors, in my opinion:
Your class
Alumno
would look like this:Refactor the Exercise class
This is your main class, but that doesn't mean it can't have attributes. You can either make the attributes static (not a very good idea) or create a new instance of the method
main
and call a non-static method on that instance. That is your decision.In this case, the
Map
student one is perfect for you. You would have something like this: