Skip to content
Snippets Groups Projects
Commit 05348526 authored by carmuno's avatar carmuno :tennis:
Browse files

update 2021

correciones y refactoring
parent d0bbd426
Branches main
No related tags found
No related merge requests found
......@@ -2,10 +2,10 @@ package EjemploDeExamenParaPracticar;
import java.util.ArrayList;
/*
/**
*
* @author Carlos Noé Muñoz
* @version 1.1
* @version 1.2 {@DATE}
*
*/
......@@ -13,303 +13,216 @@ public class ColaDosVentanillas <T>{
private ArrayList <T> ventanilla1;
private ArrayList <T> ventanilla2;
private boolean open_ventanilla1;
private boolean open_ventanilla2;
private boolean openVentanilla1;
private boolean openVentanilla2;
private int atendido;
public ColaDosVentanillas() {
/**
* Ojo, en la revision, @version 1.2 12 enero 2020, como indica,
* "inicialización a partir de una lista de elementos: se inicia con
* sus dos venta- nillas abiertas para atencio ́n y coloca en orden cada
* elemento de dicha lista alternativamente en cada una de las dos ventanillas.", he añadido con @javrami
* una cola de inicializacion qu rellena ambas ventanillas.
*/
public ColaDosVentanillas(ArrayList<T> cola) {
ventanilla1 = new ArrayList<T>();
ventanilla2 = new ArrayList<T>();
open_ventanilla1 = true;
open_ventanilla2=true;
openVentanilla1 = true;
openVentanilla2 =true;
for(int i = 0; i < cola.size(); i++) {
if(i%2==0) {
ventanilla1.add(cola.get(i));
} else {
ventanilla2.add(cola.get(i));
}
}
}
/*
/**
* Introducir un elemento en cola: requiere que al menos una de las dos ventanillas est ́e abierta para atencio ́n.
* Si ambas ventanillas esta ́n abiertas elige la ventanilla con menor cantidad de elementos pendientes y coloca el
* nuevo elemento al final. Si ambas ventanillas tienen la misma cantidad de elementos pendientes se elige la ventanilla uno.
*
* @param element se trata del elemnto de tipo T (generico a introducir en la cola)
* @throws IllegalArgumentException cuando {@code: open_ventanilla1 ==false && open_ventanilla2==false }
* o tambien si {@code: (ventanilla1.contains(element) || ventanilla2.contains(element)}
* @throws IllegalArgumentException si {@code: (ventanilla1.contains(element) || ventanilla2.contains(element)}
*/
public void add(T element) {
if(open_ventanilla1 ==false && open_ventanilla2==false){
public void add(T element) throws IllegalArgumentException{
if(openVentanilla1 ==false && openVentanilla2 ==false){
throw new IllegalArgumentException("ambas ventanillas estan cerradas");
}
if(ventanilla1.contains(element) || ventanilla2.contains(element)) {
if(ventanilla1.contains(element) || ventanilla2.contains(element)) { //ojo estamos comprobando el elemento por referencia...
throw new IllegalArgumentException("el elemento ya esta contenido");
}
if(open_ventanilla1 ==true && open_ventanilla2==true) {
//si mabas estan abiertas, colocamos en la que haya menos elementos y lo colocamos al final.
if(ventanilla1.size()<ventanilla2.size()) {
ventanilla1.add(ventanilla1.size()+1,element); //se añade al final.
if(openVentanilla1 ==true && openVentanilla2 ==true) {
//si ambas estan abiertas, colocamos en la que haya menos elementos y lo colocamos al final.
if(ventanilla2.size()<ventanilla1.size()) {
ventanilla2.add(element); //se añade al final.
}
else {
ventanilla2.add(ventanilla1.size()+1,element); //se añade al final.
ventanilla1.add(element); //se añade al final.
}
}
if(open_ventanilla1 ==false && open_ventanilla2==true){ //si las dos no estan abiertas o
ventanilla2.add(ventanilla1.size()+1,element); //se añade al final.
if(openVentanilla1 ==false && openVentanilla2 ==true){ //si las dos no estan abiertas o
ventanilla2.add(element); //se añade al final.
}
if(open_ventanilla1 ==true && open_ventanilla2==false){ //si las dos no estan abiertas o
ventanilla1.add(ventanilla1.size()+1,element); //se añade al final.
}
}
public void openventanilla1() {
assert(open_ventanilla1==true);
if(open_ventanilla1==false && open_ventanilla2==false) {
open_ventanilla1 = true;
if(openVentanilla1 ==true && openVentanilla2 ==false){ //si las dos no estan abiertas o
ventanilla1.add(element); //se añade al final.
}
else {
open_ventanilla1=true;
}
/**
* Abrir Ventanilla.
*/
public void abrirVentanilla() throws IllegalStateException {
if(openVentanilla1 && openVentanilla2) {
throw new IllegalStateException("No se puede abrir ninguna ventanilla porque ya estan abiertas.");
}
public void openventanilla2() {
assert(open_ventanilla2==true);
if(open_ventanilla1==false && open_ventanilla2==false) {
open_ventanilla1 = true;
if(!openVentanilla1 && !openVentanilla2) {
openVentanilla1 = true;
} else if(!openVentanilla1 && openVentanilla2) {
openVentanilla1 = true;
} else {
openVentanilla2 = true;
}
open_ventanilla2 = true;
}
/*
*
/**
* @throws IllegalArgumentException si ocurre {@code open_ventanilla1 ==false && open_ventanilla2==false}
* o cuando estemos en esta situacion; {@code open_ventanilla1 ==true && open_ventanilla2==true},
* que {@code ventanilla1.isEmpty()} o {@code ventanilla2.isEmpty()}.
*
*
*/
public void atender() {
if(open_ventanilla1 ==false && open_ventanilla2==false) {
public void atender() throws IllegalArgumentException {
if(!openVentanilla1 && openVentanilla2) {
throw new IllegalArgumentException("si las dos ventanillas estan cerradas");
}
if(open_ventanilla1 ==true && open_ventanilla2==true) {
if(openVentanilla1 && openVentanilla2) {
if(ventanilla1.isEmpty()) {
throw new IllegalArgumentException("ventanilla1 vacia");
}
if(ventanilla2.isEmpty()) {
throw new IllegalArgumentException("ventanilla2 vacia");
}
//cuando atienede a la ventanilla 1, la siguiente sera la 2, y la siguinete la 1, y logramos esta alternancia
//cuando atienede a la ventanilla 1, la siguiente sera la 2, y la siguiente la 1, y logramos esta alternancia
//mediante el flag de estado qeu hemos creado "atendido".
//esta idea es de fso cuando haciamos sincronización de procesos (no eran semaforos aun).
if(atendido==1) {
ventanilla1.remove(0);
atendido=0;
}
if(atendido==0) {
ventanilla1.remove(0);
atendido=1;
}
}
if(open_ventanilla1 ==false && open_ventanilla2==true) {
if(!openVentanilla1 && openVentanilla2) {
ventanilla1.remove(0); //solo atiende la ventanilla 1.
}
if(open_ventanilla1 ==true && open_ventanilla2==false) {
if(openVentanilla1 && !openVentanilla2) {
ventanilla1.remove(0); //solo atiende la ventanilla 2.
}
}
public int sin_atender1() {
public int sinAtender1() {
return ventanilla1.size();
}
public int sin_atender2() {
public int sinAtender2() {
return ventanilla2.size();
}
public int sin_atendertotales() {
public int sinAtendertotales() {
return ventanilla1.size() + ventanilla2.size();
}
public void closeventanilla1() {
assert(open_ventanilla1== true);
open_ventanilla1 = false;
public void closeventanilla1() throws IllegalAccessException {
if(openVentanilla1) throw new IllegalAccessException();
openVentanilla1 = false;
}
public void closeventanilla2() {
assert(open_ventanilla2== true);
open_ventanilla2 = false;
public void closeventanilla2() throws IllegalAccessException {
if(openVentanilla2) throw new IllegalAccessException();
openVentanilla2 = false;
}
public void cerrarventanilla() {
/**
* cerrar una ventanilla: elige la ventanilla con menor cantidad de elementos pen-
* dientes de atender y la cierra, transfiriendo todos los elementos pendientes de
* atencio ́n en dicha ventanilla a la otra. Si ambas ventanillas tienen la misma cantidad
* de elementos elegir ́a la ventanilla 1.
*/
public void cerrarventanilla() {
if(ventanilla1.size()<ventanilla2.size()) {
ventanilla2.addAll(ventanilla1);
ventanilla1.clear();
}
if(ventanilla1.size()>ventanilla2.size()) {
ventanilla1.addAll(ventanilla1);
ventanilla1.addAll(ventanilla2);
ventanilla2.clear();
}
if(ventanilla1.size()==ventanilla2.size()) {
ventanilla2.addAll(ventanilla1);
ventanilla1.clear();
}
}
/*
* cierra todas las ventanillas
/**
* Cierra todas las ventanillas
*
* @thows IllegalArgumentException si ocurre que {@code pen_ventanilla1== false} o
* {@code open_ventanilla2== false} o tambien puede ocurrir que {@code ventanilla1.isEmpty() && ventanilla2.isEmpty()}
*
*/
public void closeall() {
if(open_ventanilla1== false) {
public void closeall() throws IllegalArgumentException{
if(!openVentanilla1 || !openVentanilla2) {
throw new IllegalArgumentException("ventanilla1 ya esta cerrada");
}
if(open_ventanilla2== false){
throw new IllegalArgumentException("ventanilla2 ya esta cerrada");
}
if(ventanilla1.isEmpty() && ventanilla2.isEmpty()) {
ventanilla1.clear();
ventanilla2.clear();
}
}
/*
/**
*
* @throws IllegalArgumentException con {@code !open_ventanilla1== true && !open_ventanilla2== true}
* o tambien puede ocurrir si las ventanillas ya estan equilibradas {@code ventanilla1.size() == ventanilla2.size}
*
* balancear: requiere que ambas ventanillas est ́en abiertas, transfiere elementos de una ventanilla a la otra de forma
* que quede la misma cantidad de elementos en ambas (o a lo sumo diferencia de un elemento).
*
*/
public void balancear() {
if(!open_ventanilla1== true && !open_ventanilla2== true) {
public void balancear() throws IllegalArgumentException {
if(!openVentanilla1 && !openVentanilla2) {
throw new IllegalArgumentException("ventanilla 1 y ventanilla 2 no esta abiertas");
}//tienen que estar ambas abiertas.
if(ventanilla1.size() == ventanilla2.size()) {
throw new IllegalArgumentException("ya estan equilibradas");
}//ya estarian equilibrados
//iguala el numero de elementos en las dos ventanillas.
if(ventanilla1.size() > ventanilla2.size()) {
int elementos_a_trasferir = ventanilla1.size() - ventanilla2.size();
for(int i = 0; i< elementos_a_trasferir ;i++) {
T element = ventanilla1.get(ventanilla1.size()-i);
ventanilla2.add(element);
}
}
if(ventanilla1.size() < ventanilla2.size()) {
int elementos_a_trasferir = ventanilla2.size() - ventanilla1.size();
for(int i = 0; i< elementos_a_trasferir ;i++) {
T element = ventanilla2.get(ventanilla2.size() - i);
ventanilla2.add(element);
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment