Improve method to return a priority value within a list

1

I'm designing a view that shows the location of some boxes in an inventory, I'll put it in an image here

In each box the first data is the part number, the second is the amount of things you have and the third is the priority in which they have to leave and that method is the one that I want to fix since it is not automatic and I can not find a way to do it that is done by a method, this is the way I am doing it.

 foreach (var p in cajas.OrderBy(c => c.fecha))
            {
                if (p.partNumber == "10-sag")
                {
                    p.fifo = i;
                    i++;
                }

                if (p.partNumber == "10-qt")
                {
                    p.fifo = j;
                    j++;
                }
            }

I want you to do the same thing, keep a count of the order in which the box must be removed but this count is independent according to the partNumber, what I do not want is that if for example a new part is entered and the partNumber is not none of the two that are there in the if have to go to add it.

I leave the object's constructor too

  class Caja
    {
        public string partNumber;
        public int fifo;
        public DateTime fecha;
        public int quantity;
        public int position;
        public int orderColumn;
        public int boxnum;

        public Caja(string partNumber, int fifo, int quantity, int position, DateTime fecha, int orderColumn, int boxnum)
        {
            this.partNumber = partNumber;
            this.fifo = fifo;
            this.quantity = quantity;
            this.position = position;
            this.fecha = fecha;
            this.orderColumn = orderColumn;
            this.boxnum = boxnum;
        }
    }

and with this method return the color of the oldest box is red

class CajaCollection : List<Caja>
    {

        public Color ObtenerColorCaja(Caja caja)
        {
            return ObtenerColorCaja(IndexOf(caja));
        }

        public Color ObtenerColorCaja(int index)
        {
            // Localiza la primera caja ordenando por fecha y con el mismo partNumber
            var primeraCaja = this.OrderBy(c => c.fecha)
                .First(c => c.partNumber == this[index].partNumber);

            // Si el índice coincide es la primera a salir (rojo), si no verde
            return index == IndexOf(primeraCaja) ? Color.Red : Color.Green;
        }

    }
    
asked by R. Nuñez 08.02.2018 в 23:43
source

1 answer

2

You can use the same criteria to obtain the position in the FIFO queue to obtain the color: use a method from the list:

class Caja
{
    public string partNumber;
    public DateTime fecha;
    public int quantity;
    public int position;
    public int orderColumn;
    public int boxnum;

    public Caja(string partNumber, int quantity, int position, DateTime fecha, int orderColumn, int boxnum)
    {
        this.partNumber = partNumber;
        this.quantity = quantity;
        this.position = position;
        this.fecha = fecha;
        this.orderColumn = orderColumn;
        this.boxnum = boxnum;
    }
}

class CajaCollection : List<Caja>
{

    public Color ObtenerColorCaja(Caja caja)
    {
        return ObtenerColorCaja(IndexOf(caja));
    }

    public Color ObtenerColorCaja(int index)
    {
        // Si es el primero de la cola rojo, si no verde
        return ObtenerFifo(index) == 1 ? Color.Red : Color.Green;
    }

    public int ObtenerFifo(Caja caja)
    {
        return ObtenerFifo(IndexOf(caja));
    }

    public int ObtenerFifo(int index)
    {
        // Obtiene el orden en la cola filtrado por partNumber y ordenado por fecha
        return this.Where(c => c.partNumber == this[index].partNumber)
            .OrderBy(c=> c.fecha).ToList().IndexOf(this[index]) + 1;
    }

}

This way you can get the color just by checking if the box is the first one in the queue.

    
answered by 09.02.2018 / 00:13
source