I have this problem and basically I can not interpret the documentation correctly. I am implementing a hashtable with which I receive a number of boxes_type to respond with medication. ht (medic, cant). The problem is: I can not figure out how to use the enumerator. If I want to use a put of medication quantities, I do not realize how it is handled. ac [I copy the code that works but does not answer anything. the issue is that I do not find documentation with a clear example (or at least I do not realize it) how to manage the enumerat, and how to handle the var, Integer If you indicate me or documentation or a reference, thank you very much.
import java.util.*;
public class Ejecutora {
public static void main (String [] args) {
Administradora ad = new Administradora();
Scanner s = new Scanner (System.in);
s.useDelimiter(System.getProperty("line.separator"));
int opc = 0;
do {
System.out.println("1 AGREGA un medicamento");
System.out.println("2 AGREGA STOCK");
System.out.println("3 MUESTRA DATOS CARGADOS");
System.out.println("4 AGREGA UNA CAJA TIPO");
System.out.println("0 PARA SALIR");
opc = s.nextInt();
switch (opc) {
case 1:
ad.agregaUnMedicamento();
break;
case 2:
System.out.println("Ingrese el identificador del medicamento");
int idm = s.nextInt();
System.out.println("Ingrese el codigo de la caja tipo");
int cct = s.nextInt();
System.out.println("Ingrese la cantidad de remedios");
int cant = s.nextInt();
ad.agregaMedicamentoALaCajaTipo(idm, cct, cant);
break;
case 3:
ad.muestraDatosCargados();
break;
case 4:
System.out.println("Ingree el codigo de la caja tipo");
cct = s.nextInt();
ad.agregaCaja_Tipo(cct);
}
} while (opc != 0);
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Administradora{
private ArrayList <Medicamento> medicamentos;
private ArrayList <Caja_Tipo> cajas_tipo;
public Administradora() {
medicamentos = new ArrayList<Medicamento>();
cajas_tipo = new ArrayList<Caja_Tipo>();
}
public void muestraDatosCargados() {
Scanner s = new Scanner(System.in);
System.out.println("Ingrese el codigo de la caja tipo");
int cct = s.nextInt();
Caja_Tipo ct = this.buscaCaja_Tipo(cct);
if (ct != null) {
ct.muestraElementos();
}
}
public void agregaCaja_Tipo(int cct) {
Caja_Tipo ct = new Caja_Tipo(cct);
cajas_tipo.add(ct);
}
public void agregaMedicamentoALaCajaTipo(int idm, int cct, int cant) {
Medicamento me = this.buscaMedicamento(idm);
if (me != null) {
Caja_Tipo ct = this.buscaCaja_Tipo(cct);
if (ct != null) {
ct.ponerMedicamentosEnLaCaja(me, cant);
}
}
}
public Medicamento buscaMedicamento (int a) {
int b = 0;
while (b < medicamentos.size() && !(medicamentos.get(b).sosMedicamento(a))) {
b++;
}
if (b < medicamentos.size()) {
return medicamentos.get(b);
}
else {
return null;
}
}
public Caja_Tipo buscaCaja_Tipo (int a) {
int b = 0;
while (b < cajas_tipo.size() && !(cajas_tipo.get(b).sosCaja_Tipo(a))) {
b++;
}
if (b < cajas_tipo.size()) {
return cajas_tipo.get(b);
}
else {
return null;
}
}
public void agregaUnMedicamento() {
Scanner s = new Scanner (System.in);
System.out.println("Ingrese el numero identificador del medicamento");
int idm = s.nextInt();
Medicamento me = this.buscaMedicamento(idm);
Medicamento nuevo = null;
if ( me == null) {
System.out.println("Ingrese el stock del medicamento");
int stk = s.nextInt();
int opc = 0;
do {
System.out.println("Ingrese 1 - VACUNA | 2 - PASTILLA");
opc = s.nextInt();
} while (opc != 1 && opc != 2);
if (opc == 1) {
System.out.println("Ingrese el precio de la vacuna");
float pre = s.nextFloat();
nuevo = new Vacuna (idm, stk, pre);
}
else {
nuevo = new Pastilla (idm, stk);
}
medicamentos.add(nuevo);
}
else {
System.out.println("Este medicamento ya fue cargado");
}
}
}
public abstract class Medicamento {
private int ID_Medicamento;
private int stock_Medicamento;
public Medicamento(int idM, int stM) {
ID_Medicamento = idM;
stock_Medicamento = stM;
}
public abstract float getPrecio();
public boolean sosMedicamento (int a) {
return ID_Medicamento == a;
}
public int getID_Medicamento() {
return ID_Medicamento;
}
public int getStockMedicamento() {
return stock_Medicamento;
}
}
public class Pastilla extends Medicamento {
public Pastilla (int idM, int stM) {
super (idM, stM);
}
public float getPrecio() {
return 0;
}
}
public class Vacuna extends Medicamento {
private float precio;
public Vacuna (int idM, int stM, float pre) {
super (idM, stM);
precio = pre;
}
public void setPrecio(float p) {
precio = p;
}
public float getPrecio() {
return precio;
}
}
import java.util.Enumeration;
import java.util.Hashtable;
public class Caja_Tipo {
private int codigo_Caja_Tipo;
private Hashtable<Medicamento, Integer> medicamentos;
public Caja_Tipo(int cct) {
codigo_Caja_Tipo = cct;
medicamentos = new Hashtable<Medicamento, Integer>();
}
public void ponerMedicamentosEnLaCaja(Medicamento me, int cant) {
if (medicamentos.containsKey(me))
cant = cant + medicamentos.get(me);
medicamentos.put(me, cant);
}
public void muestraElementos() {
Enumeration<Medicamento> ee = medicamentos.keys();
Medicamento medi;
while (ee.hasMoreElements()) {
medi = ee.nextElement();
System.out.println("El precio del medicamento es: " + medi.getPrecio()
+ "El stock del medicamento es: " + medi.getStockMedicamento()
+ "El id del medicamento es: " + medi.getID_Medicamento());
}
}
public boolean sosCaja_Tipo(int a) {
return codigo_Caja_Tipo == a;
}
}