By GokiSoft.com|
16:32 03/07/2023|
Java Basic
[Share Code] Java basic- Overview - viết chương trình điều khiển TIVI bằng java - C2209I
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 com.gokisoft.c2209i;
/**
*
* @author teacher
*/
public class cTivi {
private String[] channelList;
private boolean isOn;
private String currentChannel;
private int currentIndex = -1;
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(channelList.length == 0) return "";
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 ===");
}
void Switch() {
if(isOn) {
Off();
} else {
On();
}
}
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;
isFind = true;
currentChannel = channelList[i];
break;
}
}
if(!isFind) {
System.out.println("Khong tim thay channel: " + channel);
}
}
}
void nextChannel() {
if(isOn) {
seachNextChannel(true);
} else {
System.out.println("=== Tivi OFF ===");
}
}
void previousChannel() {
if(isOn) {
seachNextChannel(false);
} else {
System.out.println("=== Tivi OFF ===");
}
}
void Show() {
if(isOn) {
System.out.println("=== Tivi is on at channel: " + currentChannel);
} else {
System.out.println("=== Tivi is OFF");
}
}
public String[] getChannelList() {
return channelList;
}
public void setChannelList(String[] channelList) {
this.channelList = channelList;
}
public boolean isIsOn() {
return isOn;
}
public void setIsOn(boolean isOn) {
this.isOn = isOn;
}
public String getCurrentChannel() {
return currentChannel;
}
public void setCurrentChannel(String currentChannel) {
this.currentChannel = currentChannel;
}
public int getCurrentIndex() {
return currentIndex;
}
public void setCurrentIndex(int currentIndex) {
this.currentIndex = currentIndex;
}
}
#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 com.gokisoft.c2209i;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
cTivi tivi;
Scanner scan = new Scanner(System.in);
int choose;
System.out.println("1. Khoi tao Tivi mac dinh");
System.out.println("2. Khoi tao Tivi thiet lap kenh");
System.out.println("Chon: ");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
tivi = new cTivi();
break;
default: {
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: ");
String c = scan.nextLine();
channelList[i] = c;
}
tivi = new cTivi(channelList);
break;
}
}
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
tivi.Switch();
break;
case 2:
System.out.println("Chon ken can chuyen: ");
String search = scan.nextLine();
tivi.Switch(search);
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!!!");
break;
}
} while(choose != 8);
}
static void showMenu() {
System.out.println("1. Chuyen trang thai");
System.out.println("2. Chuyen trang thai theo kenh");
System.out.println("3. Bat tivi");
System.out.println("4. Tat tivi");
System.out.println("5. Chuyen kenh tiep");
System.out.println("6. Chuyen kenh truoc");
System.out.println("7. Xem thong tin Tivi");
System.out.println("8. Ke thuc");
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)