Guest User

Untitled

a guest
Jul 16th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.12 KB | None | 0 0
  1.  
  2. import java.util.Date;
  3.  
  4. public class Residencia {
  5. private String nombre;
  6. private Cuidador[] cuidadores;
  7. private int nCuidadores;
  8. private Habitacion[] habitaciones;
  9. private int nHabitaciones;
  10. private Residente[] residentes;
  11. private int nResidentes;
  12. private int nReservas;
  13. private Reserva[] reservas;
  14.  
  15. public Residencia(String nombre, Cuidador[] cuidadores,
  16. Habitacion[] habitaciones) {
  17. this.nombre = nombre;
  18. this.nCuidadores = 0;
  19. this.nHabitaciones = 0;
  20. this.nResidentes = 0;
  21. this.nReservas = 0;
  22. this.reservas = new Reserva[5];
  23. this.cuidadores = new Cuidador[2];
  24. this.habitaciones = new Habitacion[4];
  25. this.residentes = new Residente[5];
  26.  
  27. setHabitaciones(habitaciones);
  28. setCuidadores(cuidadores);
  29.  
  30. }
  31.  
  32. public int getNHabitaciones() {
  33. return nHabitaciones;
  34. }
  35.  
  36. public int getnReservas() {
  37. return nReservas;
  38. }
  39.  
  40. public void setnReservas(int nReservas) {
  41. this.nReservas = nReservas;
  42. }
  43.  
  44. public Reserva[] getReservas() {
  45. Reserva[] arrayAuxiliar = new Reserva[nReservas];
  46. for (int contador = 0; contador < nReservas; contador++) {
  47. arrayAuxiliar[contador] = reservas[contador];
  48. }
  49. return arrayAuxiliar;
  50. }
  51.  
  52. public void setReservas(Reserva[] reservas) {
  53. this.reservas = reservas;
  54. }
  55.  
  56. public Residente[] getResidentes() {
  57. return residentes;
  58. }
  59.  
  60. public void setResidentes(Residente[] residentes) {
  61. this.residentes = residentes.clone();
  62. }
  63.  
  64. public int getnResidentes() {
  65. return nResidentes;
  66. }
  67.  
  68. public void setnResidentes(int nResidentes) {
  69. this.nResidentes = nResidentes;
  70. }
  71.  
  72. public int getnCuidadores() {
  73. return nCuidadores;
  74. }
  75.  
  76. public void setnCuidadores(int nCuidadores) {
  77. this.nCuidadores = nCuidadores;
  78. }
  79.  
  80. public Habitacion[] getHabitaciones() {
  81. return habitaciones.clone();
  82. }
  83.  
  84. public Cuidador[] getCuidadores() {
  85. return cuidadores.clone();
  86. }
  87.  
  88. public void setHabitaciones(Habitacion[] habitaciones) {
  89. this.habitaciones = habitaciones.clone();
  90. for (int i = 0; i < habitaciones.length; i++) {
  91. if (habitaciones[i] != null) {
  92. nHabitaciones++;
  93. }
  94. }
  95. }
  96.  
  97. public void setCuidadores(Cuidador[] cuidadores) {
  98. this.cuidadores = cuidadores.clone();
  99. for (int i = 0; i < cuidadores.length; i++) {
  100. if (cuidadores[i] != null) {
  101. nCuidadores++;
  102. }
  103. }
  104. }
  105.  
  106. public int getNCuidadores() {
  107. return nCuidadores;
  108. }
  109.  
  110. public void setNCuidadores(int nCuidadores) {
  111. this.nCuidadores = nCuidadores;
  112. }
  113.  
  114. public String getNombre() {
  115. return nombre;
  116. }
  117.  
  118. public void setNombre(String nombre) {
  119. this.nombre = nombre;
  120. }
  121.  
  122. public Cuidador buscarCuidador(String nombre) {
  123.  
  124. Cuidador nuevo = null;
  125. for (int contador = 0; contador < nCuidadores; contador++) {
  126. if (cuidadores[contador].getNombre().equalsIgnoreCase(nombre)) {
  127. nuevo = cuidadores[contador];
  128. }
  129. }
  130. return nuevo;
  131. }
  132.  
  133. private void duplicarHabitaciones() {
  134. Habitacion[] aux = new Habitacion[cuidadores.length * 2];
  135. for (int i = 0; i < nCuidadores; i++) {
  136. aux[i] = habitaciones[i];
  137. }
  138. habitaciones = aux;
  139. }
  140.  
  141. private void duplicarCuidadores() {
  142. Cuidador[] aux = new Cuidador[cuidadores.length * 2];
  143. for (int i = 0; i < nCuidadores; i++) {
  144. aux[i] = cuidadores[i];
  145. }
  146. cuidadores = aux;
  147. }
  148.  
  149. public void insertarHabitacion(Habitacion habitacion) {
  150. boolean encontrado = false;
  151. if (habitaciones.length == nHabitaciones) {
  152. duplicarHabitaciones();
  153. }
  154. for (int i = 0; i < nHabitaciones; i++) {
  155. if (habitaciones[i].getNumero().compareTo(habitacion.getNumero()) == 0) {
  156. encontrado = true;
  157. }
  158. }
  159. if (encontrado == false) {
  160. habitaciones[nHabitaciones] = habitacion;
  161. nHabitaciones++;
  162. }
  163.  
  164. }
  165.  
  166. public boolean buscarCuidadorDni(String dni) {
  167. boolean encontrado = false;
  168. for (int i = 0; i < nCuidadores; i++) {
  169. if (cuidadores[i].getDni().equalsIgnoreCase(dni)) {
  170. encontrado = true;
  171. }
  172. }
  173. return encontrado;
  174. }
  175.  
  176. public void insertarCuidador(Cuidador cuidador) {
  177. boolean encontrado = buscarCuidadorDni(cuidador.getDni());
  178. if ((cuidador != null) && (encontrado == false)) {
  179. if (cuidadores.length == nCuidadores) {
  180. duplicarCuidadores();
  181. }
  182. cuidadores[nCuidadores] = cuidador;
  183. nCuidadores++;
  184. }
  185. }
  186.  
  187. public void eliminarCuidador(Cuidador cuidador3) {
  188.  
  189. int posicion = 0;
  190. while (!cuidadores[posicion].equals(cuidador3)) {
  191. posicion++;
  192. }
  193. for (int contador = posicion; contador < nCuidadores - 1; contador++) {
  194. cuidadores[contador] = cuidadores[contador + 1];
  195. }
  196. nCuidadores--;
  197. }
  198.  
  199. public Habitacion buscarHabitacion(String numero) {
  200. Habitacion nueva = null;
  201. for (int i = 0; i < nCuidadores; i++) {
  202. if (habitaciones[i].getNumero().equals(numero)) {
  203. nueva = habitaciones[i];
  204. }
  205. }
  206. return nueva;
  207. }
  208.  
  209. public void eliminarHabitacion(Habitacion habitacion1) {
  210. int posicion = 0;
  211. while (!habitaciones[posicion].equals(habitacion1)) {
  212. posicion++;
  213. }
  214. for (int contador = posicion; contador < nHabitaciones - 1; contador++) {
  215. habitaciones[contador] = habitaciones[contador + 1];
  216. }
  217. nHabitaciones--;
  218. }
  219.  
  220. public int getNReservas() {
  221.  
  222. return nReservas;
  223. }
  224.  
  225. public int getNResidentes() {
  226.  
  227. return nResidentes;
  228. }
  229.  
  230. private void duplicarResidentes() {
  231. Residente[] aux = new Residente[residentes.length * 2];
  232. for (int i = 0; i < nResidentes; i++) {
  233. aux[i] = residentes[i];
  234. }
  235. residentes = aux;
  236. }
  237.  
  238. private void duplicarReservas() {
  239. Reserva[] aux = new Reserva[reservas.length * 2];
  240. for (int i = 0; i < nReservas; i++) {
  241. aux[i] = reservas[i];
  242. }
  243. reservas = aux;
  244. }
  245.  
  246. public void cambiarHabitacion(Residente residente, Habitacion habitacion,
  247. Date fEntrada, Date fSalida) {
  248.  
  249. Reserva reserva = new Reserva(residente, habitacion, fEntrada, fSalida);
  250.  
  251. if (nReservas == reservas.length) {
  252. duplicarReservas();
  253. }
  254. reservas[nReservas] = reserva;
  255. nReservas++;
  256. }
  257.  
  258. public Residente buscarResidente(String nombre) {
  259. Residente aux = null;
  260. for (int i = 0; i < nResidentes; i++) {
  261. if (residentes[i].getNombre().toUpperCase()
  262. .equals(nombre.toUpperCase())) {
  263. aux = residentes[i];
  264. }
  265. }
  266. return aux;
  267. }
  268.  
  269. private void eliminarBloque(Residente[] residentes, int desde) {
  270. for (int i = desde; i < nResidentes - 1; i++)
  271. residentes[i] = residentes[i + 1];
  272. nResidentes--;
  273. }
  274.  
  275. public void eliminarResidente(Residente residente) {
  276. for (int i = 0; i < nResidentes; i++) {
  277. if (residentes[i].getDni().equals(residente.getDni())) {
  278. eliminarBloque(residentes, i);
  279. }
  280. }
  281. }
  282.  
  283. public void salida(Residente residente, Date fSalida) {
  284. eliminarResidente(residente);
  285. for (int i = 0; i < nReservas; i++) {
  286. if (reservas[i].getResidente().getNombre()
  287. .equalsIgnoreCase(residente.getNombre())) {
  288. reservas[i].setFechaSalida(fSalida);
  289. reservas[i].setAnulada(true);
  290. }
  291. }
  292. }
  293.  
  294. public void ingreso(Residente residente, Habitacion habitacion,
  295. Date fEntrada, Date fSalida) {
  296. boolean ingresar = true;
  297.  
  298. if ((buscarResidente(residente.getNombre()) == null)
  299. && ((fSalida.after(fEntrada)) || (fSalida.equals(fEntrada)))) {
  300. if ((isHabitacionOcupada(habitacion, fEntrada))
  301. || (isHabitacionOcupada(habitacion, fSalida))) {
  302. ingresar = false;
  303. }
  304.  
  305. if (ingresar == true) {
  306. if (nResidentes == residentes.length) {
  307. duplicarResidentes();
  308. }
  309. residentes[nResidentes] = residente;
  310. nResidentes++;
  311. Reserva reserva = new Reserva(residente, habitacion, fEntrada,
  312. fSalida);
  313. if (nReservas == reservas.length) {
  314. duplicarReservas();
  315. }
  316. reservas[nReservas] = reserva;
  317. nReservas++;
  318. }
  319. }
  320. }
  321.  
  322. public boolean isHabitacionOcupada(Habitacion habitacion, Date fecha) {
  323. boolean ocupada = false;
  324.  
  325. for (int i = 0; i < nReservas; i++) {
  326. if (reservas[i].getHabitacion().getNumero()
  327. .equalsIgnoreCase(habitacion.getNumero())) {
  328. if (reservas[i].isAnulada() == false) {
  329. if ((((reservas[i].getFechaEntrada().before(fecha)) || (reservas[i]
  330. .getFechaEntrada().equals(fecha))))
  331. && (((reservas[i].getFechaSalida().after(fecha)) || (reservas[i]
  332. .getFechaSalida().equals(fecha))))) {
  333. ocupada = true;
  334. }
  335. }
  336.  
  337. }
  338. }
  339. return ocupada;
  340. }
  341.  
  342. public String listadoHabitacionesLibres(Date date) {
  343. String listado = "";
  344. for (int i = 0; i < habitaciones.length; i++) {
  345. if (isHabitacionOcupada(habitaciones[i], date) == false) {
  346. listado = listado + habitaciones[i].getNumero() + "\n";
  347. }
  348. }
  349. return listado;
  350. }
  351.  
  352. public String listadoResidentesHabitaciones(Date date) {
  353. String listado = "";
  354. Residente[] aux = new Residente[nReservas];
  355. int nRAux = 0;
  356. for (int i = 0; i < nReservas; i++) {
  357. if (reservas[i].isAnulada() == false) {
  358. if (isHabitacionOcupada(reservas[i].getHabitacion(), date)) {
  359. aux[nRAux] = reservas[i].getResidente();
  360. nRAux++;
  361. }
  362. }
  363. }
  364. ordenarNombre(aux, nRAux);
  365.  
  366. for (int i = 0; i < nRAux; i++) {
  367. listado = listado + aux[i].getNombre() + "\n";
  368. }
  369. return listado;
  370. }
  371.  
  372. private void ordenarNombre(Residente[] residentes, int num) {
  373. Residente tmp;
  374. for (int i = 0; i < num - 1; i++) {
  375. for (int j = i + 1; j < num; j++) {
  376. if (residentes[j].getNombre().compareToIgnoreCase(
  377. residentes[i].getNombre()) < 0) {
  378. tmp = residentes[i];
  379. residentes[i] = residentes[j];
  380. residentes[j] = tmp;
  381. }
  382. }
  383. }
  384. }
  385.  
  386. public String listadoEdadMediaPorSexo(Date fecha) {
  387. String listado = "";
  388. int nMujeres = 0;
  389. int nHombres = 0;
  390. double sumaEHom = 0;
  391. double sumaEMuj = 0;
  392. double edadMediaMuj = 0;
  393. double edadMediaHom = 0;
  394. char hombre = 'V';
  395.  
  396. for (int i = 0; i < nResidentes; i++) {
  397. if (residentes[i].getSexo() == hombre) {
  398. sumaEHom = sumaEHom + residentes[i].getEdad(fecha);
  399. nHombres++;
  400. } else {
  401. sumaEMuj = sumaEMuj + residentes[i].getEdad(fecha);
  402. nMujeres++;
  403. }
  404. }
  405. if (nHombres > 0) {
  406. edadMediaHom = sumaEHom / nHombres;
  407. }
  408. if (nMujeres > 0) {
  409. edadMediaMuj = sumaEMuj / nMujeres;
  410. }
  411.  
  412. listado = "Edad media de los hombres: " + edadMediaHom
  413. + "\nEdad media de las mujeres: " + edadMediaMuj;
  414.  
  415. return listado;
  416. }
  417.  
  418. }
Add Comment
Please, Sign In to add comment