Last active
March 21, 2020 11:44
-
-
Save lmagarin/16afae04322f0e9894a6d818aac9cfc3 to your computer and use it in GitHub Desktop.
POO. Delegación. En la delegación un objeto que recibe una petición delega la ejecución del método a otro objeto. En este caso el TestAlmacen delega en Almacen, y Almacen en Articulo. Todos los métodos PUEDEN tener el mismo nombre: incrementaStock() y entradaMercancia()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Incrementa el stock del artículo con el código indicado | |
* | |
* @param codigo Código identificador del artículo que se busca | |
* @param cantidad Cantidad que quiere incrementarse | |
* @throws UnidadesNegativasException Cuando se intenta incrementar el stock con | |
* una cantidad negativa | |
* @throws ArticuloNoExisteException cuando no exista un artículo con el código | |
* indicado | |
* @throws StockNegativoException Cuando las unidades del stock intentan | |
* pasarse a negativos | |
*/ | |
public void incrementaStock(int codigo, int cantidad) throws UnidadesNegativasException, ArticuloNoExisteException, StockNegativoException { | |
try { | |
get(codigo).incrementaStock(cantidad); | |
} catch (IndexOutOfBoundsException e) { | |
throw new ArticuloNoExisteException("El artículo de código " + codigo + " no existe."); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Incrementa el número de unidades de un artículo | |
* | |
* @param unidades cantidad que se va a sumar del stock actual | |
* @throws UnidadesNegativasException Cuando se intenta incrementar el stock con | |
* una cantidad negativa | |
* @throws StockNegativoException Cuando las unidades del stock intentan | |
* pasarse a negativos | |
*/ | |
void incrementaStock(int unidades) throws UnidadesNegativasException, StockNegativoException { | |
if (unidades < 0) | |
throw new UnidadesNegativasException("Las unidades a incrementar al stock han de ser positivas."); | |
setUnidades(getUnidades() + unidades); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void entradaMercancia() { | |
try { | |
mostrar(); | |
almacen.incrementaStock(Teclado.getEntero("Introduce el código del artículo:"), | |
Teclado.getEntero("Introduce las unidades que han entrado")); | |
} catch (UnidadesNegativasException | ArticuloNoExisteException | StockNegativoException e) { | |
System.err.println("No se ha podido incrementar el stock. " + e.getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment