diff --git a/src/es/markse/Muelle.java b/src/es/markse/Muelle.java index eb4a0ebd25d029b7f01aca1c2e780c9980c20fea..e187462545095ebbf529b9f80f368295aec279ae 100644 --- a/src/es/markse/Muelle.java +++ b/src/es/markse/Muelle.java @@ -24,6 +24,10 @@ public class Muelle { private final Pattern IDENTIFICADOR_REGUEX = Pattern.compile("^\\d{2}$"); private final Pattern CODIGO_CONTENEDOR = Pattern.compile("[A-Z]{4}\\d{7}"); + //Modificaciones Practica 2 + private boolean utilidadTren; + private boolean alejadoBarco; + private static class Plaza { private int altura; private final List<Contenedor> contenedores = new ArrayList<>(); @@ -162,8 +166,13 @@ public class Muelle { * @param estadoActual Estado del muelle. * @param plazas Numero de plazas totales que tiene el Muelle * @param altura numero maximo de contenedores que se pueden apilar encima de otro + * @param utilidadTren booleano que indica si un muelle puede usar un tren como transporte + * @param alejadoBarco booleano que indica si esta alejado de un barco (puede o no usar ese transporte) + * @throws IllegalArgumentException si el identificador o la coordenada es nulo + * @throws IllegalArgumentException si el tamaño de la altura y plaza son menores a 1 + * @throws IllegalArgumentException si el identificador tiene un formato incorrecto */ - public Muelle (String identificador, GPSCoordinate cord, estado estadoActual, int plazas, int altura) { + public Muelle (String identificador, GPSCoordinate cord, estado estadoActual, int plazas, int altura, boolean utilidadTren, boolean alejadoBarco) { if(identificador == null || cord == null) throw new IllegalArgumentException("Los valores del identificador y coordenada no pueden ser nulos"); if(plazas<1 || altura<1) @@ -179,6 +188,10 @@ public class Muelle { for (int i = 0; i < plazas; i++) { this.plazas[i] = new Plaza(altura); } + + //Modificaciones practica 2 + this.alejadoBarco = alejadoBarco; + this.utilidadTren = utilidadTren; } /** @@ -405,4 +418,20 @@ public class Muelle { public Plaza[] plazas() { return this.plazas.clone(); } + + /** + * Metodo que devuelve si el muelle puede usar el tren + * @return booleano con la utilidad del tren + */ + public boolean utilidadTren() { + return this.utilidadTren; + } + + /** + * Metodo que indica si el muelle esta alejado de un barco. + * @return booleano que indica si el muelle esta alejado o no de un barco + */ + public boolean alejadoBarco() { + return this.alejadoBarco; + } } \ No newline at end of file diff --git a/src/es/markse/TBarco.java b/src/es/markse/TBarco.java index 564cab546fba79bffcc3e454fe318ddd3061dfbc..97474f0ce7168522436b0670ed223c87d36c9cb5 100644 --- a/src/es/markse/TBarco.java +++ b/src/es/markse/TBarco.java @@ -9,7 +9,8 @@ package es.markse; * @author victorm */ public class TBarco extends TrayectoSimple { - private static final double COSTE_DIARIO = 4000; + + private final double COSTE_DIARIO = 4000; /** * Constructor de la clase TBarco @@ -19,9 +20,16 @@ public class TBarco extends TrayectoSimple { * @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 + * @throws IllegalStateException el Muelle de origen esta alejado del barco + * @throws IllegalStateException el Muelle de destino esta alejado del barco */ public TBarco(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); + + if(muelleOrigen.alejadoBarco()) + throw new IllegalStateException("El Muelle de origen esta alejado del barco (No puede usar ese transporte)"); + if(muelleDestino.alejadoBarco()) + throw new IllegalStateException("El Muelle de destino esta alejado del barco"); } /** diff --git a/src/es/markse/TCamion.java b/src/es/markse/TCamion.java index 96535c84e1bad01b734a37b0166207c1f5613c34..b45da73e38c64d599bd5b2addc74258641cc6c98 100644 --- a/src/es/markse/TCamion.java +++ b/src/es/markse/TCamion.java @@ -10,8 +10,8 @@ package es.markse; */ public class TCamion extends TrayectoSimple { - private static final double COSTE_FIJO = 200; - private static final double COSTE_POR_KILOMETRO = 4.5; + private double COSTE_FIJO = 200; + private double COSTE_POR_KILOMETRO = 4.5; /** * Constructor de la clase TCamion @@ -34,4 +34,26 @@ public class TCamion extends TrayectoSimple { public double costeTrayecto() { return COSTE_FIJO + (this.distanciaKilometros * COSTE_POR_KILOMETRO); } + + /** + * Metodo para cambiar el coste fijo de un trayecto + * @param coste coste fijo nuevo + * @throws IllegalArgumentException el coste no puede ser negativo + */ + public void cambiarCosteFijo(double coste) { + if(coste<0) + throw new IllegalArgumentException("Coste no puede ser negativo"); + this.COSTE_FIJO = coste; + } + + /** + * Metodo para cambiar el coste por kilometro de un trayecto + * @param coste coste por kilometro nuevo + * @throws IllegalArgumentException el coste no puede ser negativo + */ + public void cambiarCostePorKilometro(double coste) { + if(coste<0) + throw new IllegalArgumentException("Coste no puede ser negativo"); + this.COSTE_POR_KILOMETRO = coste; + } } \ No newline at end of file diff --git a/src/es/markse/TPackCamionBarco.java b/src/es/markse/TPackCamionBarco.java index bb6046c339d83fdfeb084593d2506312e6ecfb0f..fef3ffa9a5774de70558e5b12758b2a61dff4866 100644 --- a/src/es/markse/TPackCamionBarco.java +++ b/src/es/markse/TPackCamionBarco.java @@ -3,6 +3,8 @@ */ package es.markse; +import java.util.ArrayList; + /** * Implementacion de la clase PackCamionBarco, que representa un trayecto combinado de camión y barco. * @author javcalv @@ -10,9 +12,10 @@ package es.markse; */ public class TPackCamionBarco extends TrayectoCombinado { - //POSIBLE CAMBIO --> PUEDEN SER VARIOS TRAYECTOS DE BARCOS Y VARIOS DE CAMIONES - private TCamion camion; - private TBarco barco; + private final ArrayList<TCamion> trayectosCamion; + private final ArrayList<TBarco> trayectosBarco; + + //Descuentos que se aplican private final double DESCUENTO = 0.85; /** @@ -23,15 +26,21 @@ public class TPackCamionBarco extends TrayectoCombinado { * @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 - * @param camion Trayecto de camión - * @param barco Trayecto de barco + * @param trayectosCamion arrayList con los trayectos + * @param trayectosBarco */ - public TPackCamionBarco(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto, TCamion camion, TBarco barco) { - super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); - this.camion = camion; - this.barco = barco; - this.trayectosSimples.add(camion); - this.trayectosSimples.add(barco); + public TPackCamionBarco(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, + Puerto puertoDestino, Fecha fechaFinTrayecto, ArrayList<TCamion> trayectosCamion, ArrayList<TBarco> trayectosBarco) { + super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); + + //Comprobamos si los trayectos no estan nulos (Debe de tener uno al menos) + if(trayectosCamion == null) + throw new IllegalArgumentException("Los trayectos en camion estan vacios"); + if(trayectosBarco == null) + throw new IllegalArgumentException("Los trayectos en barco estan nulos"); + + this.trayectosCamion = trayectosCamion; + this.trayectosBarco = trayectosBarco; } /** @@ -40,8 +49,13 @@ public class TPackCamionBarco extends TrayectoCombinado { */ @Override public double costeTrayecto() { - double costeBarco = this.barco.costeTrayecto() * DESCUENTO; - double costeCamion = this.camion.costeTrayecto(); - return costeBarco + costeCamion; + double coste = 0; + for (TCamion camion : this.trayectosCamion) { + coste += camion.costeTrayecto(); + } + for (TBarco barco : this.trayectosBarco) { + coste += barco.costeTrayecto() * this.DESCUENTO; + } + return coste; } } \ No newline at end of file diff --git a/src/es/markse/TPackCamionTren.java b/src/es/markse/TPackCamionTren.java index 731d9bce896610fee133d383be29c642500367d3..2c3aa7ce987cb3dc68c3e07002e59c8d2e3ab009 100644 --- a/src/es/markse/TPackCamionTren.java +++ b/src/es/markse/TPackCamionTren.java @@ -2,6 +2,7 @@ * Copyright Universidad de Valladolid 2024 */ package es.markse; +import java.util.ArrayList; /** * Implementacion de la clase PackCamionTren, que representa un trayecto combinado de camión y tren. @@ -10,9 +11,9 @@ package es.markse; */ public class TPackCamionTren extends TrayectoCombinado { - //POSIBLES CAMBIOS --> Arraylist de trayectos ??? Cosultar - private TCamion camion; - private TTren tren; + //Nuevas ofertas para este pack + private final int COSTE_FIJO = 0; + private final int COSTE_POR_KILOMETRO = 10; /** * Constructor de la clase PackCamionTren @@ -22,24 +23,58 @@ public class TPackCamionTren extends TrayectoCombinado { * @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 - * @param camion Trayecto de camión - * @param tren Trayecto de tren + * @param trayectosSimples ArrayList con los trayectos simples */ - public TPackCamionTren(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto, TCamion camion, TTren tren) { + public TPackCamionTren(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, + Puerto puertoDestino, Fecha fechaFinTrayecto, ArrayList<TrayectoSimple> trayectosSimples) { super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); - this.camion = camion; - this.tren = tren; - trayectosSimples.add(camion); - trayectosSimples.add(tren); + + //Comprobamos que los trayectos no sean nulos/vacios + if (estanVacios(trayectosSimples)) + throw new IllegalArgumentException("Los trayectos no pueden ser nulos/vacios"); + + //Comprobar si hay un trayecto de barco y de camion al menos (Obligatorio por nuestra parte) + if (!this.comprobarSiHayTrayectosCamion(trayectosSimples)) + throw new IllegalArgumentException("En los trayectos no hay trayectos en camion (Es obligatorio)"); + if (!this.comprobarSiHayTrayectosTren(trayectosSimples)) + throw new IllegalArgumentException("En los trayectos no hay trayectos en tren (Es obligatorio)"); + + //Comprobamos que el orden de los trayectos sea correcto (Destino de uno es el origen del siguiente) + if (!this.ordenTrayectosCorrecto(trayectosSimples)) + throw new IllegalArgumentException("El orden de los trayectos no es correcto. " + + "Comprueba si el destino es el mismo que el origen del siguiente trayecto"); + + this.trayectosSimples = trayectosSimples; } - + + /** + * Metodo que comprueba si hay trayectos de tren en el pack + * @param trayectos ArrayList de los trayectos simples a comprobar + * @return true si hay trayectos de tren o false si no los hay + */ + private boolean comprobarSiHayTrayectosTren(ArrayList<TrayectoSimple> trayectos) { + int trayectosTren = 0; + for (TrayectoSimple t : trayectos) { + if (t instanceof TTren) trayectosTren++; + } + return (trayectosTren == 0)? false : true; + } + + + /************************ + * METODOS DEL TRAYECTO * + ************************/ + /** - * Método que calcula el coste del trayecto combinado de camión y tren + * Método que calcula el coste del trayecto combinado de camiónes y trenes * @return Coste del trayecto en euros */ @Override + //CAMBIAR --> CREAR SUBCLASE?? Y CREAR AHI LOS METODOS?? o PONERLOS EN EL SIMPLE Y HERENCIA public double costeTrayecto() { - double distanciaTotal = camion.distanciaKilometros + tren.distanciaKilometros; - return distanciaTotal * 10; + for (TrayectoSimple ts : this.trayectosSimples) { + ts. + } } + } \ No newline at end of file diff --git a/src/es/markse/TTren.java b/src/es/markse/TTren.java index a642a09029757a9bd9c51d99c5152c31b60423a9..c491cf259939f00bc5ad9c8af3a0a99bed4fd31f 100644 --- a/src/es/markse/TTren.java +++ b/src/es/markse/TTren.java @@ -10,8 +10,8 @@ package es.markse; */ public class TTren extends TrayectoSimple { - private static final double COSTE_FIJO = 20; - private static final double COSTE_POR_KILOMETRO = 12.5; + private double COSTE_FIJO = 20; + private double COSTE_POR_KILOMETRO = 12.5; /** * Constructor de la clase TTren @@ -21,10 +21,20 @@ public class TTren extends TrayectoSimple { * @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 + * @throws IllegalStateException si el muelle de origen no puede usar el tren + * @throws IllegalStateException si el muelle de destino no puede usar el tren */ public TTren(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); + + //Comprobamos si los muelles de origen y destino pueden usar el tren + if(!muelleOrigen.utilidadTren()) + throw new IllegalStateException("El muelle de origen no puede usar el transporte Tren"); + if(!muelleDestino.utilidadTren()) + throw new IllegalStateException("El muelle destino no puede usar el transporte Tren"); } + + /** * Método que calcula el coste del trayecto por tren @@ -34,4 +44,26 @@ public class TTren extends TrayectoSimple { public double costeTrayecto() { return COSTE_FIJO + (this.distanciaKilometros * COSTE_POR_KILOMETRO); } + + /** + * Metodo para cambiar el coste fijo de un trayecto + * @param coste coste fijo nuevo + * @throws IllegalArgumentException el coste no puede ser negativo + */ + protected void cambiarCosteFijo(double coste) { + if(coste<0) + throw new IllegalArgumentException("Coste no puede ser negativo"); + this.COSTE_FIJO = coste; + } + + /** + * Metodo para cambiar el coste por kilometro de un trayecto + * @param coste coste por kilometro nuevo + * @throws IllegalArgumentException el coste no puede ser negativo + */ + protected void cambiarCostePorKilometro(double coste) { + if(coste<0) + throw new IllegalArgumentException("Coste no puede ser negativo"); + this.COSTE_POR_KILOMETRO = coste; + } } \ No newline at end of file diff --git a/src/es/markse/TrayectoCombinado.java b/src/es/markse/TrayectoCombinado.java index 9481516244ca2192daa4e3558f9f1809c979d13a..2854f6527f9f89e91691ac65a84c1f1e33abb20a 100644 --- a/src/es/markse/TrayectoCombinado.java +++ b/src/es/markse/TrayectoCombinado.java @@ -11,9 +11,9 @@ import java.util.ArrayList; */ public abstract class TrayectoCombinado extends Trayecto { - //POSIBLE CAMBIO --> ARRAYLIST DE ARRAYLISTS - protected final ArrayList<TrayectoSimple> trayectosSimples; - + //ArrayList para guardar todos los trayectos simples + protected ArrayList<TrayectoSimple> trayectosSimples; + /** * Constructor de la clase abstracta TrayectoCombinado * @param muelleOrigen Muelle desde el que empieza el Trayecto @@ -25,7 +25,57 @@ public abstract class TrayectoCombinado extends Trayecto { */ public TrayectoCombinado(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); - this.trayectosSimples = new ArrayList<>(); } + + /********************************************************* + * METODOS EJECUTADOS POR EL COSTRUCTOR DE LAS SUBCLASES * + *********************************************************/ + protected boolean estanVacios(ArrayList<TrayectoSimple> ts) { + return (ts.isEmpty() || ts == null) ? true : false; + } + + /** + * Metodo que comprueba si hay trayectos de camion en el pack + * @param trayectos ArrayList de los trayectos simples a comprobar + * @return true si hay trayectos de camion o false si no los hay + */ + protected boolean comprobarSiHayTrayectosCamion(ArrayList<TrayectoSimple> ts) { + int trayectosCamiones = 0; + for (TrayectoSimple t : ts) { + if (t instanceof TCamion) trayectosCamiones++; + } + return (trayectosCamiones == 0)? false : true; + } + + /** + * Metodo que comprueba si el arraylist tiene un orden correcto: + * - El destino del muelle/puerto es el origen del trayecto siguiente + * @param ts ArrayList de los trayectos + * @return true si es un orden corr + */ + protected boolean ordenTrayectosCorrecto(ArrayList<TrayectoSimple> ts) { + for (int i = 0; i<(ts.size() - 1); i++) { + TrayectoSimple t1 = ts.get(i); + TrayectoSimple t2 = ts.get(i + 1); + if (t1 == null || t2 == null) { + throw new IllegalArgumentException("Hay trayectos nulos en la lista"); + } + if (!comprobarDestinoOrigenIguales(t1, t2)) + return false; + } + return true; + } + + /** + * Metodo que comprueba si el puerto y muelle destino son el origen del siguiente trayecto + * @param t1 trayecto simple primero + * @param t2 trayecto compuesto posterior + * @return true si el destino y el origen de siguiente son iguales o false si no lo son + */ + private boolean comprobarDestinoOrigenIguales(TrayectoSimple t1, TrayectoSimple t2) { + return (t1.getMuelleDestino().getIdentificador().equals(t2.getMuelleOrigen().getIdentificador()) && + t1.getPuertoDestino().identificadorPuerto().equals(t2.getPuertoOrigen().identificadorPuerto())) + ? true : false; + } }