viewing paste Unknown #14852 | C#

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
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;
        }
    }
Viewed 629 times, submitted by Guest.