Remot TV

1. Class window



2. Command yang tersedia



3. Mengganti channel



4. Mengganti volume



5. Source code
Television Class

/**
 * Television Class
 *
 * @author (ARINO JENYNOF)
 * @version (20180920)
 */
public class Television
{
    private int channel;
    private int volume;
    private boolean state;
    
    public Television()
    {
        channel = 1;
        volume = 0;
        state = false;
    }
    
    public void set_state(boolean state)
    {
        this.state = state;
    }
    
    public void set_channel(int inp)
    {
        if (state)
        {
            if (inp > 0 && inp <= 20)
            {
                channel = inp;
                System.out.println(">>> Now showing channel " + channel);
            }
            else
            {
                System.out.println(">>> Channel " + inp + " not available");
                System.out.println(">>> Now showing channel " + channel);
            }
        }
        else
        {
            System.out.println(">>> TV is off!");
        }
    }
    
    public void change_channel(int dir)
    {
        if (state)
        {
            if (dir == 1)
            {
                channel += 1;
                if (channel > 20)
                {
                    channel = 1;
                }
                System.out.println(">>> Now showing channel " + channel);
            }
            else if (dir == 2)
            {
                channel -= 1;
                if (channel <= 0)
                {
                    channel = 20;
                }
                System.out.println(">>> Now showing channel " + channel);            
            }
        }
        else
        {
            System.out.println(">>> TV is off!");
        }
    }
    
    public void change_volume(int dir)
    {
        if (state)
        {
            if (dir == 1)
            {
                volume += 1;
                if (volume > 10)
                {
                    volume = 10;
                }
                System.out.println(">>> Volume now " + volume);
            }
            else if (dir == 2)
            {
                volume -= 1;
                if (volume < 0)
                {
                    volume = 0;
                }
                System.out.println(">>> Volume now " + volume);
            }
        }
        else
        {
            System.out.println(">>> TV is off!");            
        }
    }
}


Remote Class
/**
 * TV Remote Class
 *
 * @author (ARINO JENYNOF)
 * @version (20180920)
 */
import java.util.Scanner;
public class Remote
{
    public static void main(String[] args)
    {
        Television tv = new Television();
        Scanner inp = new Scanner(System.in);
        
        System.out.println("#####################################");
        System.out.println("BLUEJ TV SYSTEM");
        System.out.println("AVAILABLE COMMANDS");
        System.out.println("ON\t: Turn TV on");
        System.out.println("OFF\t: Turn TV off");
        System.out.println("CH #\t: Set channel to #");
        System.out.println("NEXT\t: Change to next channel");
        System.out.println("PREV\t: Change to previous channel");
        System.out.println("VOLUP\t: Increase volume");
        System.out.println("VOLDOWN\t: Decrease volume");
        System.out.println("QUIT\t: Stop watching TV");
        System.out.println("Command is not Case-sensitive");
        System.out.println("#####################################");
        
        String command = inp.next();
        while (true)
        {
            if (command.equalsIgnoreCase("ON"))
            {
                tv.set_state(true);
                System.out.println(">>> TV now on");
            }
            else if (command.equalsIgnoreCase("OFF"))
            {
                tv.set_state(false);
                System.out.println(">>> TV now off");
            }
            else if (command.equalsIgnoreCase("CH"))
            {
                tv.set_channel(inp.nextInt());
            }
            else if (command.equalsIgnoreCase("NEXT"))
            {
                tv.change_channel(1);
            }
            else if (command.equalsIgnoreCase("PREV"))
            {
                tv.change_channel(2);
            }
            else if (command.equalsIgnoreCase("VOLUP"))
            {
                tv.change_volume(1);
            }
            else if (command.equalsIgnoreCase("VOLDOWN"))
            {
                tv.change_volume(2);
            }
            else if (command.equalsIgnoreCase("QUIT"))
            {
                break;
            }
            else
            {
                System.out.println("Invalid Command");
            }
            command = inp.next();
        }
    }
}

Comments