0

I have a menu that displays the items from the observable collection from this class :

 class DeHavMenu
{
    private string foodName;
    private double price;

    public string FoodName
    {
        get {return foodName; }

        set {  foodName = value; }
    }

    public double Price
    {
        get {return price;}

        set {  price = value; }
    }

    public DeHavMenu(string f, double p)
    {
        this.foodName = f;
        this.price = p;
    }

   public override string ToString()
    {
        return (this.FoodName + " Price: £" + this.Price);
    }

}

Now on the menu page , I add the items on the page to display it on the list view:

public sealed partial class DeHavillandMenu : Page
{
    private ObservableCollection<DeHavMenu> menuItems = new ObservableCollection<DeHavMenu>();
    public DeHavillandMenu()
    { this.InitializeComponent();
        MenuList.SelectionMode = ListViewSelectionMode.Multiple;
        this.menuItems = new ObservableCollection<DeHavMenu>();
        this.menuItems.Add(new DeHavMenu("Sausage/Bacon roll and hot drink", 2.25));
        this.menuItems.Add(new DeHavMenu("5 item cooked breakfast and hot drink", 2.75));
        this.menuItems.Add(new DeHavMenu("Pizza with selection of pasta, wedges and garlic bread", 4.25));
        this.menuItems.Add(new DeHavMenu("Baked / grilled Halal chicken with piri piri", 6.50));
        this.menuItems.Add(new DeHavMenu("Baked / grilled Halal chicken with tandoori", 6.50)); 
        MenuList.DataContext = menuItems;
     }
}

The menu page displays the items fine, can select multiple items fine, but cannot find the solution to calculate the total price by taking the sum of the selected items. I know using Menulist.selecteditems.price is appropriate, but it does not give me the option to.

Thanks in advance.

t_Consumer
  • 47
  • 1
  • 6
  • Maybe you should have a readonly property where you sum Menulist.selecteditems.price with a foreach and return the total value. – Carlos Mar 16 '16 at 18:18
  • Off topic: consider using a [data type other than `double`](http://stackoverflow.com/questions/316727/is-a-double-really-unsuitable-for-money) to store money/currency values. – ardila Mar 16 '16 at 20:37
  • Will do , thanks for the advice – t_Consumer Mar 16 '16 at 20:53

3 Answers3

0
double sum = 0;
foreach(ListViewItem Item in MenuList.SelectedItems)
{
    sum += double.Parse(Item.SubItems[1].Text);
}
Ashkan Mobayen Khiabani
  • 30,915
  • 26
  • 90
  • 147
0

You can extend ObservableCollection to create a Menu type. This way, whenever you want to get the total, you can using a property that automatically does your calculations. You would also need to update DeHavMenu to have an IsSelected property (this will either be bound in XAML or set programmatically).

Menu:

public class Menu : ObservableCollection<DeHavMenu>
{
    public double TotalPrice 
    {
        get 
        { 
            var totalPrice = 0.0;
            foreach (var menuItem in this)
            {
                if (menuItem.IsSelected)
                {
                    totalPrice += menuItem.Price;
                }
            }
            return totalPrice;
        }
    }
}

Updated Item:

public class DeHavMenu
{
    private string foodName;
    private double price;
    private bool isSelected;

    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            isSelected = value;
            RaisePropetyyChanged(); // If XAML data-binding
        }
    }

    public string FoodName
    {
        get {return foodName; }

        set {  foodName = value; }
    }

    public double Price
    {
        get {return price;}

        set {  price = value; }
    }

    public DeHavMenu(string f, double p)
    {
        this.foodName = f;
        this.price = p;
    }

    public override string ToString()
    {
        return (this.FoodName + " Price: £" + this.Price);
    }
}

Then, update your menu items to:

this.menuItems = new Menu();

Now, you can get the total by doing:

var menu = this.menuItems as Menu;
if (menu != null)
{
    var total = menu.TotalPrice;
}

if you are not using DataBinding, and are setting stuff code-behind, you can add the following code before you get the TotalPrice.

foreach (var item in Menu)
{
    item.IsSelected = false; // reset to default
}

foreach(var selectedItem in Menulist.selecteditems)
{
    var deHavMenu = selectedItem as DeHavMenu;
    if (devHavMenu != null)
    {
        devHavMenu.IsSelected = true; 
    }
}
d.moncada
  • 14,927
  • 4
  • 45
  • 72
  • Sorry to sound like a newbie, but on the Menu:ObservableCollection , is this from a new class ? – t_Consumer Mar 16 '16 at 19:00
  • Yes, you are just inheriting from it. Menu will be a new derived class. – d.moncada Mar 16 '16 at 19:43
  • I made a new menu class which derives from Observablecollection , however I'm getting an inconsistent accessibility message indicating that the Observable Collections is less accessible to class Menu – t_Consumer Mar 16 '16 at 19:56
  • Make sure all of your classes are public. Menu and DeHavMenu – d.moncada Mar 16 '16 at 20:03
  • That was a silly mistake I overlooked by me, sorry. but this.menuItems. cannot find the Total Price method from the menu class – t_Consumer Mar 16 '16 at 20:14
  • The program compiled without any errors, but when I i tried the menu, the value stayed at 0? I added a textblock to display the data but stays zero. Within the if statement : test_block.text = total.ToString(); – t_Consumer Mar 16 '16 at 20:42
  • Okay, so I am guessing IsSelected is false then on all of your DeHavMenus which is why it is zero. Are you using XAML or WinForms? you will ned to set the IsSelected property on the item whenever it it selected on the UI – d.moncada Mar 16 '16 at 20:47
  • I'm using XAML , developing in a universal Windows application. – t_Consumer Mar 16 '16 at 20:50
  • hmm, it says because Menu is a type, its not valid in the context – t_Consumer Mar 16 '16 at 21:22
  • 1
    Okay so i left out "resetting the isselected property to default" just to try it assuming it was on defaulty already, tried the menu again the price does add up! Thank you so much for your help, very much appreciated :) – t_Consumer Mar 16 '16 at 21:30
  • awesome, no problem! – d.moncada Mar 16 '16 at 21:32
0

The "Menulist.selecteditems" you mentioned is an enumerable (a collection of something). This means it doesn't have one particular "Price" property. Instead, you need to add up every Price within MenuList.SelectedItems. You can use LINQ to do this pretty quickly with something along the lines of:

MenuList.SelectedItems.Sum(x => x.Price)