By GokiSoft.com|
14:45 10/10/2022|
Java Basic
[Source Code] Java basic- Overview - viết chương trình điều khiển TIVI bằng java - C2109I
Java basic- Overview - viết chương trình điều khiển TIVI bằng java
#CTivi.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package bt999;
/**
*
* @author diepvan
*/
public class CTivi {
String[] channelList;
boolean isOn;
String currentChannel;
int currentIndex = 0;
public CTivi() {
isOn = false;
channelList = new String[]{"VTV1", "VTV2", "VTV3"};
currentChannel = "VTV1";
}
public CTivi(String[] channelList) {
this.channelList = channelList;
isOn = false;
if(this.channelList.length > 0) {
currentChannel = channelList[0];
}
}
String searchNextChannel(boolean isForward) {
if(channelList.length == 0) return null;
if(isForward) {
currentIndex++;
if(currentIndex >= channelList.length) {
currentIndex = 0;
}
} else {
currentIndex--;
if(currentIndex < 0) {
currentIndex = channelList.length - 1;
}
}
currentChannel = channelList[currentIndex];
return currentChannel;
}
void On() {
isOn = true;
System.out.println("-- Tivi ON --");
}
void Off() {
isOn = false;
System.out.println("-- Tivi Off --");
}
public void Switch() {
if(isOn) {
Off();
} else {
On();
}
}
/**
* overloading & override
* @param channel
*/
public void Switch(String channel) {
Switch();
if(isOn) {
System.out.println("Bat dau tim kenh ...");
boolean isFind = false;
for (int i = 0; i < channelList.length; i++) {
if(channelList[i].equalsIgnoreCase(channel)) {
currentChannel = channelList[i];
currentIndex = i;
isFind = true;
break;
}
}
if(!isFind) {
System.out.println("Khong tim thay kenh phu hop");
}
}
}
public void nextChannel() {
if(isOn) {
searchNextChannel(true);
System.out.println("Kenh hien tai: " + currentChannel);
}
}
public void previousChannel() {
if(isOn) {
searchNextChannel(false);
System.out.println("Kenh hien tai: " + currentChannel);
}
}
public void Show() {
if(isOn) {
System.out.println("-- Tivi is ON channel: " + currentChannel);
} else {
System.out.println("-- Tivi is OFF! --");
}
}
}
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package bt999;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Main {
public static void main(String[] args) {
CTivi tivi;
Scanner scan = new Scanner(System.in);
System.out.println("1. Khoi tao mac dinh");
System.out.println("2. Khoi tao tu thiet lap");
System.out.println("Chon: ");
int choose = Integer.parseInt(scan.nextLine());
if(choose == 1) {
tivi = new CTivi();
} else {
System.out.println("Nhap so kenh can them: ");
int N = Integer.parseInt(scan.nextLine());
String[] channelList = new String[N];
for (int i = 0; i < N; i++) {
System.out.println("Nhap kenh: ");
channelList[i] = scan.nextLine();
}
tivi = new CTivi(channelList);
}
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("Thoat!!!");
break;
default:
System.out.println("Nhap sai!!!");
}
} while(choose != 8);
}
static void showMenu() {
System.out.println("1. Chuyen trang thai");
System.out.println("2. Chuyen trang thai kem theo kenh");
System.out.println("3. Bat tivi");
System.out.println("4. Tat tivi");
System.out.println("5. Chuyen kenh tiep theo");
System.out.println("6. Chuyen kenh trc do");
System.out.println("7. Xem tivi");
System.out.println("8. Thoat");
System.out.println("Chon: ");
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)