如何从文本文件读取数据,然后编写具有新计算结果的新文本文件并使用子类方法进行计算?

时间:2019-04-15 13:27:00

标签: c#

首先,我想知道如何对具有以下数据的文本文件执行计算:
类型产品价格数量
普通面包2.00,2
普通牛奶2.00,3

我想执行一些计算,例如获取每一行,每一行的总成本,然后将结果写入新文件。

请注意,我需要将其格式设置为以下结构:杂货项目是基类,而PurchasedItem和FreshItem都是子类,因此我需要使用每个子类上的方法进行计算。 注意我尝试使用测试值而不是读取文本文件值,并且似乎无法从FreshItem子类检索方法,但是它可以与PurchasedItem类上的方法一起使用(我想这个问题将在以后,我想确保即时消息使用的是首先兼容的writeAllLines方法,可以使用子类方法) 下面的示例缺少构造函数,只是一个示例布局

class GroceryItem 


{
public void writeFile()
        {
            List<string> lines = new List<string>(File.ReadAllLines("groceries.txt"));

            //regular item cost calculator
            PurchasedItem purchasedItem = new PurchasedItem(50,1); //testing to return value to print into the write out file
            double cost = purchasedItem.regularCost();
            lines.Add(Convert.ToString(cost));

            //fresh item cost calculation here
            double purchaseCost = purchasedItem.freshItemCost(10,1); //<<<<<<============Trying to add the method from the sub-subclass freshItemCost()

            //total items calculation here

            //total cost calculation here

            // Reads all lines from the file and puts them into the list.
            lines.Add("Grocery for you"); // You can add new lines now.
            lines.Add(DateTime.Now.ToString());
            File.WriteAllLines("c:\\MicrosoftVisual\\invoice.txt", lines);
        }
public class PurchasedItem : GroceryItem //inheriting properties from class GroceryItem
        {
                public double regularCost() //method from cost of regular
            {
                return this.price * this.quantity * 1.1; //workout out cost  for purchases including GST
            }
        }
public class FreshItem : PurchasedItem    //Inheriting properties from class Purchased Item
        {
public double freshItemCost(double w, double p) //method to get cost of fresh item
                {
                    return this.weight * this.price; //workout cost of the fresh item excluding GST
                }

2 个答案:

答案 0 :(得分:0)

尝试以下代码开始使用:

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


namespace ConsoleApplication108
{
    class Program
    {
        const string FILENAME = "groceries.txt";
        static void Main(string[] args)
        {
            GroceryItem grocery = new GroceryItem();
            grocery.ReadFile(FILENAME);

        }
    }
    class GroceryItem
    {
        public static List<GroceryItem> items = new List<GroceryItem>();

        public string _type { get; set; }
        public string name { get; set; }

        public decimal price { get; set; }
        public int quantity { get; set; }
        public decimal weight { get; set; }

        public void ReadFile(string filename)
        {
            string line = "";
            StreamReader reader = new StreamReader(filename);
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();

                if(line.Length > 0)
                {
                    string[] splitArray = line.Split(new char[] { ',' });
                    //regular item cost calculator

                    _type = splitArray[0];
                    name = splitArray[1];
                    PurchasedItem purchasedItem = new PurchasedItem(splitArray); //testing to return value to print into the write out file
                    items.Add(purchasedItem);
                }
            }

        }
        public class PurchasedItem : GroceryItem //inheriting properties from class GroceryItem
        {
            public PurchasedItem(string[] splitArray)
            {
                this._type = splitArray[0];
                this.name = splitArray[1];
                this.quantity = int.Parse(splitArray[2]);
                this.price = decimal.Parse(splitArray[3]);
            }
            public decimal regularCost() //method from cost of regular
            {
                return this.price * this.quantity * (decimal)1.1; //workout out cost  for purchases including GST
            }

        }

    }
}

答案 1 :(得分:0)

好,这是我尝试将其集成的方式,我尝试将项目更改为字符串,否则它不会让我添加我的方法来执行食品杂货价格的计算,但是,如果我更改了它,则尝试添加时一切似乎都出错了,这是到目前为止的代码:

抱歉,它可能很长,并且在https://dotnetfiddle.net/pBrXwz

中包含一些不必要的内容