Question

Hello I am trying to make a shopping basket program for some coursework I have been given a diffent type of "Shopping basket" to follow but i cant get my list to keep updating. so the problem im having is the 'ShoppingBasketList()' needs a return type but in the example i got given it does not. i have spent ages trying to work out why and i just cant. if anyone has any idea would be a great help!

public class ShoppingBasket
{
    public List<ShoppingBasketItem> Items { get; private set; }        


     public ShoppingBasketList()
    {
      Items = new List<ShoppingBasketItem>();
    }

    internal static void AddToList(string productName, int quantity, decimal latestPrice)
    {
        for (int i = 0; i < Items.Count; i++)
        {
            // if the item is already in the list
            if (Items[i].ItemName == productName)
            {
                Items[i].UpdateShoppingBasketList(quantity, latestPrice);
                return;
            }
        }

        // It's not in the list
        ShoppingBasketItem sbi = new ShoppingBasketItem(productName, quantity, latestPrice);
        Items.Add(sbi);
    }
}
Was it helpful?

Solution

public ShoppingBasketList()
{
    Items = new List<ShoppingBasketItem>();
}

Is a constructor declaration (because it does not specify a return type). A constructor should always have the same name as the class it belongs to. Your constructor is called ShoppingBasketList while your class is called ShoppingBasket. You should rename your class to ShoppingBasketList or rename your constructor to ShoppingBasket.

eg.

public ShoppingBasket()
{
    Items = new List<ShoppingBasketItem>();
}

You can read a bit more about constructors here.

OTHER TIPS

In addition to those above who correctly state that you should rename ShoppingBasketList() to ShoppingBasket(), you don't need a method with a return value to get to see your items. You already have declared Items to be a List. From you ShoppingBasket() object, you will get your list by myShoppingBasket.Items

Your program is notifying you of a return type because you have specified a return;. Therefore remove the return in your for.

for (int i = 0; i < Items.Count; i++)
{
    // if the item is already in the list
    if (Items[i].ItemName == productName)
    {
        Items[i].UpdateShoppingBasketList(quantity, latestPrice);
        return; //replace me with something else...
    }
}

Instead, add a looping invariant notFound (or break).

Boolean notFound = true;
for (int i = 0; i < Items.Count && notFound; i++)
{
    // if the item is already in the list
    if (Items[i].ItemName == productName)
    {
        Items[i].UpdateShoppingBasketList(quantity, latestPrice);
        notFound = false; //exiting the the loop
    }
}

Oh and your method ShoppingBasketList is written as a constructor (anything that is public <Insert Name Here> is a constructor) which is called upon when declaring the class for example ShoppingBasket sb = new ShoppingBasket();.

 public ShoppingBasketList()
{
  Items = new List<ShoppingBasketItem>();
}

Either rename it as your class name, public ShoppingBasket or remove it altogether and initialize your Items in the declaration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top