문제
리모컨의 입력(전원, 상, 하, 좌, 우)에 따라 TV, 에어컨, 로봇청소기를 제어하는 문제입니다 실행순서를 클릭하세요
리모컨의 상, 하, 좌, 우, 전원, 종료 중 하나를 입력하세요 : 전원
TV 전원을 켭니다
에어컨 전원을 켭니다
로봇청소기 전원을 켭니다
리모컨의 상, 하, 좌, 우, 전원, 종료 중 하나를 입력하세요 : 상
현재 채널은 91번입니다
현재 온도는 20도입니다
직진합니다
리모컨의 상, 하, 좌, 우, 전원, 종료 중 하나를 입력하세요 : 종료
프로그램 코드
import java.util.Scanner;
import remoteControl.*;
public class AllApplianceControl
{
1 public static void main (String[] args ) {
Scanner scan = new Scanner( System.in );
2 RemoteControl[] appliance = {
3 new TV(),
4 new AirConditioner(),
5 new RobotCleaner()
};
String result = "";
6 do {
System.out.print( "리모컨의 상, 하, 좌, 우, 전원, 종료 중 하나를 입력하세요 : ");
7 result = scan.next();
8 if ( result.equals( "전원" ) ) {
9 for ( int i = 0; i < appliance.length; i++ ) {
10 appliance[i].clickPower();
}
}
11 else if ( result.equals( "상" ) ) {
12 for ( int i = 0; i < appliance.length; i++ ) {
13 appliance[i].clickUp();
}
}
14 else if ( result.equals( "하" ) ) {
15 for ( int i = 0; i < appliance.length; i++ ) {
16 appliance[i].clickDown();
}
}
17 else if ( result.equals( "좌" ) ) {
18 for ( int i = 0; i < appliance.length; i++ ) {
19 appliance[i].clickLeft();
}
}
20 else if ( result.equals( "우" ) ) {
21 for ( int i = 0; i < appliance.length; i++ ) {
22 appliance[i].clickRight();
}
}
23 } while( result.equals( "전원" ) || result.equals( "상" ) || result.equals( "하" ) || result.equals( "좌" ) || result.equals( "우" ) );
24 }
}
package remoteControl;
public interface RemoteControl
{
boolean ON = true, OFF = false;
void clickPower();
void clickUp();
void clickDown();
void clickLeft();
void clickRight();
}
package remoteControl;
import java.util.Scanner;
public class TV implements RemoteControl
{
private boolean power;
private int channel;
private int volume;
T1b public TV() {
this.power = OFF;
this.channel = 90;
this.volume = 10;
T1e }
@Override
T2b public void clickPower() {
if( this.power == OFF ) {
this.power = ON;
System.out.println( "TV 전원을 켭니다" );
}
else {
this.power = OFF;
System.out.println( "TV 전원을 끕니다" );
}
T2e }
@Override
T3b public void clickUp() {
System.out.println( "현재 채널은 " + ( ++this.channel ) + "번입니다" );
T3e }
@Override
T4b public void clickDown() {
System.out.println( "현재 채널은 " + ( --this.channel ) + "번입니다" );
T4e }
@Override
T5b public void clickLeft() {
System.out.println( "현재 음량은 " + ( --this.volume ) + "입니다" );
T5e }
@Override
T6b public void clickRight() {
System.out.println( "현재 음량은 " + ( ++this.volume ) + "입니다" );
T6e }
}
package remoteControl;
public class AirConditioner implements RemoteControl
{
private boolean power;
private int temperature, windStrength;
A1b public AirConditioner() {
this.power = OFF;
this.temperature = 19;
this.windStrength = 5;
A1e }
@Override
A2b public void clickPower() {
if( this.power == OFF ) {
this.power = ON;
System.out.println( "에어컨 전원을 켭니다" );
}
else {
this.power = OFF;
System.out.println( "에어컨 전원을 끕니다" );
}
A2e }
@Override
A3b public void clickUp() {
System.out.println( "현재 온도는 " + ( ++this.temperature ) + "도입니다" );
A3e }
@Override
A4b public void clickDown() {
System.out.println( "현재 온도는 " + ( --this.temperature ) + "도입니다" );
A4e }
@Override
A5b public void clickLeft() {
System.out.println( "현재 풍량은 " + ( --this.windStrength ) + "입니다" );
A5e }
@Override
A6b public void clickRight() {
System.out.println( "현재 풍량은 " + ( ++this.windStrength ) + "입니다" );
A6e }
}
package remoteControl;
public class RobotCleaner implements RemoteControl
{
private boolean power;
R1b public RobotCleaner() {
this.power = OFF;
R1e }
@Override
R2b public void clickPower() {
if( this.power == OFF ) {
this.power = ON;
System.out.println( "로봇청소기 전원을 켭니다" );
}
else {
this.power = OFF;
System.out.println( "로봇청소기 전원을 끕니다" );
}
R2e }
@Override
R3b public void clickUp() {
System.out.println( "직진합니다" );
R3e }
@Override
R4b public void clickDown() {
System.out.println( "후진합니다" );
R4e }
@Override
R5b public void clickLeft() {
System.out.println( "좌회전합니다" );
R5e }
@Override
R6b public void clickRight() {
System.out.println( "우회전합니다" );
R6e }
}