Hi,

The program compiles and executes with no errors but its not saving the category that the users enters as one of the constants listed in the enum 'ItemCategory'. So if the users types 1, it should save category as 'Women', if users types 5, the program should save category as 'Babies' etc. Category is displayed as blank but everything else is shown correctly.
I haven't done the data validation yet, I will do that later.

So if you guys can please help me out, I'd appreciate it, thank you.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DepartmentStore9
{
    public enum ItemCategory
    {
        Unspecified,
        Women,
        Girls,
        Men,
        Boys,
        Babies
        
    }
    class ShoppingItem
    {
        private ItemCategory cat; 
        private long itemNo;
        private string name;
        private string size;
        private decimal price;
        //private uint category = 1;
        // A constructor used to initialize an item
        
         /* Custom constructor
         */ 
        public ShoppingItem(long itemNo,
                               string name,
                               string size,
                               decimal price)
        {
            this.itemNo = itemNo;
            this.name = name;
            this.size = size;
            this.price = price;
        }
        
        public ShoppingItem() // default constructor
        {
        }


        // A property for the stock number of an item
        public long ItemNumber
        {
            get
            {
                return this.itemNo;
            }
            set
            {
                this.itemNo = value;
            }
        }

        // A property for the name of an item
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

        // A property for size of a merchandise
        public string Size
        {
            get
            {
                if (this.size == "0")
                    return "Unknown Size or Fits All";
                else
                    return this.size;
            }
            set
            {
                this.size = value;
            }
        }

        // A property for the marked price of an item
        public decimal UnitPrice
        {
            get
            {
                return this.price;
            }
            set
            {
                this.price = value;
            }
        }

        public ItemCategory Category
        {
          get
          { 
                return cat;
          }
            set
            {
                cat = value;
            }
        }
         
        public static ShoppingItem Read()
        {
           
            ShoppingItem store = new ShoppingItem();

            int category;
            Console.WriteLine("Department Store");
            Console.Write("Enter number#:    ");
            store.ItemNumber = long.Parse(Console.ReadLine());

            Console.WriteLine("Store Items category");
            Console.WriteLine("\t1. Women");
            Console.WriteLine("\t2. Girls");
            Console.WriteLine("\t3. Men");
            Console.WriteLine("\t4. Boys");
            Console.WriteLine("\t5. Babies");
            Console.Write("Enter the category \t");
            category = int.Parse(Console.ReadLine());

            //-------------------------- debug purposes
            Console.WriteLine("category entered is: ", category.ToString());
            //-----------------------------
            if (category == 1)
            {
                store.Category = ItemCategory.Women;
            }
            if (category == 2)
            {
                store.Category = ItemCategory.Girls;
            }

            if (category == 3)
            {
                store.Category = ItemCategory.Men;
            }

            if (category == 4)
            {
                store.Category = ItemCategory.Boys;
            }

            if (category == 5)
            {
                store.Category = ItemCategory.Babies;
            }
            else
            {
                store.Category = ItemCategory.Unspecified;
            }
            //-------------------debug purposes
            Console.WriteLine("category saved is:   ", store.Category);
            //-------------------

            Console.Write("Enter name#:    ");
            store.Name = Console.ReadLine();

            Console.Write("Enter size#:     ");
            store.Size = Console.ReadLine();

            Console.Write("Enter price#:    ");
            store.UnitPrice = decimal.Parse(Console.ReadLine());

            return store;
        }

        public static void write(ShoppingItem store)
        {
            Console.WriteLine("Department Store invoice");
            Console.WriteLine("---------------------------");
            Console.WriteLine("Customer Invoice#:       ");
            Console.WriteLine("---------------------------");
            Console.WriteLine("Item #:      {0}", store.ItemNumber.ToString());
            Console.WriteLine("Category#:   ", store.Category);
            Console.WriteLine("Description: {0}", store.Name);
            Console.WriteLine("Item Size:   {0}", store.Size);
            Console.WriteLine("Unit Price:  {0:C}", store.UnitPrice);
        }

    }
}

Use if..elseif..
Correct code:

//-------------------------- debug purposes
            Console.WriteLine("category entered is: {0} ",  category );
            //-----------------------------
            if (category == 1)
            {
                store.Category = ItemCategory.Women;
            }
            else
            if (category == 2)
            {
                store.Category = ItemCategory.Girls;
            }
            else
            if (category == 3)
            {
                store.Category = ItemCategory.Men;
            }
            else
            if (category == 4)
            {
                store.Category = ItemCategory.Boys;
            }
            else
            if (category == 5)
            {
                store.Category = ItemCategory.Babies;
            }
            else
            {
                store.Category = ItemCategory.Unspecified;
            }
            //-------------------debug purposes
            Console.WriteLine("category saved is:  {0} ", store.Category);
            //-------------------

Don't even bother with the if. Enum's are basically just numbers so you can do this:

Store.Category = (ItemCategory) category;

That still hasn't solved the problem because for some reason int.Parse() is not converting category entered by user to int.

Ok, I inserted this code'Console.WriteLine("category entered is: ", Console.ReadLine());' and found out the program is not even accepting what the users enters for category
After the user has entered the category, ie '2', it still is showing "Category entered is:"
It is showing category as blank;

Can anyone please tell me what I am doing wrong? Thankyou.

Look at your code.

This is wrong

Console.WriteLine("category entered is: ", category.ToString());

It must be.

Console.WriteLine("category entered is: {0} ", category);
//Or
Console.WriteLine("category entered is: " +  category);

Thanks alot. It works now. I changed the code from
'Console.WriteLine("category entered is: ", category);'
to
'Console.WriteLine("category entered is: {0} "+ category);'
and it works now.

Thank you adatapost.

Do you know why
'Console.WriteLine("category entered is: ", category);'
was showing blank though?

Edited 5 Years Ago by sunny124: n/a

Every method has a specific syntax or prototype and we must have to follow it. Thanks! I'm glad you got it working.

This question has already been answered. Start a new discussion instead.