Let's say I have a two-dimensional array like this. This represents an excel template or whatever you want it to be.
The intention of the data table is that it resembles the excel template where I have to insert the data.
The main problem is to find the place in the two-dimensional array where I have to insert each data.
I have written the program in C# for ease, but I need a pseudocode , an algorithm to do it, an example in some other similar language...
Although the ideal would be the pseudocode or algorithm so that anyone could adapt it to X language in case that it was necessary.
(EDIT) This array represents an excel that I have to fill, obtaining SQL values.
+—————————————+———————————+
| | STOCK |
+—————————————+———————————+
| Adidas | |
+—————————————+———————————+
| Camisetas | 52 | <---- los datos de STOCK se obtienen de SQL
+—————————————+———————————+
| Pantalones | 37 | <---- Lo normal es que vengan vacíos.
+—————————————+———————————+
| Zapatillas | 15 |
+—————————————+———————————+
| Puma | |
+—————————————+———————————+
| Camisetas | 55 |
+—————————————+———————————+
| Pantalones | 73 |
+—————————————+———————————+
|Ropa interior| 21 |
+—————————————+———————————+
| Nike | |
+—————————————+———————————+
| Zapatillas | 23 |
+—————————————+———————————+
I do a query to SQL that returns a result type
+—————————+————————————+———————+
| Marca | Producto | STOCK |
+—————————+————————————+———————+
| Adidas | Camiseta | 52 |
+—————————+————————————+———————+
| Adidas | Zapatillas | 15 |
+—————————+————————————+———————+
| Puma | Pantalones | 57 |
+—————————+————————————+———————+
| ... | ... | ... |
+—————————+————————————+———————+
I put these types of results in the following way.
Class Producto(){
public String marca;
public String producto;
public int stock;
/*pongamos que a parte, aquí hay getters y setters básicos*/
}
Class Principal{
public static void rellenarLista(){
/*Establecemos conexion con BBDD lanzamos la consulta y tal*/
List<Producto> productos = new List<Producto>();
While(datareader.Read()){
Producto p = new Producto();
p.marca = dr.getString(0);
p.producto = dr.getString(1);
p.stock = dr.getInt(2);
productos.Add(p);
}
}
}
Now the thing is,
List<Producto> productos
I have to create a search algorithm with the following requirements: - That it locates each Stock element in its respective place in the two-dimensional array.
Be self-sufficient:
If one day I add a mark, it has to be able to find it and insert the data automatically.
That you insert the data corresponding to your brand, since in the row the products have the same name
If there was a Puma Sneakers, I would put it in Nike Sneakers, because in the two-dimensional structure, there is no record that Puma sells sneakers.
The problems I have had are:
1- Data positioning. 2- When I can locate the data in their respective places or it takes a long time or I can't locate them correctly. 3- Lack of adaptability, if you add a new brand it stops working, the algorithm is not self-sufficient so it will not take that brand into account in the future.
This is what I have tried so far.
public static void busqueda(){ //se llama busqueda pero perfectamente podría llamarse inserción
String[,] matriz = new Matriz[num1, num2];
int fila;
Boolean marcaEncontrada;
for(int lista=0;lista<productos.Count;lista++){
marcaEncontrada=false;
for(int i = 0; i<matriz.getLengt(0);i++){ //X
for(int j = 0; i<getLength(1);j++){ //Y
if(matriz[i,j].Contains(productos[lista].getMarca())){
marcaEncontrada=true;
}
if (excel[i, j].Contains(productos[lista].getProducto() && marcaEncontrada)){
fila=i;
}
}
}
matriz[fila,1] = productos.getStock();
}
}
I think the code is a bit "sloppy" but I think it is understood for what I want to do.
As I have the code, it will insert the code, it always inserts me at the end, that is, if they are sneakers, regardless of the brand, it always inserts them at the end, in the Nike part , if they are pants, I always inserts them into the Puma part .
-I use the list to be able to make the comparisons and insert all the data using a single loop.
In the end this is what has worked for me, considering the code written above.
//Method compareMark
What
"IF IMPORTANTE"
it does is compare if the position in the matrix is one of the brands, if it is, it enters the compareBrands method that will return true or false if the brand corresponds to the product whose stock must be entered.In this way, if in the future it finds a brand that does not correspond to the product to be introduced, it will not be introduced, at the same time that if more products of different brands are added, it will take them into account since we obtain the brand of the product lines before when we give value to
marcas
.I leave here the steps that I still hope you understand:
With this I get that:
I would create a dictionary and use it as an index
and it would be the key: brand + product
and in value guard data of the row where it was found: {RowIndex, CellValue}
also adds another benefit, it will be faster
something like that
Searching:
//we load the new ones at the end of the excel sheet