Help with a list type object or modify code to make it work

0

I am working on a project in which I have to show boxes that enter an inventory, what happens is that of these boxes I have to show some of their data that are what type of pieces, according to their date of entry to the inventory show a number to indicate that it must first go out and put it in red, while the other boxes are green, and what happened to me was to make an object that keeps both the form of I did not order it, but that is why I want to do the fifo thing and change the colors first.

For example if 4 different pieces are handled, a, b, c and d, then all the boxes that have pieces of a must show the number of pieces they contain, and the order in which they must be removed according to the date, in addition to the that it must first be painted red, and so with each of the types b, c and d

What I have is the following but it does not work for me because I feel that the object is wrong or I'm not doing something right:

This is the case builder

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

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

        }

    }

And this is the part of trying to make it work

  public partial class Form1 : Form
    {
        int order = 0;
        List<cajas> caja = new List<cajas>();
        string cad = "";
        bool cachado = false;
        int comp = 0;
        DateTime aux = DateTime.MaxValue;
        string auxpart = "";
        int c = -10;
        int a = 0;
        int b = 100;
        string color2 = "Green";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            startTimer();
        }

        private void button17_Click(object sender, EventArgs e)
        {
            order++;
            a++;
            b++;
            caja.Add(new cajas("10-qt", 120, 110, 2,DateTime.Now,order,"Green",b));
            caja.Add( new cajas("10-sag",10,110,1,DateTime.Now,order,"Green",a));

        }
        private void startTimer()
        {
            timer1.Start();
            timer1.Interval = 500;
        }

        public void comparador(DateTime d1, DateTime d2, string partNum, string partNum2,int boxnum)
        {

            foreach (cajas acaja in caja)
            {

                if (acaja.boxnum == boxnum)
                {
                    if (partNum == partNum2)
                    {

                        DateTime.Compare(d1, d2);
                        if (c < 0)
                        {
                            acaja.color = "Red";
                        }
                        else if (c == 0)
                        {
                            acaja.color = "Green";
                        }
                        else
                        {
                            acaja.color = "Green";
                        }

                    }
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = aux.ToString();
            label2.Text = auxpart;

            foreach (cajas acaja in caja)
            {
                switch (acaja.position)
                {
                    case 1:
                        switch (acaja.orderColumn)
                        {
                            case 1:
                                button1.Text = acaja.partNumber + "\n" + acaja.quantity + "\n" + acaja.fecha;
                                comparador(acaja.fecha, aux, acaja.partNumber, auxpart,acaja.boxnum);
                                button1.BackColor = Color.FromName(acaja.color);
                                aux = acaja.fecha;
                                auxpart = acaja.partNumber;

                                break;
                            case 2:
                                button5.Text = acaja.partNumber + "\n" + acaja.quantity + "\n" + acaja.fecha;
                                comparador(acaja.fecha, aux, acaja.partNumber, auxpart,acaja.boxnum);
                                button5.BackColor = Color.FromName(acaja.color);
                                aux = acaja.fecha;

                                auxpart = acaja.partNumber;
                                break;
                            case 3:
                                button9.Text = acaja.partNumber + "\n" + acaja.quantity + "\n" + acaja.fecha;
                                comparador(acaja.fecha, aux, acaja.partNumber, auxpart, acaja.boxnum);
                                button9.BackColor = Color.FromName(acaja.color);
                                aux = acaja.fecha;
                                auxpart = acaja.partNumber;
                                break;
                            case 4:
                                button13.Text = acaja.partNumber + "\n" + acaja.quantity + "\n" + acaja.fecha;
                                comparador(acaja.fecha, aux, acaja.partNumber, auxpart, acaja.boxnum);
                                button13.BackColor = Color.FromName(acaja.color);
                                aux = acaja.fecha;
                                auxpart = acaja.partNumber;
                                break;
                        }
                        break;

                    case 2:
                        if (button2.Text == "")
                        {
                            button2.Text = acaja.fifo.ToString();
                        }
                        break;
                }
            }

        }
    }

The comparator does not work for me since after comparing the first and all the red paints

    
asked by R. Nuñez 06.02.2018 в 23:51
source

1 answer

1

To begin with, I would remove the field color from the class that defines the box, since the color of the box does not depend on the properties of the box, but on the position it occupies in a collection of boxes.

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;
    }
}

Then you could create a class CajaCollection that is a list of boxes and that has a method to obtain the color of a particular box:

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;
    }
}

So you would not have to define the list of boxes as an object CajaCollection :

CajaCollection caja = new CajaCollection();

and to obtain the color of a specific box, you could call any of the overloads of the ObtenerColorCaja method:

button5.BackColor = caja.ObtenerColorCaja(acaja);
    
answered by 07.02.2018 / 10:10
source