public partial class Form2 : Form { public Form2() { InitializeComponent(); Cuarto = new Almacen(2, 1); Cuarto.AgregarCaja(1, "RED"); Cuarto.AgregarCaja(2, "RED"); Cuarto.AgregarCaja(3, "RED"); Cuarto.AgregarCaja(4, "RED"); Cuarto.RemoverCaja(2); } public Almacen Cuarto; } public class Almacen { public Contenedor[] Contenedores; public Almacen(int Cantidad, int Capacidad) { Contenedores = new Contenedor[Cantidad]; for (int i = 0; i < Cantidad; i++) { Contenedores[i] = new Contenedor(Capacidad); } } public void AgregarCaja(int ID,string Nombre) { if (VerificarDisponibilidad()) { Caja C = new Caja(Nombre, ID); for (int i = 0; i < Contenedores.Length; i++) { if (Contenedores[i].Count < Contenedores[i].Cap) { Contenedores[i].Push(C); return; } } } } public void RemoverCaja(int ID) { if (VerificarDisponibilidad(ID)) { bool stop = false; int ContenedorOrigen = Buscar(ID); if (ContenedorOrigen != -1) { while (!stop) { Caja Box = Contenedores[ContenedorOrigen].Pop(); //SE OBTIENE EL PRIMER ELEMENTO DE LA LISTA if (Box.ID != ID) //SI NO ES EL ELEMENTO QUE BUSCAMOS SE REACOMODA { //SI SI ERA, NO ENTRA LA CONDICION (DESCARTANDO EL ELEMENTO) for (int i = 0; i < Contenedores.Length; i++) { if (i != ContenedorOrigen) { if (Contenedores[i].Count < Contenedores[i].Cap) { Contenedores[i].Push(Box); break; } } } } else { stop = true; } } } } } public int Buscar(int ID) // PARA SABER CUAL ES EL CONTENEDOR DEL CUAL SALDRAN LOS ELEMENTOS (Y NO PONER LOS REMPLAZADOS AHI) { bool busqueda = false; for (int i = 0; i < Contenedores.Length; i++) { busqueda = Contenedores[i].Buscar(ID); if (busqueda == true) { return i; } } return -1; } public bool VerificarDisponibilidad() //PARA INSERTAR UN NUEVO ELEMENTO HAY QUE VER SI HAY ALMENOS 1 ESPACIO EN CUALQUIER CONTENEDOR { int disponiblidad = 0; for (int i = 0; i < Contenedores.Length; i++) { disponiblidad = disponiblidad + (Contenedores[i].Cap - Contenedores[i].Count); } if (disponiblidad > 0) return true; return false; } public bool VerificarDisponibilidad(int ID) // PARA REACOMODAR AHAY QUE VER SI LA CANTIDAD DE ESPACIO ES ALMENOS IGUAL O MAYOR A LA "DISPONIBILIDAD" DE LA CAJA. { int disponiblidad = 0; int Contenedor = -1; for (int i = 0; i < Contenedores.Length; i++) { if (Contenedores[i].Buscar(ID) == false) { disponiblidad = disponiblidad + (Contenedores[i].Cap - Contenedores[i].Count); } else { Contenedor = i; } } if (Contenedor != -1 && Contenedores[Contenedor].Disponibilidad(ID) <= disponiblidad) return true; return false; } } public class Contenedor { public Caja[] Cajas; public int Cap = 0; public int Count = 0; public Contenedor(int Capacidad) { Cajas = new Caja[Capacidad]; Cap = Capacidad; } public void Push( Caja Obj) { if (Count < Cap) { Caja[] newCajas = new Caja[Cap]; newCajas[0] = Obj; int _Count = 1; for (int i = 0; i < Cajas.Length -1; i++) // { // newCajas[_Count] = Cajas[i]; // SE PONE EL NUEVO OBJETO EN LA POSICION 0 Y LOS DEMAS if (Cajas[i] != null) // SE ACOMODAN DESPUES DE EL. _Count++; // } // Cajas = newCajas; Count++; } } public Caja Pop() { Caja Ret = null; if (Count > 0) { Caja[] newCajas = new Caja[Cap]; Ret = Cajas[0]; if(Count > 1) // for (int i = 0; i < Count - 1; i++) // SE OBTIENE EL OBJETO EN LA POSICION 0 (PARA RETORNARLO MAS ADELANTE) { // SE CREA UNA NUEVA LISTA Y SE AGREGAN LOS ELEMENTOS RESTANTES newCajas[i] = Cajas[i+1]; // SE IGUALA LA ANTIGUA LISTA A LA NUEVA (PARA REMPLAZARLA) } // Cajas = newCajas; // AL FINAL EL ELEMENTO "ELIMINADO" SE RETORNA PARA SU REACOMODO Count--; // SI EL ELEMENTO RETORNADO ES EL QUE SE ELIMINA SE DESCARTA DONDE SE LLAMA ESTA FUNCION } return Ret; } public int Disponibilidad(int ID) { for (int i = 0; i < Cajas.Length; i++) { if (Cajas[i].ID == ID) //NOS INDICA CUANTOS ELEMENTOS HAY SOBRE EL ELEMENTO QUE BUSCAMOS { return i; } } return -1; } public bool Buscar(int ID) { for (int i = 0; i < Cap; i++) { if (Cajas[i] != null && Cajas[i].ID == ID) //NOS DICE SI EL ELEMENTO EXISTE O NO EN LA LISTA return true; } return false; } } public class Caja { public string Nombre = ""; public int ID = 0; public Caja(string nombre, int id) { Nombre = nombre; ID = id; } }