Home > OS >  Tv Remote Control in C#
Tv Remote Control in C#

Time:09-27

I'm new using C# and I'm building a remote control. I got stuck and my code is not compiling. Please I need suggestions and how can I improve my code.

I think its Console.Readline() but I'm not sure how to use it. Also how can change this to 2 primary classes?

public class testRemote
{
    public static bool PowerOn(bool powerStatus)
    {
        if (powerStatus == true)
        {
            testRemote.DisplayMessage("TV already on");
        }
        else
        {
            powerStatus = true;
            testRemote.DisplayMessage("TV On, Your Channel is 3 and the Volume is 5");
        }
        return powerStatus;
    }

    public static bool PowerOff(bool powerStatus)
    {
        if (powerStatus == false)
        {
            testRemote.DisplayMessage("TV already off");
        }
        else
        {
            powerStatus = false;
            testRemote.DisplayMessage("TV is now off");
        }
        return powerStatus;
    }
    
    public static int VolumeUp(bool powerStat, int vol)
    {
        if (powerStat == false)
        {
            testRemote.DisplayMessage("TV is off");
        }
        else
        {
            if (vol >= 10)
            {
                testRemote.DisplayMessage("TV is already on Maximum Volume.");
            }
            else
            {
                vol  ;
                if (vol == 10)
                {
                    testRemote.DisplayMessage("Maximum Volume.");
                }
            }
        }
        return vol;
    }

    public static int VolumeDown(bool powerStat, int vol)
    {
        if (powerStat == false)
        {
            testRemote.DisplayMessage("TV is off");
        }
        else
        {
            if (vol <= 0)
            {
                testRemote.DisplayMessage("Sound Muted");
            }
            else
            {
                vol--;
                if (vol == 0)
                {
                    testRemote.DisplayMessage("Sound Muted");
                }
            }
        }
        return vol;
    }

    public static int ChannelUp(bool powerStat, int channelNo)
    {
        if (powerStat == false)
        {
            testRemote.DisplayMessage("TV is off");
        }
        else
        {
            if (channelNo < 99)
            {
                channelNo  ;
            }
            else
            {
                channelNo = 2;
            }
            Console.WriteLine("Channel "   channelNo.ToString());
        }
        return channelNo;
    }

    public static int ChannelDown(bool powerStat, int channelNo)
    {
        if (powerStat == false)
        {
            testRemote.DisplayMessage("TV is off");
        }
        else
        {
            if (channelNo == 2)
            {
                channelNo = 99;
            }
            else
            {
                channelNo--;
            }
            Console.WriteLine("Channel "   channelNo.ToString());
        }
        return channelNo;
    }

    public static String SmartMenu(bool powerStat, String md)
    {
        if (powerStat == false)
        {
            testRemote.DisplayMessage("TV is off");
        }
        else
        {
            testRemote.DisplayMessage("Smart Menu On");
            md = "TV";
        }
        return md;
    }

    public static String SetSettings(bool powerStat, String md)
    {
        if (powerStat == false)
        {
            testRemote.DisplayMessage("TV is off");
        }
        else
        {
            testRemote.DisplayMessage("Settings On");
            md = "Settings";
        }
        return md;
    }

    public static void DisplayMessage(String msg)
    {
        Console.WriteLine(msg);
    }

    public static void Banner()
    {
        Console.WriteLine("Welcome to TV Remote Control");
        Console.WriteLine("Please enter your selection");
        Console.WriteLine("Power On - turns on your TV");
        Console.WriteLine("Power Off - turns off your TV");
        Console.WriteLine("Increase volume - turns up the volume");
        Console.WriteLine("Decrease volume - turn down the volume");
        Console.WriteLine("Channel Up - increments the channel");
        Console.WriteLine("Channel Down - decrements the channel");
        Console.WriteLine("Smart Menu");
        Console.WriteLine("Settings");
        Console.WriteLine();
        Console.WriteLine("Please enter your selection");
    }

    public static void Main(String[] args)
    {
        bool powerOn = false;
        int channel = 3;
        int volume = 5;
        string mode = "TV";
        string choice = "Turn On";
        string c = Console.ReadLine();
        while (choice.ToLower().Equals("Exit".ToLower()) != true)
        {
            if (choice.ToLower().Equals("Power On".ToLower()) == true)
            {
                powerOn = testRemote.PowerOn(powerOn);
            }
            if (choice.ToLower().Equals("Power Off".ToLower()) == true)
            {
                powerOn = testRemote.PowerOff(powerOn);
            }
            if (choice.ToLower().Equals("Increase volume".ToLower()) == true)
            {
                volume = testRemote.VolumeUp(powerOn, volume);
            }
            if (choice.ToLower().Equals("Decrease volume".ToLower()) == true)
            {
                volume = testRemote.VolumeDown(powerOn, volume);
            }
            if (choice.ToLower().Equals("Channel Up".ToLower()) == true)
            {
                channel = testRemote.ChannelUp(powerOn, channel);
            }
            if (choice.ToLower().Equals("Channel Down".ToLower()) == true)
            {
                channel = testRemote.ChannelDown(powerOn, channel);
            }
            if (choice.ToLower().Equals("Mode TV".ToLower()) == true)
            {
                mode = testRemote.SmartMenu(powerOn, mode);
            }
            if (choice.ToLower().Equals("Mode DVD".ToLower()) == true)
            {
                mode = testRemote.SetSettings(powerOn, mode);
            }
            Console.WriteLine();
            Console.WriteLine();
            choice = Console.ReadLine();
        }
        Console.WriteLine("Thank you for the Remote Controller");
        Console.ReadLine();
    }
}

Compiler error message

CodePudding user response:

