How to calculate total in arrayList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jackwaters
    New Member
    • Jul 2010
    • 5

    #1

    How to calculate total in arrayList

    I have a problem in that I have an arrayList called pizzaOrders which contains a number of pizza orders. The pizza orders contain information like names, addresses, total price for orders and the dates of orders.

    The date is selected through a dateTimePicker. Basically what I need to do is count the orders on a particular day and produce a graph for this.

    So I need, for example, to search the arrayList and calculate how many orders were made on each day. I assume this is done by totalling the number of times Monday appears in the arrayList to give me a figure.

    Any help would be great, I've been trying hard but to no avail.

    Thanks

    Jack
  • Christopher Nigro
    New Member
    • Jul 2010
    • 53

    #2
    Can you ditch the array list and use a List<PizzaOrder > instead? If so, you could do something similiar to this:

    Code:
    List<PizzaOrder> orders = new List<PizzaOrder>();
    orders.Add(new PizzaOrder("Customer A", new DateTime(2010, 7, 1, 12, 5, 0), (decimal)19.95d));
    orders.Add(new PizzaOrder("Customer B", new DateTime(2010, 7, 1, 13, 30, 0), (decimal)6.99d));
    orders.Add(new PizzaOrder("Customer C", new DateTime(2010, 7, 2, 11, 45, 0), (decimal)42.80d));
    orders.Add(new PizzaOrder("Customer D", new DateTime(2010, 7, 3, 04, 0, 0), (decimal)10.00d));
    
    decimal dayTotal = (from o in orders where o.OrderDate.DayOfWeek == DayOfWeek.Thursday select o.Total).Sum();
    
    Console.WriteLine(string.Format("The total for Thursday is: {0}", dayTotal));

    Comment

    • Jackwaters
      New Member
      • Jul 2010
      • 5

      #3
      How would I use it to display the total number of orders on say Monday. I don't want the total for that day just the total number of orders.

      For example (using your code):
      Code:
      orders.Add(new PizzaOrder("Customer A", new DateTime(2010, 7, 1, 12, 5, 0), (decimal)19.95d));
      orders.Add(new PizzaOrder("Customer B", new DateTime(2010, 7, 1, 13, 30, 0), (decimal)6.99d));
      orders.Add(new PizzaOrder("Customer C", new DateTime(2010, 7, 2, 11, 45, 0), (decimal)42.80d));
      orders.Add(new PizzaOrder("Customer D", new DateTime(2010, 7, 3, 04, 0, 0), (decimal)10.00d));
      To produce the result that 2 orders were made on Thursday? At the moment it is displaying the total price.

      Thanks,

      Jack

      Comment

      • Christopher Nigro
        New Member
        • Jul 2010
        • 53

        #4
        Ah, sorry about that! I misread your original post. All you need to do is change that one line to this:

        Code:
        int dayTotal = (from o in orders where o.OrderDate.DayOfWeek == DayOfWeek.Thursday select o.Total).Count();
        I assume you are using Visual Studio - if so, delete that last '.' before Count() and then retype it - IntelliSense will pop up and show you all of the goodies that are available.

        Comment

        • Jackwaters
          New Member
          • Jul 2010
          • 5

          #5
          Works perfectly, thank you.

          Comment

          Working...