sok
sok

Reputation: 632

How to read and write a file after receiving an array with repeated items

I receive a list of products(listS) and a list of its quantity(listI), what I want is to remove some items from my database, depending on the quantity that I receive on the listI. It is working like a charm, as long as, I don't receive repeated products on the listS, and that's what I'm trying to solve.

    public void remove(string[] listS, int[] listI)
    {
        int check = 0;
        int quantidadeDB;
        string ingredientDB;
        string ingredient;
        int quantity;
        string newLine;
        string[] linesSplit;
        string[] lines = System.IO.File.ReadAllLines("c:\\z.txt");
        System.IO.StreamWriter fileTemp = new System.IO.StreamWriter("c:\\z2.txt");
        for(int i = 0; i < lines.Length;i++)
        {
            linesSplit = lines[i].Split(' ');
            ingredientDB = linesSplit[0];
            quantityeDB = int.Parse(linesSplit[1]);
            check = 0;
            for(int j=0;j<listS.Length;j++)
            {
                ingredient = listS[j];
                quantity = listI[j];
                if (ingredientDB.ToLower().Equals(ingredient.ToLower()))
                {
                    newLine = ingredientDB + " " + (quantityDB - quantity);
                    fileTemp.WriteLine(newLine);
                    check++;
                }
            }
            if(check.Equals(0))
            fileB.WriteLine(ingredientDB + " " + quantityDB);
        }
        fileTemp.Close();
        string text = System.IO.File.ReadAllText("c:\\z2.txt");
        System.IO.StreamWriter fileL = new System.IO.StreamWriter("c:\\z.txt");
        fileL.Write(text);
        fileL.Close();
    }

Example: If my database was this one: Fanta 100 Pepsi 2 Coca-Cola 3 IcedTea 23 Ketchup 23

After receiving this arrays:

listS= {"Pepsi", "Pepsi", "Ketchup", "Pepsi", "Ketchup"}; 
listI = {2, 3, 1, 3, 2};

This is how it should the output:

Fanta 100
Pepsi -6
Coca-Cola 3
IcedTea
Ketchup 20

Instead of:

Fanta 100
Pepsi 0
Pepsi -1
Pepsi -1
Coca-Cola 3
IcedTea 23
Ketchup 22
Ketchup 21

Upvotes: 0

Views: 91

Answers (2)

Daniel Johnson
Daniel Johnson

Reputation: 701

It would be much better implemented if you had a product struct that contained the Name and the Quantity.

struct Product
{
    public string Name;
    public int Quantity;
}

There's a way to fix your problem with a nested for loop and converting your arrays into a List.

List<Product> products = new List<Product>();
for(int i = 0; i < listS.Length; i++)
{
    bool duplicate = false;
    foreach(Product p in products)
    {
        if(listS[i].ToLower().Equals(p.Name.ToLower()))
        {
            p.Quantity += listI[i];
            duplicate = true;
            break;
        }
    }
    if(!duplicate)
    {
        Product p;
        p.Name = listS[i];
        p.Quantity = listI[i];
        products.Add(a)
    }
}

Then products will have no duplicates

Edit: If you really want to stay with your code for thoughts that converting everything to a struct will result in a loss in performance, the fix implemented with your code will actually be very inefficient.

The entire method can be implemented as such tho.

public void remove(string[] listS, int[] listI)
{
    List<Product> products = new List<Product>();
    for(int i = 0; i < listS.Length; i++)
    {
        bool duplicate = false;
        foreach(Product p in products)
        {
            if(listS[i].ToLower().Equals(p.Name.ToLower()))
            {
                p.Quantity += listI[i];
                duplicate = true;
                break;
            }
        }
        if(!duplicate)
        {
            Product p;
            p.Name = listS[i];
            p.Quantity = listI[i];
            products.Add(a)
        }
    }
    string[] lines = System.IO.File.ReadAllLines("c:\\z.txt");
    for(int i = 0; i < lines.Length; i++)
    {
        string[] linesSplit = lines[i].Split(' ');
        foreach(Product p in products)
        {
            if (linesSplit[0].ToLower().Equals(p.Name.ToLower()))
            {
                lines[i] = linesSplit[0] + " " + (int.Parse(linesSplit[1]) - p.Quantity);
                products.Remove(p)
                break;
            }
        }
    }
    System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\z.txt");
    file.Write(lines);
    file.Close();
}

Upvotes: 1

Shaharyar
Shaharyar

Reputation: 12439

Change this line

newLine = ingredientDB + " " + (quantityDB - quantity);

to this:

newLine = ingredientDB + " " + (Int32.Parse(quantityDB) - Int32.Parse(quantity)).ToString();

You can not implicitly convert string to int. So had some unusual results.

Upvotes: 0

Related Questions