我如何计算然后写入文本文件

时间:2016-10-29 04:23:50

标签: c#

如何计算以下加粗项目以创建文本文件中的所有项目? invoices.Add(counter +“,”+ freshGrocery.Name +“,”+ freshGrocery.Price +“,”+ freshGrocery.Weight); invoices.Add(counter +“,”+ grocery.Name +“,”+ 价格 +“,”+ grocery.Quantity);

下面的完整代码。不确定如何从不同的字符串中添加int

    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;
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

如果您要做的只是转换为字符串,请使用string.Format()。 string.Format()接受一个字符串参数,该参数指定字符串应如何显示在括号为后续参数的占位符的位置。然后在每个参数上调用.ToString()。

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

以上与下面的相同,但更容易理解:

invoices.Add(counter + "," + freshGrocery.Name.ToString() + "," +
   freshGrocery.Price.ToString() + "," + freshGrocery.Weight.ToString());