Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2013
    Posts
    5

    Post-101 lesson6 enhanced product names question

    Hello,

    Lesson6 from C# 101 had an example with product names, introducing OOP concept (code at the bottom of the post). However I noticed that when you hit Enter right away, nothing displays, so I changed that by adding the following piece of code:

    Code:
    if (products.Count == 0)
                    Console.WriteLine("Nothing to declare, no products");
                else
                {
                    foreach (Product product in products)
                    {
                        product.Display();
                    }
                }
    This looks bad, as it should be part of the Display() method. So my question is how to incorporate this into the Display() method of the Product class? This requires to somehow pass to Display() a count of the List products. Any ideas?

    Here is the entire code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace OOP_lesson6_01
    {
        class Product
        {
            public string Name;
            public decimal Price;
            public bool IsInStock;
    
            public Product(string name, decimal price, bool isInStock)
            {
                Name = name;
                Price = price;
                IsInStock = isInStock;
            }
    
            public void Display()
            {
                Console.Write("(Printing): " + this.Name + " is $" + this.Price);
    
                if (this.IsInStock)
                    Console.WriteLine(" - in stock");
                else
                    Console.WriteLine(" - not in stock");
            }
        }
    
        class Program
        {
            static void Main()
            {
                List<Product> products = new List<Product>();
                bool isEnteringProduct = true;
                string name;
                decimal price;
                bool inStock;
    
                while (isEnteringProduct)
                {
                    Console.Write("Enter product name (or hit Enter to stop):");
                    name = Console.ReadLine();
    
                    if (name != "")
                    {
                        Console.Write("Enter product price $:");
                        price = decimal.Parse(Console.ReadLine());
                        Console.Write("Is in stock?");
                        inStock = Console.ReadLine() == "1";
    
                        products.Add(new Product(name, price, inStock));
                    }
                    else
                    {
                        isEnteringProduct = false;
                    }
                }
    
                Console.WriteLine("");
                Console.WriteLine("Done gathering products, now printing:");
                if (products.Count == 0)
                    Console.WriteLine("Nothing to declare, no products");
                else
                {
                    foreach (Product product in products)
                    {
                        product.Display();
                    }
                }
                Console.ReadKey();
            }
        }
    }
    Last edited by ProgrOne; 09-01-2013 at 06:03 AM.

  2. #2
    Join Date
    Jan 2007
    Posts
    216
    There is no way to do that inside of the Product class. Also why should it be part of that class? It's "error checking".

    Also if the list is empty, product.Display(); is never called. So even if you move it to product.Display() it will never get called.
    If you want to hide the error checking you need another class. Something like this
    Code:
    class ProductList
        {
            private List<Product> products = new List<Product>();
    
            public Product() { }
            
            public Add(Product product) {
                products.add(product);
            }
    
            public void Display() {
                if (products.Count == 0)
                    Console.WriteLine("Nothing to declare, no products");
                else
                {
                    foreach (Product product in products)
                    {
                        product.Display();
                    }
                }
            }
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •