【问题标题】:How do I read data from a text file, then write a new text file with new calculations and use my subclass methods for calculations?如何从文本文件中读取数据,然后用新的计算编写一个新的文本文件并使用我的子类方法进行计算?
【发布时间】:2019-04-16 12:29:14
【问题描述】:

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

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

请注意,我需要将其格式化为 Grocery Item 是基类而 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
                }

【问题讨论】:

  • 好吧,不是每个PurchasedItem 实际上都是FreshItem,它肯定没有freshItemCost。你会创建一个FreshItem 的实例而不是PurchasedItem 来调用成员:FreshItem purchasedItem = new FreshItem(50,1)
  • omg 感谢您指出这一点,出于某种原因,我试图在新类而不是基类中添加实例,我上周才开始学习 OOP,所以可能是新手错误.. . 好的,现在子类的事情已经解决了......我如何计算我正在阅读的内容?
  • 我不明白你的问题。您要执行计算吗?你想计算什么? freshItemCost-method 不符合您的预期吗?

标签: c#


【解决方案1】:

尝试以下代码开始:

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
            }

        }

    }
}

【讨论】:

    【解决方案2】:

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

    抱歉,它可能很长,并且有一些不必要的东西:https://dotnetfiddle.net/pBrXwz

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多