0
I have a customer registration system and I am trying to add products to registered customers. For the registration list I have the following:
public class Cliente
{
public string Nome { get; set; }
public string CPF { get; set; }
public List<ProdutoComprado> Comprou { get; set; }
}
public class ProdutoComprado
{
public string NomeProduto { get; set; }
public string ValorProduto { get; set; }
}
public static List<Cliente> Clientes = new List<Cliente>();
public static List<ProdutoComprado> ProdutosComprados = new List<ProdutoComprado>();
To register the customer, I use the following code:
private void btnCadastrarCliente_Click(object sender, EventArgs e)
{
Cliente cliente = new Cliente();
cliente.Nome = caixaClienteNome.Text;
cliente.CPF = caixaClienteCPF.Text;
listaClientes.Items.Add(cliente.Nome);
Clientes.Add(cliente);
}
To register a product for the customer I use:
private void btnCadastrarProduto_Click(object sender, EventArgs e)
{
Cliente procli = Clientes[listaClientes.SelectedIndex];
procli.Comprou = new List<ProdutoComprado>
{
new ProdutoComprado
{
NomeProduto = caixaProdutoComprado.Text,
ValorProduto = caixaValor.Text
}
};
foreach (var prod in procli.Comprou)
{
ProdutosComprados.Add(prod);
listaProdutosComprados.Items.Add(prod.NomeProduto);
}
}
Note that the code uses as matrix the client selected in the customers listbox, so that the product is added to it.
I also use JSON.net to save the data, and I have as output the following:
[
{
"Nome": "João",
"CPF": "00000000000",
"Comprou": [
{
"NomeProduto": "Carro 4x4",
"ValorProduto": "R$40000"
}
]
}
]
The problem is that this code only adds 1 product. If I select the customer and register another product for it, the product instead of being added simply overwrites the previous one.
Instead of saving in this way, for example:
[
{
"Nome": "João",
"CPF": "00000000000",
"Comprou": [
{
"NomeProduto": "Carro 4x4",
"ValorProduto": "R$40000",
"NomeProduto": "Moto Honda",
"ValorProduto": "R$20000"
}
]
}
]
..it saves so by overwriting the previous:
[
{
"Nome": "João",
"CPF": "00000000000",
"Comprou": [
{
"NomeProduto": "Moto Honda",
"ValorProduto": "R$20000"
}
]
}
]
How to add one product followed by another?
I was reading for a long time, trying to interpret and I couldn’t understand what your code does and what’s wrong with it. Unless I’m making a mistake in something it’s probably missing relevant parts of code or a better explanation.
– Maniero
@Bigown Thanks for your comment! I did not insert another part of this code so there would be too much information, which would hinder the resolution of the problem. This other part consists of registering the customer. I updated the question and ask you to take a look.
– M. Victor
Customer registration really seems to be irrelevant. JSON helps you understand a little but I don’t know what part of your code is dealing with it. For example, I don’t know what it is
caixaProdutoComprado.Text
, not what islistaProdutosComprados
just p/ cite two information that is there as magic. in fact the whole code is difficult to identify what it does. But clearly the part of the code that uses thatcaixaProdutoComprado.Text
is taking only one product, wherever this comes from. The problem is basically there. You have to take others, but just seeing this is not helping.– Maniero
@Box bigown Productproduct.Text is the text of a textbox. productlistComprados is a listbox where the customer’s products should be added. The product name comes from the textbox boxProductComprado at the moment the user presses the product registration button. My problem is I can’t add more than one product. As I said in the question, if there is already a product, when clicking the sign-up button to register a new one, instead of it being added as I exemplified in JSON, it overwrites the previous one.
– M. Victor
@bigown can not specify all products at once because they should be added separately.
– M. Victor