TV 컨트롤 프로그램을 만들어보시오
public class TV {
boolean power = false;
int volume = 5;
int channel = 1;
final int MIN_CHANNEL = 1;
final int MAX_CHANNEL = 100;
final int MIN_VOLUME = 0;
final int MAX_VOLUME = 10;
void powerMethod(){
power = !power;
System.out.println(power ? "TV 켜짐" : "TV 꺼짐");
}
void changeChannel(int channel){
if(power){
if(MIN_CHANNEL <= channel && channel <= MAX_CHANNEL){
this.channel = channel;
//전역변수와 지역변수를 비교하기 위해 전역변수 앞에 this
}
System.out.println("채널. " + this.channel);
}
}
void channelUp(){
changeChannel(channel + 1); //코드를 재사용 한다.
}
void channelDown(){
changeChannel(channel - 1);
}
void volumeUp(){
if(power){
if(volume < MAX_VOLUME){
volume++;
}
showVolume();
}
}
void voluemeDown(){
if(power){
if(MIN_VOLUME < volume){
volume--;
}
showVolume();
}
}
void showVolume(){
System.out.print("음량. ");
for(int i = MIN_VOLUME + 1; i <= MAX_VOLUME; i++){
if(i <= volume){
System.out.print("■");
}else{
System.out.print("□");
}
}
System.out.println();
}
public static void main(String[] args) {
TV tv = new TV();
while(true){
System.out.println("--------------------------");
System.out.println("1.전원 2.채널변경 3.채널업 4.채널다운");
System.out.println("5.볼륨업 6.불륨다운 0.종료");
System.out.println("--------------------------");
System.out.println("번호 입력>");
int input = ScanUtil.nextInt();
switch(input){
case 1: tv.powerMethod(); break;
case 2:
System.out.println("변경할 채널 입력>");
int ch = ScanUtil.nextInt();
tv.changeChannel(ch);
break;
case 3: tv.channelUp(); break;
case 4: tv.channelDown(); break;
case 5: tv.volumeUp(); break;
case 6: tv.voluemeDown(); break;
case 0:
System.out.println("종료되었습니다.");
System.exit(0);
}
}
}