Created
September 21, 2016 04:09
-
-
Save alvareztech/d15fc9bfa878c5202c4b42fad5931826 to your computer and use it in GitHub Desktop.
Ejercicio Hospital Pilas Colas en Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez; | |
public class Constantes { | |
public static final int SIN_ESPECIALIDAD = 0; | |
public static final int ESPECIALIDAD_ODONTOLOGIA = 1; | |
public static final int ESPECIALIDAD_NEUROLOGIA = 2; | |
public static final int ESPECIALIDAD_PEDIATRIA = 3; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez; | |
public class Ficha { | |
private int especialidad; | |
private int codigo; | |
public Ficha(int especialidad, int codigo) { | |
this.especialidad = especialidad; | |
this.codigo = codigo; | |
} | |
public int getEspecialidad() { | |
return especialidad; | |
} | |
public void setEspecialidad(int especialidad) { | |
this.especialidad = especialidad; | |
} | |
public int getCodigo() { | |
return codigo; | |
} | |
public void setCodigo(int codigo) { | |
this.codigo = codigo; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
import java.util.Stack; | |
public class Main { | |
public static void main(String[] args) { | |
Stack<Ficha> o = new Stack<Ficha>(); | |
Stack<Ficha> n = new Stack<Ficha>(); | |
Stack<Ficha> p = new Stack<Ficha>(); | |
// llenar | |
for (int i = 1; i <= 20; i++) { | |
o.push(new Ficha(Constantes.ESPECIALIDAD_ODONTOLOGIA, i)); | |
n.push(new Ficha(Constantes.ESPECIALIDAD_NEUROLOGIA, i)); | |
p.push(new Ficha(Constantes.ESPECIALIDAD_PEDIATRIA, i)); | |
} | |
System.out.println("ODONTOLOGIA"); | |
mostrar(o); | |
System.out.println("NEUROLOGIA"); | |
mostrar(n); | |
System.out.println("PEDIATRIA"); | |
mostrar(p); | |
Queue<Paciente> pacientes = new LinkedList<Paciente>(); | |
pacientes.add(new Paciente("Nombre1", Constantes.ESPECIALIDAD_ODONTOLOGIA, Constantes.ESPECIALIDAD_PEDIATRIA)); | |
pacientes.add(new Paciente("Nombre2", Constantes.ESPECIALIDAD_PEDIATRIA, Constantes.SIN_ESPECIALIDAD)); | |
pacientes.add(new Paciente("Nombre3", Constantes.ESPECIALIDAD_NEUROLOGIA, Constantes.ESPECIALIDAD_ODONTOLOGIA)); | |
pacientes.add(new Paciente("Nombre4", Constantes.ESPECIALIDAD_ODONTOLOGIA, Constantes.SIN_ESPECIALIDAD)); | |
pacientes.add(new Paciente("Nombre5", Constantes.ESPECIALIDAD_ODONTOLOGIA, Constantes.SIN_ESPECIALIDAD)); | |
pacientes.add(new Paciente("Nombre6", Constantes.ESPECIALIDAD_PEDIATRIA, Constantes.SIN_ESPECIALIDAD)); | |
pacientes.add(new Paciente("Nombre7", Constantes.ESPECIALIDAD_NEUROLOGIA, Constantes.SIN_ESPECIALIDAD)); | |
pacientes.add(new Paciente("Nombre8", Constantes.ESPECIALIDAD_NEUROLOGIA, Constantes.SIN_ESPECIALIDAD)); | |
pacientes.add(new Paciente("Nombre9", Constantes.ESPECIALIDAD_NEUROLOGIA, Constantes.SIN_ESPECIALIDAD)); | |
pacientes.add(new Paciente("Nombre10", Constantes.ESPECIALIDAD_NEUROLOGIA, Constantes.SIN_ESPECIALIDAD)); | |
System.out.println("PACIENTES"); | |
mostrar(pacientes); | |
// Solución 1 | |
asignarFichas(o, n, p, pacientes); | |
System.out.println("ODONTOLOGIA"); | |
mostrar(o); | |
System.out.println("NEUROLOGIA"); | |
mostrar(n); | |
System.out.println("PEDIATRIA"); | |
mostrar(p); | |
// Solución 2 | |
// Calcular cual es la especialidad mas requerida | |
calcularEspecidadMasRequerida(o, n, p); | |
// Solución 3 | |
// Realizar el metodo atender para devolver la ficha a su lugar | |
Paciente pa = pacientes.remove(); | |
System.out.println("> paciente: " + pa.getNombre()); | |
atender(pa, o, p, n); | |
} | |
private static void atender(Paciente pa, Stack<Ficha> o, Stack<Ficha> p, Stack<Ficha> n) { | |
if (pa.getEspecialidad1() == Constantes.ESPECIALIDAD_ODONTOLOGIA) { | |
Ficha fi = pa.getFicha1(); | |
o.push(fi); | |
// reseteo | |
pa.setFicha1(null); | |
pa.setEspecialidad1(Constantes.SIN_ESPECIALIDAD); | |
} else if (pa.getEspecialidad1() == Constantes.ESPECIALIDAD_NEUROLOGIA) { | |
Ficha fi = pa.getFicha1(); | |
n.push(fi); | |
// reseteo | |
pa.setFicha1(null); | |
pa.setEspecialidad1(Constantes.SIN_ESPECIALIDAD); | |
} else if (pa.getEspecialidad1() == Constantes.ESPECIALIDAD_NEUROLOGIA) { | |
Ficha fi = pa.getFicha1(); | |
n.push(fi); | |
// reseteo | |
pa.setFicha1(null); | |
pa.setEspecialidad1(Constantes.SIN_ESPECIALIDAD); | |
} | |
if (pa.getEspecialidad2() != Constantes.SIN_ESPECIALIDAD) { | |
if (pa.getEspecialidad2() == Constantes.ESPECIALIDAD_ODONTOLOGIA) { | |
Ficha fi = pa.getFicha2(); | |
o.push(fi); | |
// reseteo | |
pa.setFicha2(null); | |
pa.setEspecialidad2(Constantes.SIN_ESPECIALIDAD); | |
} else if (pa.getEspecialidad2() == Constantes.ESPECIALIDAD_PEDIATRIA) { | |
Ficha fi = pa.getFicha2(); | |
p.push(fi); | |
// reseteo | |
pa.setFicha2(null); | |
pa.setEspecialidad2(Constantes.SIN_ESPECIALIDAD); | |
} else if (pa.getEspecialidad2() == Constantes.ESPECIALIDAD_NEUROLOGIA) { | |
Ficha fi = pa.getFicha2(); | |
n.push(fi); | |
// reseteo | |
pa.setFicha2(null); | |
pa.setEspecialidad2(Constantes.SIN_ESPECIALIDAD); | |
} | |
} | |
} | |
private static void calcularEspecidadMasRequerida(Stack<Ficha> o, Stack<Ficha> n, Stack<Ficha> p) { | |
int no = o.size(); | |
int np = p.size(); | |
int nn = n.size(); | |
if (no < np && no < nn) { | |
System.out.println("Odontologia es más requerido"); | |
} | |
if (np < no && np < nn) { | |
System.out.println("Pediatria es más requerido"); | |
} | |
if (nn < no && nn < np) { | |
System.out.println("Neurologia es más requerido"); | |
} | |
} | |
private static void asignarFichas(Stack<Ficha> o, Stack<Ficha> n, Stack<Ficha> p, Queue<Paciente> pacientes) { | |
int m = pacientes.size(); | |
for (int i = 0; i < m; i++) { | |
Paciente pa = pacientes.remove(); | |
if (pa.getEspecialidad1() == Constantes.ESPECIALIDAD_NEUROLOGIA) { | |
Ficha fi = n.pop(); | |
pa.setFicha1(fi); | |
} else if (pa.getEspecialidad1() == Constantes.ESPECIALIDAD_ODONTOLOGIA) { | |
Ficha fi = o.pop(); | |
pa.setFicha1(fi); | |
} else if (pa.getEspecialidad1() == Constantes.ESPECIALIDAD_PEDIATRIA) { | |
Ficha fi = p.pop(); | |
pa.setFicha1(fi); | |
} | |
if (pa.getEspecialidad2() != Constantes.SIN_ESPECIALIDAD) { | |
if (pa.getEspecialidad2() == Constantes.ESPECIALIDAD_NEUROLOGIA) { | |
Ficha fi = n.pop(); | |
pa.setFicha2(fi); | |
} else if (pa.getEspecialidad2() == Constantes.ESPECIALIDAD_ODONTOLOGIA) { | |
Ficha fi = o.pop(); | |
pa.setFicha2(fi); | |
} else if (pa.getEspecialidad2() == Constantes.ESPECIALIDAD_PEDIATRIA) { | |
Ficha fi = p.pop(); | |
pa.setFicha2(fi); | |
} | |
} | |
pacientes.add(pa); | |
} | |
} | |
private static void mostrar(Stack<Ficha> o) { | |
Stack<Ficha> temp = new Stack<Ficha>(); | |
while (!o.isEmpty()) { | |
Ficha fi = o.pop(); | |
System.out.println(" Ficha: " + fi.getEspecialidad() + " " + fi.getCodigo()); | |
temp.push(fi); | |
} | |
while (!temp.isEmpty()) { | |
Ficha fi = temp.pop(); | |
o.push(fi); | |
} | |
} | |
public static void mostrar(Queue<Paciente> cola) { | |
int n = cola.size(); | |
for (int i = 0; i < n; i++) { | |
Paciente pa = cola.remove(); | |
System.out.println(" Paciente: " + pa.getNombre() + " Especialidades: " + pa.getEspecialidad1() + ", " + pa.getEspecialidad2()); | |
cola.add(pa); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez; | |
public class Paciente { | |
private String nombre; | |
private Ficha ficha1; | |
private Ficha ficha2; | |
private int especialidad1; | |
private int especialidad2; | |
public Paciente(String nombre, int especialidad1, int especialidad2) { | |
this.nombre = nombre; | |
this.especialidad1 = especialidad1; | |
this.especialidad2 = especialidad2; | |
this.ficha1 = null; | |
this.ficha2 = null; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public Ficha getFicha1() { | |
return ficha1; | |
} | |
public void setFicha1(Ficha ficha1) { | |
this.ficha1 = ficha1; | |
} | |
public Ficha getFicha2() { | |
return ficha2; | |
} | |
public void setFicha2(Ficha ficha2) { | |
this.ficha2 = ficha2; | |
} | |
public int getEspecialidad1() { | |
return especialidad1; | |
} | |
public void setEspecialidad1(int especialidad1) { | |
this.especialidad1 = especialidad1; | |
} | |
public int getEspecialidad2() { | |
return especialidad2; | |
} | |
public void setEspecialidad2(int especialidad2) { | |
this.especialidad2 = especialidad2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment