diff --git a/src/es/markse/TBarco.java b/src/es/markse/TBarco.java index 97474f0ce7168522436b0670ed223c87d36c9cb5..2e896082e4f960b1c8e55d8e18f67a68641b41de 100644 --- a/src/es/markse/TBarco.java +++ b/src/es/markse/TBarco.java @@ -8,7 +8,7 @@ package es.markse; * @author javcalv * @author victorm */ -public class TBarco extends TrayectoSimple { +public class TBarco extends TSimple { private final double COSTE_DIARIO = 4000; @@ -26,10 +26,11 @@ public class TBarco extends TrayectoSimple { public TBarco(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); + //Comprobamos si el Muelle origen/destino esta alejado del barco if(muelleOrigen.alejadoBarco()) - throw new IllegalStateException("El Muelle de origen esta alejado del barco (No puede usar ese transporte)"); + throw new IllegalStateException("El Muelle de origen esta alejado del barco (No puede usar este transporte)"); if(muelleDestino.alejadoBarco()) - throw new IllegalStateException("El Muelle de destino esta alejado del barco"); + throw new IllegalStateException("El Muelle de destino esta alejado del barco (No puede usar ese transporte)"); } /** diff --git a/src/es/markse/TCamion.java b/src/es/markse/TCamion.java index b45da73e38c64d599bd5b2addc74258641cc6c98..74ffc9864f4321f200d19e77c3d6622b27ec5e38 100644 --- a/src/es/markse/TCamion.java +++ b/src/es/markse/TCamion.java @@ -8,10 +8,10 @@ package es.markse; * @author javcalv * @author victorm */ -public class TCamion extends TrayectoSimple { +public class TCamion extends TVehiculoTierra { - private double COSTE_FIJO = 200; - private double COSTE_POR_KILOMETRO = 4.5; + private final double COSTE = 200; + private final double COSTE_KILOMETRO = 4.5; /** * Constructor de la clase TCamion @@ -24,36 +24,9 @@ public class TCamion extends TrayectoSimple { */ public TCamion(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); - } - - /** - * Método que calcula el coste del trayecto por camión - * @return Coste del trayecto en euros - */ - @Override - 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; + + //Inicializamos el valor para el Camion + this.COSTE_FIJO = this.COSTE; + this.COSTE_POR_KILOMETRO = this.COSTE_KILOMETRO; } } \ No newline at end of file diff --git a/src/es/markse/TrayectoCombinado.java b/src/es/markse/TCombinado.java similarity index 76% rename from src/es/markse/TrayectoCombinado.java rename to src/es/markse/TCombinado.java index 2854f6527f9f89e91691ac65a84c1f1e33abb20a..6cf308c873bae3a2f701a570b9101477790bb8a2 100644 --- a/src/es/markse/TrayectoCombinado.java +++ b/src/es/markse/TCombinado.java @@ -9,10 +9,10 @@ import java.util.ArrayList; * @author javcalv * @author victorm */ -public abstract class TrayectoCombinado extends Trayecto { +public abstract class TCombinado extends Trayecto { //ArrayList para guardar todos los trayectos simples - protected ArrayList<TrayectoSimple> trayectosSimples; + protected ArrayList<TSimple> trayectosSimples; /** * Constructor de la clase abstracta TrayectoCombinado @@ -23,7 +23,7 @@ public abstract class TrayectoCombinado extends Trayecto { * @param puertoDestino Puerto en el que finaliza un Trayecto * @param fechaFinTrayecto Fecha en la que finaliza un Trayecto */ - public TrayectoCombinado(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { + public TCombinado(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); this.trayectosSimples = new ArrayList<>(); } @@ -31,7 +31,7 @@ public abstract class TrayectoCombinado extends Trayecto { /********************************************************* * METODOS EJECUTADOS POR EL COSTRUCTOR DE LAS SUBCLASES * *********************************************************/ - protected boolean estanVacios(ArrayList<TrayectoSimple> ts) { + protected boolean estanVacios(ArrayList<TSimple> ts) { return (ts.isEmpty() || ts == null) ? true : false; } @@ -40,9 +40,9 @@ public abstract class TrayectoCombinado extends Trayecto { * @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) { + protected boolean comprobarSiHayTrayectosCamion(ArrayList<TSimple> ts) { int trayectosCamiones = 0; - for (TrayectoSimple t : ts) { + for (TSimple t : ts) { if (t instanceof TCamion) trayectosCamiones++; } return (trayectosCamiones == 0)? false : true; @@ -52,12 +52,13 @@ public abstract class TrayectoCombinado extends Trayecto { * 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 + * @return true si es un orden correcto o false si no lo es + * @throws IllegalArgumentException Si hay elementos vacios de poer medio */ - protected boolean ordenTrayectosCorrecto(ArrayList<TrayectoSimple> ts) { + protected boolean ordenTrayectosCorrecto(ArrayList<TSimple> ts) { for (int i = 0; i<(ts.size() - 1); i++) { - TrayectoSimple t1 = ts.get(i); - TrayectoSimple t2 = ts.get(i + 1); + TSimple t1 = ts.get(i); + TSimple t2 = ts.get(i + 1); if (t1 == null || t2 == null) { throw new IllegalArgumentException("Hay trayectos nulos en la lista"); } @@ -73,7 +74,7 @@ public abstract class TrayectoCombinado extends Trayecto { * @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) { + private boolean comprobarDestinoOrigenIguales(TSimple t1, TSimple t2) { return (t1.getMuelleDestino().getIdentificador().equals(t2.getMuelleOrigen().getIdentificador()) && t1.getPuertoDestino().identificadorPuerto().equals(t2.getPuertoOrigen().identificadorPuerto())) ? true : false; diff --git a/src/es/markse/TPackCamionBarco.java b/src/es/markse/TPackCamionBarco.java index fef3ffa9a5774de70558e5b12758b2a61dff4866..59691136fa599335d982728015c2951e368deee1 100644 --- a/src/es/markse/TPackCamionBarco.java +++ b/src/es/markse/TPackCamionBarco.java @@ -10,10 +10,7 @@ import java.util.ArrayList; * @author javcalv * @author victorm */ -public class TPackCamionBarco extends TrayectoCombinado { - - private final ArrayList<TCamion> trayectosCamion; - private final ArrayList<TBarco> trayectosBarco; +public class TPackCamionBarco extends TCombinado { //Descuentos que se aplican private final double DESCUENTO = 0.85; @@ -30,32 +27,80 @@ public class TPackCamionBarco extends TrayectoCombinado { * @param trayectosBarco */ public TPackCamionBarco(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, - Puerto puertoDestino, Fecha fechaFinTrayecto, ArrayList<TCamion> trayectosCamion, ArrayList<TBarco> trayectosBarco) { + Puerto puertoDestino, Fecha fechaFinTrayecto, ArrayList<TSimple> trayectosSimples) { super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); //Comprobamos si los trayectos no estan nulos (Debe de tener uno al menos) - if(trayectosCamion == null) + if(estanVacios(trayectosSimples)) 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; + //Comprobamos si las clases son solo de tipo Camion o Tren + if (trayectosCorrectos(trayectosSimples)) + throw new IllegalArgumentException("Las clases agregadas al ArrayList son invalidas"); + + //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.comprobarSiHayTrayectosBarco(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 para comparar si las instancias de los trayectos simples son correctas + * @param ts Arraylist con los Trayectos a comparar + * @return true si son correctos o false si no lo son + */ + private boolean trayectosCorrectos(ArrayList<TSimple> ts) { + for (TSimple t : ts) { + if (!(t instanceof TCamion || t instanceof TBarco)) { + return false; + } + } + return true; + } + + /** + * Metodo que comprueba si hay trayectos de barco 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 comprobarSiHayTrayectosBarco(ArrayList<TSimple> trayectos) { + int trayectosBarco = 0; + for (TSimple t : trayectos) { + if (t instanceof TBarco) trayectosBarco++; + } + return (trayectosBarco == 0)? false : true; + } + + /************************ + * METODOS DEL TRAYECTO * + ************************/ + + /** * Método que calcula el coste del trayecto combinado de camión y barco * @return Coste del trayecto en euros */ @Override public double costeTrayecto() { double coste = 0; - for (TCamion camion : this.trayectosCamion) { - coste += camion.costeTrayecto(); - } - for (TBarco barco : this.trayectosBarco) { - coste += barco.costeTrayecto() * this.DESCUENTO; - } - return coste; + for (TSimple ts: this.trayectosSimples) { + if (ts instanceof TBarco) { + TBarco tb = (TBarco) ts; + coste += tb.costeTrayecto() * this.DESCUENTO; + } + else { + TCamion tc = (TCamion) ts; + coste += tc.costeTrayecto(); + } + } + return coste; } } \ No newline at end of file diff --git a/src/es/markse/TPackCamionTren.java b/src/es/markse/TPackCamionTren.java index 2c3aa7ce987cb3dc68c3e07002e59c8d2e3ab009..6f0eeb83e733fdcd7353cd07ce6c5499f51049bb 100644 --- a/src/es/markse/TPackCamionTren.java +++ b/src/es/markse/TPackCamionTren.java @@ -9,11 +9,11 @@ import java.util.ArrayList; * @author javcalv * @author victorm */ -public class TPackCamionTren extends TrayectoCombinado { +public class TPackCamionTren extends TCombinado { - //Nuevas ofertas para este pack - private final int COSTE_FIJO = 0; - private final int COSTE_POR_KILOMETRO = 10; + //Reduccion de los precios + private final int RED_COSTE_FIJO = 0; + private final int RED_COSTE_POR_KILOMETRO = 10; /** * Constructor de la clase PackCamionTren @@ -24,15 +24,24 @@ public class TPackCamionTren extends TrayectoCombinado { * @param puertoDestino Puerto en el que finaliza un Trayecto * @param fechaFinTrayecto Fecha en la que finaliza un Trayecto * @param trayectosSimples ArrayList con los trayectos simples + * @throws IllegalArgumentException Si la lista esta vacia + * @throws IllegalArgumentException Si las clases agregadas en el ArrayList son invalidas + * @throws IllegalArgumentException Si no se encuentra un solo trayecto de camion en la lista + * @throws IllegalArgumentException Si no se encuentra un solo trayecto de tren en la lista + * @throws IllegalArgumentException Si El orden de los trayectos no es correcto */ public TPackCamionTren(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, - Puerto puertoDestino, Fecha fechaFinTrayecto, ArrayList<TrayectoSimple> trayectosSimples) { + Puerto puertoDestino, Fecha fechaFinTrayecto, ArrayList<TSimple> trayectosSimples){ super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); //Comprobamos que los trayectos no sean nulos/vacios if (estanVacios(trayectosSimples)) throw new IllegalArgumentException("Los trayectos no pueden ser nulos/vacios"); + //Comprobamos si las clases son solo de tipo Camion o Tren + if (trayectosCorrectos(trayectosSimples)) + throw new IllegalArgumentException("Las clases agregadas al ArrayList son invalidas"); + //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)"); @@ -52,14 +61,28 @@ public class TPackCamionTren extends TrayectoCombinado { * @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) { + private boolean comprobarSiHayTrayectosTren(ArrayList<TSimple> trayectos) { int trayectosTren = 0; - for (TrayectoSimple t : trayectos) { + for (TSimple t : trayectos) { if (t instanceof TTren) trayectosTren++; } return (trayectosTren == 0)? false : true; } + /** + * Metodo para comparar si las instancias de los trayectos simples son correctas + * @param ts Arraylist con los Trayectos a comparar + * @return true si son correctos o false si no lo son + */ + private boolean trayectosCorrectos(ArrayList<TSimple> ts) { + for (TSimple t : ts) { + if (!(t instanceof TCamion || t instanceof TTren)) { + return false; + } + } + return true; + } + /************************ * METODOS DEL TRAYECTO * @@ -70,11 +93,15 @@ public class TPackCamionTren extends TrayectoCombinado { * @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() { - for (TrayectoSimple ts : this.trayectosSimples) { - ts. + double coste = 0; + for (TSimple ts : this.trayectosSimples) { + //Sabemos que obligaotoriamente es de tipo TVehiculoTierra + TVehiculoTierra tv = (TVehiculoTierra) ts; + tv.COSTE_FIJO = this.RED_COSTE_FIJO; + tv.COSTE_POR_KILOMETRO = this.RED_COSTE_POR_KILOMETRO; + coste += tv.costeTrayecto(); } + return coste; } - } \ No newline at end of file diff --git a/src/es/markse/TrayectoSimple.java b/src/es/markse/TSimple.java similarity index 85% rename from src/es/markse/TrayectoSimple.java rename to src/es/markse/TSimple.java index 4a5af2e5e042e67b45bb9a31c725e38f82699418..db15bd8cc2f245f963de7ce1abaa73f34fe7dbb8 100644 --- a/src/es/markse/TrayectoSimple.java +++ b/src/es/markse/TSimple.java @@ -8,7 +8,7 @@ package es.markse; * @author javcalv * @author victorm */ -public abstract class TrayectoSimple extends Trayecto { +public abstract class TSimple extends Trayecto { protected double distanciaKilometros; private final float MILLAS_A_KILOMETROS = 1.852f; @@ -22,7 +22,7 @@ public abstract class TrayectoSimple extends Trayecto { * @param puertoDestino Puerto en el que finaliza un Trayecto * @param fechaFinTrayecto Fecha en la que finaliza un Trayecto */ - public TrayectoSimple(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { + public TSimple(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); //Obtenemos la distancia en millas marinas del trayecto y las convertimos en kilometros diff --git a/src/es/markse/TTren.java b/src/es/markse/TTren.java index c491cf259939f00bc5ad9c8af3a0a99bed4fd31f..c997835944c6f4f6312aec8d6c91d18bf584dee7 100644 --- a/src/es/markse/TTren.java +++ b/src/es/markse/TTren.java @@ -8,10 +8,10 @@ package es.markse; * @author javcalv * @author victorm */ -public class TTren extends TrayectoSimple { +public class TTren extends TVehiculoTierra{ - private double COSTE_FIJO = 20; - private double COSTE_POR_KILOMETRO = 12.5; + private double COSTE = 20; + private double COSTE_KILOMETRO = 12.5; /** * Constructor de la clase TTren @@ -31,39 +31,10 @@ public class TTren extends TrayectoSimple { 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 - * @return Coste del trayecto en euros - */ - @Override - 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; + throw new IllegalStateException("El muelle destino no puede usar el transporte Tren"); + + //Inicializamos los valores para el trayecto Tren + this.COSTE_FIJO = COSTE; + this.COSTE_POR_KILOMETRO = this.COSTE_KILOMETRO; } } \ No newline at end of file diff --git a/src/es/markse/TVehiculoTierra.java b/src/es/markse/TVehiculoTierra.java new file mode 100644 index 0000000000000000000000000000000000000000..5af5d9e7757f50eb89c8f059332a287e4b504dcc --- /dev/null +++ b/src/es/markse/TVehiculoTierra.java @@ -0,0 +1,46 @@ +package es.markse; + +public abstract class TVehiculoTierra extends TSimple{ + + protected double COSTE_FIJO; + protected double COSTE_POR_KILOMETRO; + + public TVehiculoTierra(Muelle muelleOrigen, Puerto puertoOrigen, Fecha fechaInicioTrayecto, Muelle muelleDestino, Puerto puertoDestino, Fecha fechaFinTrayecto) { + super(muelleOrigen, puertoOrigen, fechaInicioTrayecto, muelleDestino, puertoDestino, fechaFinTrayecto); + } + + /************************************************** + * METODOS QUE SE APLICARAN A VEHICULOS DE TIERRA * + **************************************************/ + + /** + * Método que calcula el coste del trayecto por camión + * @return Coste del trayecto en euros + */ + @Override + 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; + } +}