Помогите мне узнать, как добавить полный массив, который я создал, в одну строку. Похоже, что при изменении int на удвоение до значительных изменений ошибок, так что сейчас я просто хочу попытаться добавить все во всей строке.
Кажется, что когда я пытаюсь добавить массив, он печатает имя массива, когда я пытаюсь распечатать имя экземпляра массива, он печатает путь к файлу, когда я добавляю разделенный массив [0], он успешно печатает первый элемент в массиве. Как я могу добавить весь массив, а не только первый элемент?
Вот как выглядит текст:
регулярен, хлеб, 2.00,2
обычный, молоко, 2,00,3
Вот как я хочу, чтобы это выглядело после кодирования
обычный, хлеб, 2,00,2, (результат 2 * 2 * GST)
обычный, молоко, 2,00,3, (результат 2 * 3 * GST)
Это то, что я получаю (не нужно показывать обычную строку стоимости товара):
System.Collections.Generic.List`1 [System.String]
RegularItemCost:
4,4
Это код, который я получил для чтения, а также метод и конструкторы для вычислений:
public List<string> readFile()
{
string line = "";
StreamReader reader = new StreamReader("groceries.txt"); //variable reader to read file
while ((line = reader.ReadLine()) != null) //reader reads each line while the lines is not blank, line is assigned value of reader
{
line = line.Trim(); //gets rid of any spaces on each iteration within the line
if (line.Length > 0) //during each line the below actions are performed
{
string[] splitArray = line.Split(new char[] { ',' }); //creates a array called splitArray which splits each line into an array and a new char
type = splitArray[0]; // type is assigned for each line at position [0] on
name = splitArray[1]; //name is assigned at position [1]
//<<<-------food cost calculation methods initialized-------->>>>
RegularItem purchasedItem = new RegularItem(splitArray); //purchased Item is the each line to be printed
FreshItem freshItem = new FreshItem(splitArray);
double regCost = purchasedItem.getRegularCost(); //regCost will multiply array at position [2] with [3]
double freshCost = freshItem.getFreshItemCost();
string[] arrayList = { Convert.ToString(regCost), Convert.ToString(freshCost) };
List<string> newArray = new List<string>(splitArray);
newArray.AddRange(arrayList);
if (type == "regular")
{
// items.InsertRange(4, (arrayList)); //first write a line in the list with the each line written
items.Add(Convert.ToString(newArray));
items.Add("RegularItemCost:");
items.Add(Convert.ToString(regCost)); //next add the regCost method to write a line with the cost of that item
}
else if (type == "fresh")
{
items.Add(Convert.ToString(freshItem)); //first write a line in the list with the each line written
items.Add("FreshItemCost:");
items.Add(Convert.ToString(freshCost)); //next add the fresh method to write another line with the cost of that item
}
}
}
return items;
}
// Конструктор и метод
public class RegularItem : GroceryItem //inheriting properties from class GroceryItem
{
private string[] splitArray;
public RegularItem()
{
}
public RegularItem(string[] splitArray) //enables constructor for RegularItem to split into array
{
this.type = splitArray[0];
this.name = splitArray[1];
this.price = double.Parse(splitArray[2]); //each line at position 4 is a double
this.quantity = double.Parse(splitArray[3]); //each line at position 3 is parsed to an integer
}
public double getRegularCost() //method from cost of regular
{
return this.price * this.quantity * 1.1; //workout out cost for purchases including GST
}
}