/
anton.buldygin
/
Contacts_Java
Обзор
Документация
Войти
/
anton.buldygin
/
Contacts_Java
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Topics/Command/Remote control/src/Main.java
138 строк
3 KB
AntonBuldygin
Initial commit
19 дек 2023, 15:38
19 дек 2023, 15:38
bed9366
Код
Авторство
О чём код?
import java.util.Scanner; class Client { public static void main(String[] args) { Controller controller = new Controller(); TV tv = new TV(); int[] channelList = new int[3]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < 3; i++) { channelList[i] = scanner.nextInt(); } Command turnOnTV = new TurnOnCommand(tv); /* write your code here */ controller.setCommand(turnOnTV); controller.executeCommand(); Command changeChannel; for (int i = 0; i < 3; i++) { /* write your code here */ changeChannel = new ChangeChannelCommand(new Channel(tv,channelList[i])); controller.setCommand(changeChannel); controller.executeCommand(); } Command turnOffTV = new TurnOffCommand(tv); controller.setCommand(turnOffTV); controller.executeCommand(); /* write your code here */ } } class TV { Channel channel; void turnOn() { System.out.println("Turning on the TV"); setChannel(new Channel(this, 0)); } void turnOff() { /* write your code here */ System.out.println("Turning off the TV"); } void setChannel(Channel channel) { this.channel = channel; } } class Channel { private TV tv; private int channelNumber; Channel(TV tv, int channelNumber) { /* write your code here */ this.tv =tv; this.channelNumber =channelNumber; } void changeChannel() { tv.setChannel(this); System.out.println("Channel was changed to " + channelNumber); } } interface Command { /* write your code here */ void execute(); } class TurnOnCommand implements Command { /* write your code here */ TV tv; TurnOnCommand(TV tv) { this.tv = tv; } @Override public void execute() { /* write your code here */ tv.turnOn(); } } class TurnOffCommand implements Command { /* write your code here */ TV tv; TurnOffCommand(TV tv) { this.tv = tv; } @Override public void execute() { /* write your code here */ tv.turnOff(); } } class ChangeChannelCommand implements Command { private Channel channel; ChangeChannelCommand(Channel channel) { this.channel = channel; } @Override /* write your code here */ public void execute() { channel.changeChannel(); } } class Controller { private Command command; void setCommand(Command command) { this.command = command; } void executeCommand() { /* write your code here */ command.execute(); } }