Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- public class Residencia {
- private String nombre;
- private Cuidador[] cuidadores;
- private int nCuidadores;
- private Habitacion[] habitaciones;
- private int nHabitaciones;
- private Residente[] residentes;
- private int nResidentes;
- private int nReservas;
- private Reserva[] reservas;
- public Residencia(String nombre, Cuidador[] cuidadores,
- Habitacion[] habitaciones) {
- this.nombre = nombre;
- this.nCuidadores = 0;
- this.nHabitaciones = 0;
- this.nResidentes = 0;
- this.nReservas = 0;
- this.reservas = new Reserva[5];
- this.cuidadores = new Cuidador[2];
- this.habitaciones = new Habitacion[4];
- this.residentes = new Residente[5];
- setHabitaciones(habitaciones);
- setCuidadores(cuidadores);
- }
- public int getNHabitaciones() {
- return nHabitaciones;
- }
- public int getnReservas() {
- return nReservas;
- }
- public void setnReservas(int nReservas) {
- this.nReservas = nReservas;
- }
- public Reserva[] getReservas() {
- Reserva[] arrayAuxiliar = new Reserva[nReservas];
- for (int contador = 0; contador < nReservas; contador++) {
- arrayAuxiliar[contador] = reservas[contador];
- }
- return arrayAuxiliar;
- }
- public void setReservas(Reserva[] reservas) {
- this.reservas = reservas;
- }
- public Residente[] getResidentes() {
- return residentes;
- }
- public void setResidentes(Residente[] residentes) {
- this.residentes = residentes.clone();
- }
- public int getnResidentes() {
- return nResidentes;
- }
- public void setnResidentes(int nResidentes) {
- this.nResidentes = nResidentes;
- }
- public int getnCuidadores() {
- return nCuidadores;
- }
- public void setnCuidadores(int nCuidadores) {
- this.nCuidadores = nCuidadores;
- }
- public Habitacion[] getHabitaciones() {
- return habitaciones.clone();
- }
- public Cuidador[] getCuidadores() {
- return cuidadores.clone();
- }
- public void setHabitaciones(Habitacion[] habitaciones) {
- this.habitaciones = habitaciones.clone();
- for (int i = 0; i < habitaciones.length; i++) {
- if (habitaciones[i] != null) {
- nHabitaciones++;
- }
- }
- }
- public void setCuidadores(Cuidador[] cuidadores) {
- this.cuidadores = cuidadores.clone();
- for (int i = 0; i < cuidadores.length; i++) {
- if (cuidadores[i] != null) {
- nCuidadores++;
- }
- }
- }
- public int getNCuidadores() {
- return nCuidadores;
- }
- public void setNCuidadores(int nCuidadores) {
- this.nCuidadores = nCuidadores;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- public Cuidador buscarCuidador(String nombre) {
- Cuidador nuevo = null;
- for (int contador = 0; contador < nCuidadores; contador++) {
- if (cuidadores[contador].getNombre().equalsIgnoreCase(nombre)) {
- nuevo = cuidadores[contador];
- }
- }
- return nuevo;
- }
- private void duplicarHabitaciones() {
- Habitacion[] aux = new Habitacion[cuidadores.length * 2];
- for (int i = 0; i < nCuidadores; i++) {
- aux[i] = habitaciones[i];
- }
- habitaciones = aux;
- }
- private void duplicarCuidadores() {
- Cuidador[] aux = new Cuidador[cuidadores.length * 2];
- for (int i = 0; i < nCuidadores; i++) {
- aux[i] = cuidadores[i];
- }
- cuidadores = aux;
- }
- public void insertarHabitacion(Habitacion habitacion) {
- boolean encontrado = false;
- if (habitaciones.length == nHabitaciones) {
- duplicarHabitaciones();
- }
- for (int i = 0; i < nHabitaciones; i++) {
- if (habitaciones[i].getNumero().compareTo(habitacion.getNumero()) == 0) {
- encontrado = true;
- }
- }
- if (encontrado == false) {
- habitaciones[nHabitaciones] = habitacion;
- nHabitaciones++;
- }
- }
- public boolean buscarCuidadorDni(String dni) {
- boolean encontrado = false;
- for (int i = 0; i < nCuidadores; i++) {
- if (cuidadores[i].getDni().equalsIgnoreCase(dni)) {
- encontrado = true;
- }
- }
- return encontrado;
- }
- public void insertarCuidador(Cuidador cuidador) {
- boolean encontrado = buscarCuidadorDni(cuidador.getDni());
- if ((cuidador != null) && (encontrado == false)) {
- if (cuidadores.length == nCuidadores) {
- duplicarCuidadores();
- }
- cuidadores[nCuidadores] = cuidador;
- nCuidadores++;
- }
- }
- public void eliminarCuidador(Cuidador cuidador3) {
- int posicion = 0;
- while (!cuidadores[posicion].equals(cuidador3)) {
- posicion++;
- }
- for (int contador = posicion; contador < nCuidadores - 1; contador++) {
- cuidadores[contador] = cuidadores[contador + 1];
- }
- nCuidadores--;
- }
- public Habitacion buscarHabitacion(String numero) {
- Habitacion nueva = null;
- for (int i = 0; i < nCuidadores; i++) {
- if (habitaciones[i].getNumero().equals(numero)) {
- nueva = habitaciones[i];
- }
- }
- return nueva;
- }
- public void eliminarHabitacion(Habitacion habitacion1) {
- int posicion = 0;
- while (!habitaciones[posicion].equals(habitacion1)) {
- posicion++;
- }
- for (int contador = posicion; contador < nHabitaciones - 1; contador++) {
- habitaciones[contador] = habitaciones[contador + 1];
- }
- nHabitaciones--;
- }
- public int getNReservas() {
- return nReservas;
- }
- public int getNResidentes() {
- return nResidentes;
- }
- private void duplicarResidentes() {
- Residente[] aux = new Residente[residentes.length * 2];
- for (int i = 0; i < nResidentes; i++) {
- aux[i] = residentes[i];
- }
- residentes = aux;
- }
- private void duplicarReservas() {
- Reserva[] aux = new Reserva[reservas.length * 2];
- for (int i = 0; i < nReservas; i++) {
- aux[i] = reservas[i];
- }
- reservas = aux;
- }
- public void cambiarHabitacion(Residente residente, Habitacion habitacion,
- Date fEntrada, Date fSalida) {
- Reserva reserva = new Reserva(residente, habitacion, fEntrada, fSalida);
- if (nReservas == reservas.length) {
- duplicarReservas();
- }
- reservas[nReservas] = reserva;
- nReservas++;
- }
- public Residente buscarResidente(String nombre) {
- Residente aux = null;
- for (int i = 0; i < nResidentes; i++) {
- if (residentes[i].getNombre().toUpperCase()
- .equals(nombre.toUpperCase())) {
- aux = residentes[i];
- }
- }
- return aux;
- }
- private void eliminarBloque(Residente[] residentes, int desde) {
- for (int i = desde; i < nResidentes - 1; i++)
- residentes[i] = residentes[i + 1];
- nResidentes--;
- }
- public void eliminarResidente(Residente residente) {
- for (int i = 0; i < nResidentes; i++) {
- if (residentes[i].getDni().equals(residente.getDni())) {
- eliminarBloque(residentes, i);
- }
- }
- }
- public void salida(Residente residente, Date fSalida) {
- eliminarResidente(residente);
- for (int i = 0; i < nReservas; i++) {
- if (reservas[i].getResidente().getNombre()
- .equalsIgnoreCase(residente.getNombre())) {
- reservas[i].setFechaSalida(fSalida);
- reservas[i].setAnulada(true);
- }
- }
- }
- public void ingreso(Residente residente, Habitacion habitacion,
- Date fEntrada, Date fSalida) {
- boolean ingresar = true;
- if ((buscarResidente(residente.getNombre()) == null)
- && ((fSalida.after(fEntrada)) || (fSalida.equals(fEntrada)))) {
- if ((isHabitacionOcupada(habitacion, fEntrada))
- || (isHabitacionOcupada(habitacion, fSalida))) {
- ingresar = false;
- }
- if (ingresar == true) {
- if (nResidentes == residentes.length) {
- duplicarResidentes();
- }
- residentes[nResidentes] = residente;
- nResidentes++;
- Reserva reserva = new Reserva(residente, habitacion, fEntrada,
- fSalida);
- if (nReservas == reservas.length) {
- duplicarReservas();
- }
- reservas[nReservas] = reserva;
- nReservas++;
- }
- }
- }
- public boolean isHabitacionOcupada(Habitacion habitacion, Date fecha) {
- boolean ocupada = false;
- for (int i = 0; i < nReservas; i++) {
- if (reservas[i].getHabitacion().getNumero()
- .equalsIgnoreCase(habitacion.getNumero())) {
- if (reservas[i].isAnulada() == false) {
- if ((((reservas[i].getFechaEntrada().before(fecha)) || (reservas[i]
- .getFechaEntrada().equals(fecha))))
- && (((reservas[i].getFechaSalida().after(fecha)) || (reservas[i]
- .getFechaSalida().equals(fecha))))) {
- ocupada = true;
- }
- }
- }
- }
- return ocupada;
- }
- public String listadoHabitacionesLibres(Date date) {
- String listado = "";
- for (int i = 0; i < habitaciones.length; i++) {
- if (isHabitacionOcupada(habitaciones[i], date) == false) {
- listado = listado + habitaciones[i].getNumero() + "\n";
- }
- }
- return listado;
- }
- public String listadoResidentesHabitaciones(Date date) {
- String listado = "";
- Residente[] aux = new Residente[nReservas];
- int nRAux = 0;
- for (int i = 0; i < nReservas; i++) {
- if (reservas[i].isAnulada() == false) {
- if (isHabitacionOcupada(reservas[i].getHabitacion(), date)) {
- aux[nRAux] = reservas[i].getResidente();
- nRAux++;
- }
- }
- }
- ordenarNombre(aux, nRAux);
- for (int i = 0; i < nRAux; i++) {
- listado = listado + aux[i].getNombre() + "\n";
- }
- return listado;
- }
- private void ordenarNombre(Residente[] residentes, int num) {
- Residente tmp;
- for (int i = 0; i < num - 1; i++) {
- for (int j = i + 1; j < num; j++) {
- if (residentes[j].getNombre().compareToIgnoreCase(
- residentes[i].getNombre()) < 0) {
- tmp = residentes[i];
- residentes[i] = residentes[j];
- residentes[j] = tmp;
- }
- }
- }
- }
- public String listadoEdadMediaPorSexo(Date fecha) {
- String listado = "";
- int nMujeres = 0;
- int nHombres = 0;
- double sumaEHom = 0;
- double sumaEMuj = 0;
- double edadMediaMuj = 0;
- double edadMediaHom = 0;
- char hombre = 'V';
- for (int i = 0; i < nResidentes; i++) {
- if (residentes[i].getSexo() == hombre) {
- sumaEHom = sumaEHom + residentes[i].getEdad(fecha);
- nHombres++;
- } else {
- sumaEMuj = sumaEMuj + residentes[i].getEdad(fecha);
- nMujeres++;
- }
- }
- if (nHombres > 0) {
- edadMediaHom = sumaEHom / nHombres;
- }
- if (nMujeres > 0) {
- edadMediaMuj = sumaEMuj / nMujeres;
- }
- listado = "Edad media de los hombres: " + edadMediaHom
- + "\nEdad media de las mujeres: " + edadMediaMuj;
- return listado;
- }
- }
Add Comment
Please, Sign In to add comment