Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class Program
    {
        string MobileName;
        bool Nokia5233;
        bool Nokia2690;
        bool SonyP910i;
        int Price;
        public void ListOfMobiles()
        {
           
            
            Console.WriteLine("The first Model Is Nokia5233");
            
            Console.WriteLine("The second Model Is SonyP910i");
            
            Console.WriteLine("The third Model is Nokia2690");

            if (Nokia2690)
            {
                Console.WriteLine("The price is 4900");
                //Price = Convert.ToInt32(Console.ReadLine());
            }
            else
            {

            }
            if (Nokia5233)
            {
                Console.WriteLine("The Price is 7000");
                //Price = Convert.ToInt32(Console.ReadLine());
            }
            else
            {


            }
            
        }
        
        public void ChooseMobile()
        {
            Console.WriteLine("Enter Mobile Name");
            //Console.WriteLine("/n The Model Number is: ");
            MobileName = Console.ReadLine();
        }
        public void DisplayMobileDetails()
        {
            Console.WriteLine("The Mobile name is {0}", MobileName);
            Console.WriteLine("The price is {0}", Price );
        }
        static void Main(string[] args)
        {
            Program prm = new Program();
            prm.ListOfMobiles();
            prm.ChooseMobile();
            
            prm.DisplayMobileDetails();
            Console.ReadLine();
        }
    }
}

i wrote this code nd want the result but the desire result didn't came out i want when i enter mobile name it show mobile name as well as its price
Posted
Updated 6-Nov-11 21:31pm
v2

C#
if (Nokia2690)
           {
               Console.WriteLine("The price is 4900");
               //Price = Convert.ToInt32(Console.ReadLine());
           }
           else
           {

           }
           if (Nokia5233)
           {
               Console.WriteLine("The Price is 7000");
               //Price = Convert.ToInt32(Console.ReadLine());
           }
           else
           {


           }


Who is actually setting your Nokia2690,Nokia5233 bool values. And you do not have anything in else part.So no output.

Regards
 
Share this answer
 
You need to check the entered details against you list of known items. In your case, it is probably easiest to hardwire it, but in the real world we wouldn't - for beginners though, it will give you an idea.
C#
public void ChooseMobile()
    {
    bool selected = false;
    do
        {
        Console.WriteLine("Enter Mobile Name");
        //Console.WriteLine("/n The Model Number is: ");
        MobileName = Console.ReadLine();
        switch (MobileName)
            {
            case "Nokia5233":
                Nokia5233 = true;
                selected = true;
                break;
            case Nokia2690:
                Nokia2690 = true;
                selected = true;
                break;
            case SonyP910i:
                SonyP910i = true;
                selected = true;
                break;
            }
        } while (!selected);
    }
 
Share this answer
 
If the code you provided is the complete code, it won't. Never. You see, you've declared members
C#
bool Nokia5233;
bool Nokia2690;
bool SonyP910i;

but you dont' set their values. So, they retain thair default values, which are false. So no if is gonna trigger in ListOfMobiles(). And Price value is never set as well - so what do you expect?
What you need here is to set corresponding boolean value (e.g. Nokia2690) in ChooseMobile() method depending on user input. Use switch for that. One more - and probably better - option is to turn your MobileName into property with switch in its set part that would tune your object (set corresponding bool value and price).

That'll help your solution work, no more. There are certanly other improvements that can be done, for example removing bool variables completely or replacing them by enum member
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900