Since the question is not clear i slightly modified your code, it compiles and runs BUT needs a lot of refactor. Here is the working code.

using System;

namespace TvRemote
{
    public class testRemote
    {
        public static bool PowerOn(bool powerStatus)
        {
            if (powerStatus == true)
            {
                testRemote.DisplayMessage("TV already on");
            }
            else
            {
                powerStatus = true;
                testRemote.DisplayMessage("TV On, Your Channel is 3 and the Volume is 5");
            }
            return powerStatus;
        }

        public static bool PowerOff(bool powerStatus)
        {
            if (powerStatus == false)
            {
                testRemote.DisplayMessage("TV already off");
            }
            else
            {
                powerStatus = false;
                testRemote.DisplayMessage("TV is now off");
            }
            return powerStatus;
        }

        public static int VolumeUp(bool powerStat, int vol)
        {
            if (powerStat == false)
            {
                testRemote.DisplayMessage("TV is off");
            }
            else
            {
                if (vol >= 10)
                {
                    testRemote.DisplayMessage("TV is already on Maximum Volume.");
                }
                else
                {
                    vol  ;
                    if (vol == 10)
                    {
                        testRemote.DisplayMessage("Maximum Volume.");
                    }
                }
            }
            return vol;
        }

        public static int VolumeDown(bool powerStat, int vol)
        {
            if (powerStat == false)
            {
                testRemote.DisplayMessage("TV is off");
            }
            else
            {
                if (vol <= 0)
                {
                    testRemote.DisplayMessage("Sound Muted");
                }
                else
                {
                    vol--;
                    if (vol == 0)
                    {
                        testRemote.DisplayMessage("Sound Muted");
                    }
                }
            }
            return vol;
        }

        public static int ChannelUp(bool powerStat, int channelNo)
        {
            if (powerStat == false)
            {
                testRemote.DisplayMessage("TV is off");
            }
            else
            {
                if (channelNo < 99)
                {
                    channelNo  ;
                }
                else
                {
                    channelNo = 2;
                }
                Console.WriteLine("Channel "   channelNo.ToString());
            }
            return channelNo;
        }

        public static int ChannelDown(bool powerStat, int channelNo)
        {
            if (powerStat == false)
            {
                testRemote.DisplayMessage("TV is off");
            }
            else
            {
                if (channelNo == 2)
                {
                    channelNo = 99;
                }
                else
                {
                    channelNo--;
                }
                Console.WriteLine("Channel "   channelNo.ToString());
            }
            return channelNo;
        }

        public static String SmartMenu(bool powerStat, String md)
        {
            if (powerStat == false)
            {
                testRemote.DisplayMessage("TV is off");
            }
            else
            {
                testRemote.DisplayMessage("Smart Menu On");
                md = "TV";
            }
            return md;
        }

        public static String SetSettings(bool powerStat, String md)
        {
            if (powerStat == false)
            {
                testRemote.DisplayMessage("TV is off");
            }
            else
            {
                testRemote.DisplayMessage("Settings On");
                md = "Settings";
            }
            return md;
        }

        public static void DisplayMessage(String msg)
        {
            Console.WriteLine(msg);
        }

        public static void Banner()
        {
            Console.WriteLine("Welcome to TV Remote Control");
            Console.WriteLine("Please enter your selection");
            Console.WriteLine("Power On - turns on your TV");
            Console.WriteLine("Power Off - turns off your TV");
            Console.WriteLine("Increase volume - turns up the volume");
            Console.WriteLine("Decrease volume - turn down the volume");
            Console.WriteLine("Channel Up - increments the channel");
            Console.WriteLine("Channel Down - decrements the channel");
            Console.WriteLine("Smart Menu");
            Console.WriteLine("Settings");
            Console.WriteLine();
            Console.WriteLine("Please enter your selection");
        }

        public static void Main(String[] args)
        {
            bool powerOn = false;
            int channel = 3;
            int volume = 5;
            string mode = "TV";
            //string choice = "Turn On"; //THIS MUST BE Power On if you want to power on the tv as soon as the app is launched
            Banner();
            string choice = Console.ReadLine();
            while (!choice.ToLower().Equals("Exit".ToLower()))
            {
                if (choice.ToLower().Equals("Power On".ToLower()) == true)
                {
                    powerOn = testRemote.PowerOn(powerOn);
                }
                if (choice.ToLower().Equals("Power Off".ToLower()) == true)
                {
                    powerOn = testRemote.PowerOff(powerOn);
                }
                if (choice.ToLower().Equals("Increase volume".ToLower()) == true)
                {
                    volume = testRemote.VolumeUp(powerOn, volume);
                }
                if (choice.ToLower().Equals("Decrease volume".ToLower()) == true)
                {
                    volume = testRemote.VolumeDown(powerOn, volume);
                }
                if (choice.ToLower().Equals("Channel Up".ToLower()) == true)
                {
                    channel = testRemote.ChannelUp(powerOn, channel);
                }
                if (choice.ToLower().Equals("Channel Down".ToLower()) == true)
                {
                    channel = testRemote.ChannelDown(powerOn, channel);
                }
                if (choice.ToLower().Equals("Mode TV".ToLower()) == true)
                {
                    mode = testRemote.SmartMenu(powerOn, mode);
                }
                if (choice.ToLower().Equals("Mode DVD".ToLower()) == true)
                {
                    mode = testRemote.SetSettings(powerOn, mode);
                }
                Console.WriteLine();
                Console.WriteLine();
                choice = Console.ReadLine();
            }
            Console.WriteLine("Thank you for the Remote Controller");
            Console.ReadLine();
        }
    }
}
  •  Tags:  
  • c#
  • Related