简体   繁体   中英

C# Split text array into sub array

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 ie array, list or collection. you will then use iteration

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

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();

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM