Samayoa
Samayoa

Reputation: 185

Wrong elements of Arrays being populated

I need your help,

I'm adding some values to 4 single arrays , when a user inputs the same product ID more than 1 time , it will only modify one of the arrays (cantidad_prod) on the exact position where the ID was found.

I'm running the "for" cycle twice for ID 1 , the first time it will ask me all the details and the second time it will only ask me for the new quantity (if ID 1 was on position 0, it will modify the quantity array on position 0 too).

Since I'm only updating position 0 , there should be only 1 element on each arrays but the screen is displaying 2 elements. What am I doing wrong? Below is my code and thanks in advance:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exerc_6
{
    class Program
    {
        static void Main(string[] args)
        {
            int total_prod, i, ID,TextIndex,total_elements;
            double total_a_pagar=0;
            //Input 2 for testing
            Console.WriteLine("Products being purchased?");
            total_prod = int.Parse(Console.ReadLine());
            Console.Clear();

            int[] ID_Prod = new int[total_prod];
            string[] Nombre_Producto = new string[total_prod];
            int[] cantidad_prod = new int[total_prod];
            double[] precio_uni = new double[total_prod];


            for (i = 0; i < total_prod; i++)
            {

                Console.WriteLine("Add the product ID");
                ID = int.Parse(Console.ReadLine());
                TextIndex = Array.FindIndex(ID_Prod, m => m == ID);
                Console.Clear();

                if (TextIndex>-1)
                {
                    Console.WriteLine("Amount of Products for ID #"+ID);
                    cantidad_prod[TextIndex] = cantidad_prod[TextIndex] + int.Parse(Console.ReadLine());
                    Console.Clear();
                }

                else

                {
                    ID_Prod[i] = ID;
                    Console.WriteLine("ProductName for  ID #"+ID);
                    Nombre_Producto[i] = Console.ReadLine();
                    Console.WriteLine("Amount of Products for ID ID #" + ID);
                    cantidad_prod[i] = cantidad_prod[i] + int.Parse(Console.ReadLine());
                    Console.WriteLine("Unit Price for ID #" + ID);
                    precio_uni[i] = cantidad_prod[i] + Double.Parse(Console.ReadLine());
                    Console.Clear();
                }

            }

            //This must be 1 if we enter #1 twice
            total_elements = ID_Prod.Length;
            Console.WriteLine(total_elements);

            Console.ReadKey();
        } } }

Upvotes: 0

Views: 39

Answers (1)

Daniel Loudon
Daniel Loudon

Reputation: 799

Your problem is you are creating a fixed length array of Size 2, and then only modifying the first index twice. An array can contain an empty index and when you call the .Count on an array of size, despite one index being empty, it will return 2.

Perhaps look at creating an object and storing these values as properties or look at using lists and keeping the existing for loop iterating over total_prod and appending any new IDs and values to the 4 lists (were arrays) using the List.Add(...) method

List<int> ID_Prod = new List<int>();
List<string> Nombre_Producto = new List<string>;
List<int> cantidad_prod = new  List<int>;
List<double> precio_uni = new List<double>;

For(int i = 0; i < total_prod; i++)
{
    //some code
    int ID = int.Parse(Console.ReadLine());
    If(!ID_Prod.Contains(ID))
    {
        ID_Prod.Add(ID);
        //append other lists
    }
        //update lists
    }
 }

When creating your own object:

Class Product {
    public int ID {get; set;}
    public string Name {get; set;}
    public int Quantity {get; set;}
    public double Price {get; set;} 

    public Product(int ID, string Name, int Quantity, double Price) 
    {
        this.ID = ID;
        this.Name = Name;
        this.Quantity = Quantity;
        this.Price = Price;
     }
 }

Then in your code:

 List<Product> products = new List<Product>();
 //... get values from user
Product p = new Product(ID, Name, Quantity, Price);
 products.Add(p);

And finding the product to edit:

 //user enters ID
 int ID = int.Parse(Console.ReadLine());
  If(products.Any(x=>x.ID == ID))
  {
       Product p = products.Where(x=>x.ID==ID).FirstOrDefault();
  //modify p and this will update in list as it is a reference type
  }

Upvotes: 1

Related Questions