-1

How do i calculate the following bolded items below to create a total of all items in a text file? invoices.Add(counter + "," + freshGrocery.Name + "," + freshGrocery.Price + "," + freshGrocery.Weight); invoices.Add(counter + "," + grocery.Name + "," + price + "," + grocery.Quantity);

full code below. Not sure how to add int from different strings

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

    namespace Groceries3
    {
class Program
{
    static void Main(string[] args)
    {
        string[] groceries = File.ReadAllLines("Groceries.txt");
        List<string> invoices = new List<string>();

        int counter = 0;
        foreach (var grocery2 in groceries)
        {
            counter++;
            var list = grocery2.Split(',');
            if (list[0].Equals("fresh"))
            {
                FreshGrocery freshGrocery = new FreshGrocery();
                freshGrocery.Name = list[1];
                freshGrocery.Price = double.Parse(list[2]);
                freshGrocery.Weight = double.Parse(list[3].Replace(";", ""));

                invoices.Add(counter + "," + freshGrocery.Name + "," + freshGrocery.Price + "," + freshGrocery.Weight);
            }
            else if (list[0].Equals("regular"))
            {
                Grocery grocery = new Grocery();
                grocery.Name = list[1];
                grocery.Price = double.Parse(list[2]);
                grocery.Quantity = int.Parse(list[3].Replace(";", ""));

                double price = grocery.Calculate();
                invoices.Add(counter + "," + grocery.Name + "," + price + "," + grocery.Quantity);
            }

        }
        File.WriteAllLines("Invoice.txt", invoices.ToArray());
        {
            File.AppendAllText("Invoice.txt", string.Format("{0}{1}", "Groceries for you" + " " + DateTime.Now, Environment.NewLine));
            File.AppendAllText("Invoice.txt", string.Format("{0}{1}", "Total of all groceries = ", Environment.NewLine));
        }
    }

    abstract class GroceryItem
    {
        private string name;
        private double price = 0;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public double Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }
        public abstract double Calculate();
    }

    class FreshGrocery : GroceryItem
    {
        private double weight = 0;
        public double Weight
        {
            get
            {
                return weight;
            }
            set
            {
                weight = value;
            }
        }
        public override double Calculate()
        {
            return this.Price * this.weight;
        }
    }

    class Grocery : GroceryItem
    {
        private int quantity = 0;
        private double gst = 10;

        public int Quantity
        {
            get
            {
                return quantity;
            }
            set
            {
                quantity = value;
            }
        }
        public override double Calculate()
        {
            double calculatedPrice = this.Price * this.Quantity;
            if (calculatedPrice < 0)
            {
                calculatedPrice += calculatedPrice * (gst / 100);
            }
            return calculatedPrice;
        }
    }
    class ShoppingCart
    {
        private List<GroceryItem> orders;

        public List<GroceryItem> Orders
        {
            get
            {
                return orders;
            }
            set
            {
                orders = value;
            }
        }
        public double Calculate()
        {
            double price = 0;
            if (this.Orders != null)
            {
                foreach (GroceryItem order in this.Orders)
                {
                    price += order.Calculate();
                }
            }
            return price;
        }
    }
}

}

McKimmie79
  • 1
  • 1
  • 1
  • Please also post content of Groceries.txt that can be used to reproduce your problem. – Niyoko Oct 29 '16 at 04:26
  • 1
    smelling homework – Benj Oct 29 '16 at 04:26
  • 1
    You have to calculate them before you write them – Jim Oct 29 '16 at 05:41
  • The best way to add the values is to do so with the variables that are of type `double` and to not try to add the values from the strings at all. But if you insist on using the string values, you'll need to extract the formatted text of the values of interest (see `string.Split()` or `Regex` class), convert them back to numeric values (parsing `double` works just like parsing `int`) and add those. See the marked duplicate for advice on the last part. – Peter Duniho Oct 29 '16 at 07:05
  • If that doesn't suffice, you need to reduce your question to a good [mcve] that shows clearly what you've tried, describe what research you've already done, and explain what _specifically_ you are having trouble with. – Peter Duniho Oct 29 '16 at 07:05

1 Answers1

0

If all you are looking to do is convert to string, use string.Format(). string.Format() takes a string argument that specified how your string should appear where the brackets are placeholders for your subsequent arguments. Then calls .ToString() on each of your arguments.

invoices.Add(string.Format("{0},{1},{2},{3}",
   counter, freshGrocery.Name, freshGrocery.Price, freshGrocery.Weight));

The above is the same as the one below, but easier to read:

invoices.Add(counter + "," + freshGrocery.Name.ToString() + "," +
   freshGrocery.Price.ToString() + "," + freshGrocery.Weight.ToString());
Ben Richards
  • 3,437
  • 1
  • 14
  • 18