Shashank
Shashank

Reputation: 107

Properties always return zero / null values

We have been given an assignment to develop a Shopping cart using C# Which has Item (Properties: name, price) ItemOrder(Properties: Item,Quantity) I am trying to find out the net price of a product as price*quantity, but the price property always returns 0, I even tried printing the name and it appears a null The quantity appears properly though.

Class Item

private string name; 
private double price;

public string Name { get { return name; } set { name=value;} }
public double Price { get { return price; } set { price = value; } }
public Item()
{
    Name = this.name;
    Price = this.price;
}
public Item(string name, double price)
{
    Name = name;
    Price = price;
}

Class ItemOrder : Item

private Item product;
private int quantity {get; set;}

public int Quantity { get {return quantity ;} set { quantity=value ;} }

public ItemOrder()
{
    this.product.Name = Name;
    this.product.Price = Price;
    this.Quantity = Quantity;

}
public ItemOrder(Item product, int quantity)
{
    this.product=product;
    Quantity = quantity;
}

Main Method: The ItemOrders are properly added within the cart, and printed, But i am unable to access the price and name for calculations

List<Item> inventory = new List<Item>();
Item pancakes = new Item("Pancakes", 6.25);
inventory.Add(pancakes);
List<ItemOrder> currentCart = new List<ItemOrder>();
ItemOrder item = new ItemOrder(inventory[userInput], quantity);
currentCart.Add(item);
foreach (ItemOrder i in currentCart)
{
    double currentPrice = 0; 
    currentPrice = (i.Price*.i.Quantity);
    Console.WriteLine("(" + (currentCart.IndexOf(i) + 1) + ") : " + i.ToString() + "\t        "+currentPrice);
}

Thanks in advance

Upvotes: 1

Views: 4068

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 148990

There are a number of problems here.

  1. First, if your getters and setters are that simple, there's no reason not to use auto-implemented properties. You can rid of the backing properties and use { get; set; } instead.
  2. There's no need to initialize the properties to themselves inside the default constructor.
  3. (i.Price*.i.Quantity) isn't right. You need to access the Price of the product inside the ItemOrder somehow, and *. should be just *
  4. The easiest way to expose the product that's referenced by an ItemOrder is to make a public property.
  5. You probably want to print the Name of the item, and not simply call ToString() (which will return Item).

For example:

public class Item {
    public string Name { get; set; }
    public double Price { get; set; }
    public Item()
    {
    }
    public Item(string name, double price)
    {
        this.Name = name;
        this.Price = price;
    }
}
public class ItemOrder
{
    public Item Product { get; set; }
    public int Quantity { get; set; }

    public ItemOrder()
    {
    }
    public ItemOrder(Item product, int quantity)
    {
        this.Product = product;
        this.Quantity = quantity;
    }
}

void Main()
{
    List<Item> inventory = new List<Item>();
    Item pancakes = new Item("Pancakes", 6.25);
    inventory.Add(pancakes);
    List<ItemOrder> currentCart = new List<ItemOrder>();
    ItemOrder item = new ItemOrder(inventory[0], 2); // stand in values for userInput and quantity
    currentCart.Add(item);
    foreach (ItemOrder i in currentCart)
        {
            double currentPrice = i.Product.Price*i.Quantity;
            Console.WriteLine("(" + (currentCart.IndexOf(i) + 1) + ") : " + i.Product.Name + "\t        " + currentPrice);
        }
}

This will print

(1) : Pancakes          12.5

Upvotes: 4

ekad
ekad

Reputation: 14614

Because of this line:

ItemOrder item = new ItemOrder(inventory[userInput], quantity);

You are calling this constructor:

public ItemOrder(Item product, int quantity)
{
    this.product = product;
    Quantity = quantity;
}

so the price that you're looking for is actually inside the product variable.

Try to add Product as a public property:

public Item Product { get {return product;} set { product=value ;} }

and replace this line:

currentPrice = (i.Price*.i.Quantity);

with this:

currentPrice = (i.Product.Price * i.Quantity);

Upvotes: 4

Related Questions