From 469621c7ba5c4ae5b2907c8c8c429a916823e26f Mon Sep 17 00:00:00 2001 From: Javier Calvo <javiercalvoporro@gmail.com> Date: Sun, 10 Nov 2024 11:47:23 +0100 Subject: [PATCH] Trayecto y Fecha --- src/es/markse/Fecha.java | 326 +++++++++++++++++++++++++++++++ src/es/markse/Trayecto.java | 151 ++++++++++---- uses/es/markse/MuelleTest.java | 1 - uses/es/markse/TrayectoTest.java | 131 +++++++++++++ 4 files changed, 574 insertions(+), 35 deletions(-) create mode 100644 src/es/markse/Fecha.java create mode 100644 uses/es/markse/TrayectoTest.java diff --git a/src/es/markse/Fecha.java b/src/es/markse/Fecha.java new file mode 100644 index 0000000..de0a953 --- /dev/null +++ b/src/es/markse/Fecha.java @@ -0,0 +1,326 @@ +package es.markse; + +/** + * Implementación de la fecha del calendario gregoriano. + * Ejercicio de Programación Orientada a Objetos + * Implementación basada en tres atributos + * + * @author felix + * @author marga + * + */ +public class Fecha { + + private int anno; + private int mes; + private int dia; + + /** + * Inicialización a partir de número de dÃas desde «Epoch». + * + * Utilizamos la notación habitual Epoch para referirnos al 1-1-1970, al estilo + * *N?X. No comprueba que el dÃa es correcto.<br> + * @param dDÃas transcurridos desde 1 de enero de 1970, + * @throws IllegalArgumentException cuando {@code d<0} + */ + public Fecha(int d) { + if (d<0) + throw new IllegalArgumentException("Fecha incorrecta"); + + Fecha f = new Fecha(1, 1, 1970); + for (int i = 1; i <= d; i++) { + f = f.mañana(); + } + anno = f.getAnno(); + mes = f.getMes(); + dia = f.getDia(); + } + + /** + * Inicialización a partir de un dÃa, mes y año. + * + * + * @param d + * DÃa del mes, entre 1 y el número de dÃas del {@code mes} + * @param m + * Mes, entre 1 y 12 + * @param a + * Año con cuatro cifras, no negativo + * @throws IllegalArgumentException cuando la d, m o a no corresponda a una fecha correcta + */ + public Fecha(int d, int m, int a) { + if (!esFechaCorrecta(d,m,a)) + throw new IllegalArgumentException("Fecha incorrecta"); + dia = d; + mes = m; + anno = a; + } + + /** + * DÃas transcurridos desde el 1 de enero de 1970. + * + * @return Negativo para fechas anteriores a 1-1-1970 + */ + public int getDiasDesdeEpoch() { + return diferencia(new Fecha(1, 1, 1970)); + } + + /** + * Número de dÃa del mes. + * + * @return entre 1 y número de dÃas de {\@code getMes()} + */ + public int getDia() { + return dia; + } + + /** + * Número del meses. + * + * @return Entre 1 y 12 + */ + public int getMes() { + return mes; + } + + /** + * Año. + * + * @return Año con 4 cifras y positivo + */ + public int getAnno() { + return anno; + } + + /** + * El dÃa de la semana. + * + * La semana empieza el lunes + * + * @return dÃa de la semana entre (1=lunes,...,7=domingo) + */ + public int diaSemana() { + // El 1 de enero de 1970 fue jueves + return (getDiasDesdeEpoch()+3) % 7 + 1 ; + } + + /** + * ¿Es dÃa de diario? + * + * @return Cierto de lunes a viernes, falso en otro caso + */ + public boolean esLaborable() { + return (diaSemana() >= 1) && (diaSemana() <= 5); + } + + /** + * El mañana de {@code this}. + * + * @return Mañana + */ + public Fecha mañana() { + int d = getDia(); + int m = getMes(); + int a = getAnno(); + d = d + 1; + if (d > diasMes(m, a)) { + d = 1; + m = m + 1; + if (m == 13) { + m = 1; + a = a + 1; + } + } + return new Fecha(d, m, a); + + } + + /** + * El ayer de {@code this}. + * + * @return Ayer + */ + public Fecha ayer() { + int d = getDia(); + int m = getMes(); + int a = getAnno(); + d = d - 1; + if (d == 0) { + m = m - 1; + if (m == 0) { + m = 12; + a = a - 1; + } + d = diasMes(m, a); + } + return new Fecha(d, m, a); + + } + + /** + * Diferencia en dÃas entre {@code this} y {@code other}. + * + * @param otro + * dÃa a restar + * @return {@code this - otro} + * @throws IllegalArgumentException cuando {@code otro==null} + */ + public int diferencia(Fecha otro) { + if (otro==null) + throw new IllegalArgumentException("LLamada incorrecta: otro==null"); + + Fecha inicio, fin, contador; + int signo = comparaCon(otro); + switch (signo) { + case 1: + inicio = otro; + fin = this; + + break; + case -1: + inicio = this; + fin = otro; + break; + default: // Las dos fechas son iguales + return 0; + } + contador = inicio; + int dias = 0; + while (!contador.igualA(fin)) { + contador = contador.mañana(); + dias += 1; + } + return signo * dias; + } + + /** + * Incremento en un número de dÃas. + * + * @param n Número de dÃas en que se incrementa, debe ser un número positivo + * @return {@code this + n} + * @throws IllegalArgumentException cuando {@code n>=0} + */ + public Fecha mas(int n) { + if (n<=0) + throw new IllegalArgumentException("Llamada incorrecta: n >=0"); + + Fecha f = new Fecha(getDia(), getMes(), getAnno()); + for (int i = 1; i <= n; i++) { + f = f.mañana(); + } + return f; + } + + /** + * Número de la semana según la norma ISO. + * + * La semana empieza el lunes y termina el domingo. El número de semana es el + * número de orden para determinar una semana exacto durante el año. De acuerdo + * con la norma ISO 8601 y NEN 2772 es la primera semana del año en que contiene + * cuatro o más dÃas de ese año. Porque, según NEN 2772, el primer dia de la + * semana es un Lunes, se puede decir que la semana 1 es la semana que lleva el + * primer Jueves del año. La primera semana del año tambien es la semana que + * conlleva la fecha 4 de enero, o la semana con la mayoria de los dias. + * + * @return Número de semana entre 1 y TODO Determinar + */ + public int semana() { + // TODO Implementar número de semana + return 0; + } + + /** + * Comparación habitual entre fechas. + * <ul> + * <li>a {@literal >} b cuando a.compareTo(b) {@literal >} 0 <br> + * <li>a {@literal <} b cuando a.compareTo(b) {@literal <} 0<br> + * <li>a {@literal >}= b cuando a.compareTo(b) {@literal >}= 0<br> + * <li>a {@literal <}= b cuando a.compareTo(b) {@literal <}= 0<br> + * </ul> + * + * @param otro + * fecha con la que comparamos + * @return -1,0 o 1 + * @throws IllegalArgumentException cuando {@code otro==null} + */ + public int comparaCon(Fecha otro) { + if (otro==null) + throw new IllegalArgumentException("LLamada incorrecta: otro==null"); + if (getAnno() != otro.getAnno()) + return Integer.compare(getAnno(), otro.getAnno()); + else if (getMes() != otro.getMes()) + return Integer.compare(getMes(), otro.getMes()); + else if (getDia() != otro.getDia()) + return Integer.compare(getDia(), otro.getDia()); + else + return 0; + } + + /** Comparar si una fecha es igual a otra. + * ¿Porqué no podemos utilizar equals() como decÃa alguna de las transparencias + * de la asignatura? + * + * @param f + * Fecha con que comparamos this + * @return Cierto si los dos objetos representan la misma fecha + * @throws IllegalArgumentException cuando {@code f==null} + */ + public boolean igualA(Fecha f) { + if (f==null) + throw new IllegalArgumentException("LLamada incorrecta: f==null"); + + return getDia() == f.getDia() && getMes() == f.getMes() && getAnno() == f.getAnno(); + } + + /** + * Representación elemental en formato numérico + * @return La fecha en formato legible, d-m-a + */ + public String aCadena() { + return getDia()+"-"+getMes()+"-"+getAnno(); + } + + /** + * Dado un mes y un año, se obtiene el número de los dÃas de ese mes. Se tiene en cuenta que el año sea + * bisiesto + */ + public int diasMes(int m, int a) { + int resultado = 0; + switch (m) { + case 1: + case 3: + case 5: + case 7: + case 8: + case 10: + case 12: + resultado = 31; + break; + case 4: + case 6: + case 9: + case 11: + resultado = 30; + break; + case 2: + if ((a % 4 == 0) && (a % 100 != 0) || (a % 400 == 0)) + resultado = 29; + else + resultado = 28; + } + return resultado; + } + +/** + * Es un método que dado una fecha en formato dia , mes y año, nos devuelve si es correcta o no + */ +private boolean esFechaCorrecta(int dd, int mm, int aa){ + if ((dd>diasMes(mm,aa)) || (dd<1) ||(mm<1) || (mm>12)||(aa<1000)||(aa>9999)){ + return false; + }else + { + return true; + } + + } +} \ No newline at end of file diff --git a/src/es/markse/Trayecto.java b/src/es/markse/Trayecto.java index 670ae55..77047b6 100644 --- a/src/es/markse/Trayecto.java +++ b/src/es/markse/Trayecto.java @@ -2,70 +2,153 @@ * Copyright Universidad de Valladolid */ package es.markse; -import java.util.*; -import java.time.LocalDate; + /** * Implementacion de la clase Trayecto - * @author vicma + * @author vicmtorm + * @author javcalv * */ public class Trayecto { - private Muelle muelleOrigen; private Puerto puertoOrigen; - private LocalDate fechaInicioTrayecto; + private Fecha fechaInicioTrayecto; private Muelle muelleDestino; private Puerto puertoDestino; - private LocalDate fechaFinTrayecto; - + private Fecha fechaFinTrayecto; /** - * - * @param MuelleOrigen - * @param PuertoOrigen - * @param FechaInicioTrayecto - * @param MuelleDestino - * @param PuertoDestino - * @param FechaFinTrayecto + * Constructor del objeto Trayecto. + * @param muelleOrigen Muelle desde el que se empieza el Trayecto + * @param puertoOrigen Puerto desde el que se empieza el Trayecto + * @param fechaInicioTrayecto Fecha en la que comienza el Trayecto + * @param muelleDestino Muelle en el que finaliza un Trayecto + * @param puertoDestino Puerto en el que finaliza un Trayecto + * @param fechaFinTrayecto Fecha en la que finaliza un Trayecto */ - public Trayecto (Muelle MuelleOrigen, Puerto PuertoOrigen, LocalDate FechaInicioTrayecto, Muelle MuelleDestino, Puerto PuertoDestino, LocalDate FechaFinTrayecto) { - this.muelleOrigen=MuelleOrigen; - this.puertoOrigen=PuertoOrigen; - this.fechaInicioTrayecto=FechaInicioTrayecto; - this.muelleDestino = MuelleDestino; - this.puertoDestino = PuertoDestino; - this.fechaFinTrayecto = FechaFinTrayecto; + public Trayecto (Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { + this.muelleOrigen=muelleOrigen; + this.puertoOrigen=puertoOrigen; + this.fechaInicioTrayecto=fechaInicioTrayecto; + this.muelleDestino = muelleDestino; + this.puertoDestino = puertoDestino; + this.fechaFinTrayecto = fechaFinTrayecto; + comprobacionFecha(fechaInicioTrayecto); + comprobacionFecha(fechaFinTrayecto); + comprobacionOrdenFechas(); } - public boolean errorFechaFinTrayecto(LocalDate fechaDada) { - if(fechaFinTrayecto.compareTo(fechaDada)!=0) { - System.out.println("Las Fechas No Coinciden"); + public Muelle getMuelleOrigen() { + return this.muelleOrigen; + } + public Muelle getMuelleDestino() { + return this.muelleDestino; + } + public Puerto getPuertoOrigen() { + return this.puertoOrigen; + } + public Puerto getPuertoDestino() { + return this.puertoDestino; + } + public Fecha getFechaInicioTrayecto() { + return this.fechaInicioTrayecto; + } + public Fecha getFechaFinTrayecto() { + return this.fechaFinTrayecto; + } + + /** + * Método que Comprueba si la Fecha Introducida al Declarar un nuevo Trayecto es válida o no + * @param fechaDada Fecha Introducida por el Usuario + */ + + private void comprobacionFecha(Fecha fechaDada) { + if (fechaDada == null) { + throw new IllegalArgumentException ("La Fecha introducida no puede ser nula"); + } + int d = fechaDada.getDia(); + int m = fechaDada.getMes(); + int a = fechaDada.getAnno(); + + if((d>fechaDada.diasMes(m,a))|| (d<1) || (m<1) || (m>12) || (a<1000) || (a>9999)) { + throw new IllegalArgumentException ("La Fecha Introducida no es Válida"); + } + } + + /** + * Método que Comprueba si el Orden de las Fechas de Inicio y del Final del Trayecto es el adecuado. + */ + + + private void comprobacionOrdenFechas() { + if (this.fechaInicioTrayecto.getDiasDesdeEpoch()>this.fechaFinTrayecto.getDiasDesdeEpoch()){ + throw new IllegalArgumentException ("La Fecha del Inicio del Trayecto no puede ser posterior a la fecha del Fin del Trayecto"); + } + } + + /** + * Método que indica si la Fecha de Fin de Trayecto es superior a una Fecha Dada + * @param fechaDada Fecha de Introducida por el usuario + * @return True si la Fecha de Fin de Trayecto es Superior, y False si la Fecha de Fin de Trayecto no es Superior + */ + + + public boolean FechaFinTrayectoSuperior(Fecha fechaDada) { + if (fechaFinTrayecto.getDiasDesdeEpoch()>fechaDada.getDiasDesdeEpoch()) { + System.out.println("La Fecha del Fin del Trayecto es superior a la Fecha Dada"); return true; - }; - System.out.println("Las Fechas Coinciden"); - return false; + } + else { + System.out.println("La Fecha del Fin del Trayecto NO es superior a la Fecha Dada"); + return false; + } + } - public double precioTrayectoEnEuros(int costeDiarioTrayecto, int costePorMilla) { - int dias = fechaFinTrayecto.getDayOfYear()-fechaInicioTrayecto.getDayOfYear(); + /** + * Método que, introduciendo el coste diario y el coste por milla, devuelve el precio calculado en Euros de un Trayecto + * @param costeDiarioTrayecto Valor en Euros del Coste Diario de un Trayecto + * @param costePorMilla Valor en Euros del Coste por Milla de un Trayecto + * @return Precio del Trayecto Calculado en Euros + */ + + public double precioTrayectoEnEuros(double costeDiarioTrayecto, double costePorMilla) { + if (costeDiarioTrayecto <= 0) { + throw new IllegalArgumentException ("El Coste Diario del Trayecto no es válido"); + } + if (costePorMilla <= 0) { + throw new IllegalArgumentException ("El Coste por Milla del Trayecto no es válido"); + } + int dias = fechaFinTrayecto.getDiasDesdeEpoch()-fechaInicioTrayecto.getDiasDesdeEpoch(); double dist = distanciaMillasMarinas(); - return ((dias*costeDiarioTrayecto)+(dist*costePorMilla)); + return ((dias*costeDiarioTrayecto)+(dist*costePorMilla)); } + + /** + * Método que devuelve la Distancia calculada en Millas Marinas entre 2 Muelles Distintos + * @return Distancia entre 2 Muelles Distintos calculada en Millas Marinas + * + */ public double distanciaMillasMarinas() { return muelleOrigen.getGPSCoordinate().getDistanceTo(muelleDestino.getGPSCoordinate()); } + + /** + * Método que Proporciona Información acerca del Trayecto: Localidad y PaÃs de los Puertos de Origen y Destino, y Fechas de Inicio y Fin del Trayecto + * @return Información Sobre el Origen (Ciudad, Pais, Fecha Inicio) y el Destino (Ciudad, Pais, Fecha Fin) + */ + public String inforTrayecto() { String ciudadOrig = this.puertoOrigen.ciudadDelPuerto(); String paisOrig = this.puertoOrigen.paisDelPuerto(); - String FechaInicio = this.fechaInicioTrayecto.toString(); + String FechaInicio = this.fechaInicioTrayecto.aCadena(); String ciudadDest = this.puertoDestino.ciudadDelPuerto(); String paisDest = this.puertoDestino.paisDelPuerto(); - String FechaFin = this.fechaFinTrayecto.toString(); - + String FechaFin = this.fechaFinTrayecto.aCadena(); + return"INFORMACION SOBRE EL ORIGEN: \nCiudad Origen: "+ciudadOrig+"\nPais Origen: "+paisOrig+"\nFecha del Inicio: "+FechaInicio+ "\nINFORMACION SOBRE EL DESTINO: \nCiudad Destino: "+ciudadDest+"\nPais Destino: "+paisDest+"\nFecha del Fin: "+FechaFin; - } } diff --git a/uses/es/markse/MuelleTest.java b/uses/es/markse/MuelleTest.java index e319683..700c2ad 100644 --- a/uses/es/markse/MuelleTest.java +++ b/uses/es/markse/MuelleTest.java @@ -366,7 +366,6 @@ public class MuelleTest { GPSCoordinate cord = new GPSCoordinate(5d, 5d); Muelle m = new Muelle("01", cord, true, 1,3); Muelle m2 = new Muelle("01", cord, true, 1,2); - Muelle m3 = new Muelle("01", cord, true, 1,1); Contenedor c = new Contenedor("DFR", 'J', "056731", 100, 400, 500, false, false); Contenedor c2 = new Contenedor("ZZE", 'J', "056731", 100, 400, 500, false, true); Contenedor c3 = new Contenedor("ZRE", 'J', "056731", 100, 400, 500, false, false); diff --git a/uses/es/markse/TrayectoTest.java b/uses/es/markse/TrayectoTest.java new file mode 100644 index 0000000..9d9305a --- /dev/null +++ b/uses/es/markse/TrayectoTest.java @@ -0,0 +1,131 @@ +package es.markse; + +import static org.junit.Assert.*; + +import org.junit.Test; +import es.uva.inf.poo.maps.GPSCoordinate; +public class TrayectoTest { + + @Test + public void testTrayecto() { + GPSCoordinate c1 = new GPSCoordinate(5d,5d); + GPSCoordinate c2 = new GPSCoordinate(10d,10d); + Muelle m1 = new Muelle ("01",c1,true,2,2); + Muelle m2 = new Muelle ("02",c2,true,2,2); + Puerto p1 = new Puerto ("ES","BAR"); + Puerto p2 = new Puerto ("ES","BAR"); + Fecha f1 = new Fecha (9,11,2024); + Fecha f2 = new Fecha(11,11,2024); + Trayecto t = new Trayecto(m1,p1,f1,m2,p2,f2); + assertEquals(m1,t.getMuelleOrigen()); + assertEquals(p1,t.getPuertoOrigen()); + assertEquals(f1,t.getFechaInicioTrayecto()); + assertEquals(m2,t.getMuelleDestino()); + assertEquals(p2,t.getPuertoDestino()); + assertEquals(f2,t.getFechaFinTrayecto()); + } + + @Test + public void testGetMuelleOrigen() { + GPSCoordinate c1 = new GPSCoordinate(5d,5d); + GPSCoordinate c2 = new GPSCoordinate(10d,10d); + Muelle m1 = new Muelle ("01",c1,true,2,2); + Muelle m2 = new Muelle ("02",c2,true,2,2); + Puerto p1 = new Puerto ("ES","PAL"); + Puerto p2 = new Puerto ("AR","BUE"); + Fecha f1 = new Fecha (9,11,2024); + Fecha f2 = new Fecha(11,11,2024); + Trayecto t = new Trayecto(m1,p1,f1,m2,p2,f2); + assertEquals(m1,t.getMuelleOrigen()); + } + + @Test + public void testGetMuelleDestino() { + GPSCoordinate c1 = new GPSCoordinate(5d,5d); + GPSCoordinate c2 = new GPSCoordinate(10d,10d); + Muelle m1 = new Muelle ("01",c1,true,2,2); + Muelle m2 = new Muelle ("02",c2,true,2,2); + Puerto p1 = new Puerto ("ES","PAL"); + Puerto p2 = new Puerto ("AR","BUE"); + Fecha f1 = new Fecha (9,11,2024); + Fecha f2 = new Fecha(11,11,2024); + Trayecto t = new Trayecto(m1,p1,f1,m2,p2,f2); + assertEquals(m2,t.getMuelleDestino()); + } + + @Test + public void testGetPuertoOrigen() { + GPSCoordinate c1 = new GPSCoordinate(5d,5d); + GPSCoordinate c2 = new GPSCoordinate(10d,10d); + Muelle m1 = new Muelle ("01",c1,true,2,2); + Muelle m2 = new Muelle ("02",c2,true,2,2); + Puerto p1 = new Puerto ("ES","PAL"); + Puerto p2 = new Puerto ("AR","BUE"); + Fecha f1 = new Fecha (9,11,2024); + Fecha f2 = new Fecha(11,11,2024); + Trayecto t = new Trayecto(m1,p1,f1,m2,p2,f2); + assertEquals(p1,t.getPuertoOrigen()); + } + + @Test + public void testGetPuertoDestino() { + GPSCoordinate c1 = new GPSCoordinate(5d,5d); + GPSCoordinate c2 = new GPSCoordinate(10d,10d); + Muelle m1 = new Muelle ("01",c1,true,2,2); + Muelle m2 = new Muelle ("02",c2,true,2,2); + Puerto p1 = new Puerto ("ES","PAL"); + Puerto p2 = new Puerto ("AR","BUE"); + Fecha f1 = new Fecha (9,11,2024); + Fecha f2 = new Fecha(11,11,2024); + Trayecto t = new Trayecto(m1,p1,f1,m2,p2,f2); + assertEquals(p2,t.getPuertoDestino()); + } + + @Test + public void testGetFechaInicioTrayecto() { + GPSCoordinate c1 = new GPSCoordinate(5d,5d); + GPSCoordinate c2 = new GPSCoordinate(10d,10d); + Muelle m1 = new Muelle ("01",c1,true,2,2); + Muelle m2 = new Muelle ("02",c2,true,2,2); + Puerto p1 = new Puerto ("ES","PAL"); + Puerto p2 = new Puerto ("AR","BUE"); + Fecha f1 = new Fecha (9,11,2024); + Fecha f2 = new Fecha(11,11,2024); + Trayecto t = new Trayecto(m1,p1,f1,m2,p2,f2); + assertEquals(f1,t.getFechaInicioTrayecto()); + } + + @Test + public void testGetFechaFinTrayecto() { + GPSCoordinate c1 = new GPSCoordinate(5d,5d); + GPSCoordinate c2 = new GPSCoordinate(10d,10d); + Muelle m1 = new Muelle ("01",c1,true,2,2); + Muelle m2 = new Muelle ("02",c2,true,2,2); + Puerto p1 = new Puerto ("ES","PAL"); + Puerto p2 = new Puerto ("AR","BUE"); + Fecha f1 = new Fecha (9,11,2024); + Fecha f2 = new Fecha(11,11,2024); + Trayecto t = new Trayecto(m1,p1,f1,m2,p2,f2); + assertEquals(f2,t.getFechaFinTrayecto()); + } + + @Test + public void comprobacionFecha() { + } + + @Test + public void testPrecioTrayectoEnEuros() { + fail("Not yet implemented"); + } + + @Test + public void testDistanciaMillasMarinas() { + fail("Not yet implemented"); + } + + @Test + public void testInforTrayecto() { + fail("Not yet implemented"); + } + +} \ No newline at end of file -- GitLab