By GokiSoft.com| 09:12 19/07/2021|
Java Basic

[Share Code] Java basic- Overview - viết chương trình điều khiển TIVI bằng java - C2010G

Java basic- Overview - viết chương trình điều khiển TIVI bằng java


#Program.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson07.bt999;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Program {
    public static void main(String[] args) {
        CTivi tivi;
        Scanner scan = new Scanner(System.in);
        int choose, n;
        
        System.out.println("1. Khoi tao TV mac dinh");
        System.out.println("2. Chon kenh");
        System.out.println("Chon: ");
        choose = Integer.parseInt(scan.nextLine());
        
        switch(choose) {
            case 1:
                tivi = new CTivi();
                break;
            default:
                System.out.println("Nhap vao so kenh: ");
                n = Integer.parseInt(scan.nextLine());
                String[] channelList = new String[n];
                
                for (int i = 0; i < n; i++) {
                    System.out.printf("\nNhap kenh (%d)", i + 1);
                    channelList[i] = scan.nextLine();
                }
                
                tivi = new CTivi(channelList);
                break;
        }
        
        //Phat trien menu chuong trinh
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    tivi.Switch();
                    break;
                case 2:
                    System.out.println("Nhap kenh can chuyen: ");
                    String channel = scan.nextLine();
                    
                    tivi.Switch(channel);
                    break;
                case 3:
                    tivi.On();
                    break;
                case 4:
                    tivi.Off();
                    break;
                case 5:
                    tivi.nextChannel();
                    break;
                case 6:
                    tivi.previousChannel();
                    break;
                case 7:
                    tivi.Show();
                    break;
                case 8:
                    System.out.println("Goodbye!!!");
                    break;
                default:
                    System.out.println("Input failed!!!");
                    break;
            }
        } while(choose != 8);
    }
    
    static void showMenu() {
        System.out.println("1. Switch");
        System.out.println("2. Switch by channel");
        System.out.println("3. On");
        System.out.println("4. Off");
        System.out.println("5. Next channel");
        System.out.println("6. Previous channel");
        System.out.println("7. Show");
        System.out.println("8. Exit");
        System.out.println("Choose: ");
    }
}


#CTivi.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson07.bt999;

/**
 *
 * @author Diep.Tran
 */
public class CTivi {
    String[] channelList;
    boolean isOn;
    String currentChannel;
    int currentIndex;

    public CTivi() {
        isOn = false;
        channelList = new String[] {"VTV1", "VTV2", "VTV3"};
        currentChannel = "VTV1";
        currentIndex = 0;
    }

    public CTivi(String[] channelList) {
        this.channelList = channelList;
        isOn = false;
        if(channelList.length > 0) {
            currentChannel = channelList[0];
        }
        currentIndex = 0;
    }
    
    private String seachNextChannel(boolean isForward) {
        if(isForward) {
            currentIndex++;
            if(currentIndex >= channelList.length) {
                currentIndex = 0;
            }
        } else {
            currentIndex--;
            if(currentIndex < 0) {
                currentIndex = channelList.length - 1;
            }
        }
        return channelList[currentIndex];
    }
    
    public void On() {
        isOn = true;
        System.out.println("-- TV ON --");
    }
    
    public void Off() {
        isOn = false;
        System.out.println("-- TV OFF --");
    }
    
    public void Switch() {
        if(isOn) {
            Off();
        } else {
            On();
        }
    }
    
    public void Switch(String channel) {
        Switch();
        
        if(isOn) {
            boolean isFind = false;
            for (int i = 0; i < channelList.length; i++) {
                if(channelList[i].equalsIgnoreCase(channel)) {
                    currentIndex = i;
                    currentChannel = channelList[i];
                    System.out.println("Tim thay kenh");
                    isFind = true;
                    break;
                }
            }
            if(!isFind) {
                System.out.println("Khong tim thay kenh");
            }
        }
    }
    
    public void nextChannel() {
        if(isOn) {
            currentChannel = seachNextChannel(true);
            System.out.println("Current Channel: " + currentChannel);
        }
    }
    
    public void previousChannel() {
        if(isOn) {
            currentChannel = seachNextChannel(false);
            System.out.println("Current Channel: " + currentChannel);
        }
    }
    
    public void Show() {
        if(isOn) {
            System.out.println("-- Tivi is On at channel: " + currentChannel);
        } else {
            System.out.println("-- Tivi now OFF! --");
        }
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)