0

I can't seem to find much on this online :(

For my course I have to get text from a grocery.txt file and output an invoice.

The text file looks like this:

regular,bread,2.00,2
regular,milk,2.00,3
regular,cereal,4.00,1
fresh,rump steak,11.99,0.8
fresh,apple,4.60,1.00
fresh,cucumber,2.20,0.936
regular,cream,2.20,1
regular,mustard,3.30,1
fresh,carrots,1.79,1.5
regular,tomato sauce,2.95,1
regular,salt,2.80,1
fresh,bananas,4.00,1.2

Currently I use this method to get the text:

    string[] groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt");

What I get is an array that in stores the entire line (for example "fresh,bananas,4.00,1.2"). I have no idea how to split the array into sub arrays or even whats the best way to go about this. Unfortunately this course task are way too advanced for the little material we have been taught and time we have for this. I have already spend around 6 hours watching videos and getting not very far in learning the advanced stuff.

This is what I am trying to accomplish.

A. Class named grocerrItem. This class has properties: name and price this class must have 2 constructors

A. subclass of this is a purchasedItem. this class has an integer property 'quantity' one of its methods findCost, calculates the cost of the item, including 10% GST. the formula for this is price * quantity * 1.1

C. A subclass of this class (purchasedItem), freshItem has a property weight, a double type. The findCost method here, uses the formula:wieght * price, as it is not subject to GST.

Groceries.txt contains information as follows:

type(regular or fresh), name, price, quantity/weight - depending on being regular or fresh.

** An invoice should be represented as a collection of grocery items (parent class). This can be done using any container structure i.e. array, list or collection. you will then use iteration

RiCHiE
  • 278
  • 3
  • 14

3 Answers3

2

You can just use String.Split which returns you the sub array that you want.

public static void Main()
{
    string[] groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt");

    // now split each line content with comma. It'll return you an array (sub-array)
    foreach (var line in groceryItemsArray)
    {
        string[] itemsInLine = line.Split(',');

       // Do whatevery you want to do with this.
       // e.g. for 1st line itemsInLine array will contains 4 items 
       // "regular","bread","2.00", "2"
    }
}
Sachin
  • 40,216
  • 7
  • 90
  • 102
  • Interesting, makes sense. but when I try to write out itemsInLine I get an error "The name itemsInLine does not exist in this context". Can you please explain a little how this works? – RiCHiE May 28 '15 at 05:39
  • @RiCHiE where are you accessing that `itemsInLine` variable and also let me know, what actually you want to do at the end so that I can explain how to achieve that – Sachin May 28 '15 at 05:45
  • Never mind sorry I was using wrong syntax. I'm using Console.Write(groceryItemsArray[0][1]); this seems to be seperated by letter though and not by the , – RiCHiE May 28 '15 at 05:45
2

You can use this code (I'm not sure what exactly you looking for):

/// It's return all text as a single list
var groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt")
      .SelectMany(x => x.Split(new char[]{','} , StringSplitOptions.RemoveEmptyEntries)).ToList() ;

Or if want to return as Child List can use this code:

 var groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt").Select(x => 
           new { Child = x.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) }).ToList();
Peyman
  • 3,068
  • 1
  • 18
  • 32
  • Looking to split the array up by each item in each line but not loose each line as an item. – RiCHiE May 28 '15 at 05:53
  • Lambdas might be a bit too advanced for him. He needs to understand what they are, what they do and how they should be used... not just given the code. Just a thought. – B.K. May 28 '15 at 06:22
1

I didn't really understand what are you trying to output, but here's some basic things I did understand on your question:

(code not tested)

I created a parent class:

class GroceryItem 
{
    public string ItemName {get;set;}
    public decimal Price {get;set;}

    public GroceryItem() { }
}

And creates a child class:

class PurchasedItem : GroceryItem
{
    public int Quantity { get;set; }
    public decimal Cost { get;set; }

    public PurchasedItem(string[] item)
    {
        ItemName = item[1];
        Price = decimal.Parse(item[2]);
        Quantity = int.Parse(item[3]);
        FindCost();
    }

    private void FindCost()
    {
        Cost = Price * Quantity * 1.1;
    }
}

Using your input to get all groceryItems, you can iterate using foreach loop and collect a list of purchasedItems.

Main()
{
    string[] groceryItems = System.IO.File.ReadAllLines(@"C:\C\groceries.txt");

    var purchasedItems = new List<PurchasedItem>();
    foreach (var item in groceryItems)
    {
        string[] line = item.Split(',');
        purchasedItems.Add(new PurchasedItem(line));
    }
}

Well if you didn't understand this basic concept of programming, you better start on this link.

jomsk1e
  • 3,585
  • 7
  • 34
  • 59
  • Yeah a bit lost I guess, been through a lot of basic material. – RiCHiE May 28 '15 at 06:28
  • If you didn't understand the example that I've posted. I recommend you to start with the link I included to my answer, good luck! :) – jomsk1e May 28 '15 at 06:32
  • Yeah I'm having a look, I don't see why it's so complicated to turn the array into a multidimensional array that has the 4 split values similar to what Sachin posted. Maybe my thinking of other languages is completely different to C# – RiCHiE May 28 '15 at 06:46