클래스 확장하기
1. 스마트 홈 디바이스 기능확장
1-1. main class -> app
public class SmartHomeApp {
public static void main(String[] args) {
SmartHomeController controller = new SmartHomeController();
controller.loadDevicesFromFile("device.dat");
controller.addDevice(new SmartLight("거실 전등"));
controller.addDevice(new SmartLight("주방전등"));
controller.turnAllOn();
controller.runAllFeature();
controller.showAllStatus();
controller.showListNumber();
controller.saveDevicesToFile("device.dat");
}
}
1-2. app에서의 기능적 역할을 모아둔 controller
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class SmartHomeController {
private List<SmartDevice> devices;
private static final long serialVersionUID = 1L;
public SmartHomeController(){
devices = new ArrayList<>();
}
public void addDevice(SmartDevice device){
devices.add(device);
System.out.printf("[%s]기기가 등록되었습니다.\n", device.getName());
}
public void turnAllOn(){
System.out.println("등록된 모든 기기의 전원이 켜집니다.");
for (SmartDevice i : devices){
i.turnOn();
}
}
public void turnAllOff(){
System.out.println("등록된 모든 기기의 전원이 끕니다.");
for (SmartDevice i : devices){
i.turnOff();
}
}
public void runAllFeature(){
System.out.println("등록된 모든 기기의 고유기능이 켜집니다.");
for (SmartDevice i : devices){
i.runfeature();
}
}
public void showAllStatus(){
System.out.println("등록된 모든 기기의 현재 상태를 확인합니다.");
for (SmartDevice i : devices){
i.showStatus();
}
}
public void showListNumber(){
System.out.println(devices.size());
}
public void saveDevicesToFile(String filename){
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename))){
out.writeObject(devices);
System.out.println("[파일 저장 완료]");
}
catch (IOException e){
e.printStackTrace();
}
}
public void loadDevicesFromFile(String filename){
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename))){
devices = (List<SmartDevice>) in.readObject();
System.out.println("[파일 불러오기 완료]");
}
catch (IOException | ClassNotFoundException e){
e.printStackTrace();
}
}
}
1-3. 스마트 디바이스들의 공통 기능을 정의한 smartdevice
import java.io.Serializable ;
public abstract class SmartDevice implements Serializable {
private String name;
private boolean isOn;
SmartDevice(String name){
this.name = name;
this.isOn = false;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean getIsOn() {
return this.isOn;
}
public void setIsOn(boolean isOn) {
this.isOn = isOn;
}
public void turnOn(){
this.isOn = true;
System.out.printf("[%s]가 켜졌습니다.\n", this.name);
}
public void turnOff(){
this.isOn = false;
System.out.printf("[%s]가 꺼졌습니다.\n", this.name);
}
public void showStatus(){
System.out.printf("[%s]의 상태: %s입니다.\n", this.name, this.isOn ? "ON" : "OFF");
}
public abstract void runfeature();
}
1-4. 스마트 전등
public class SmartLight extends SmartDevice {
SmartLight(String name){
super(name);
}
public void changeColor(String color){
if (!getIsOn()){
System.out.printf("%s가 꺼져있어 색상을 변경할 수 없습니다.\n", getName());
}
else {
System.out.printf("%s의 색상이 %s로 변경되었습니다.\n", getName(), color);
}
}
@Override
public void runfeature() {
changeColor("파란색");
}
}
1-5. 스마트 스피커
public class SmartSpeaker extends SmartDevice {
SmartSpeaker(String name){
super(name);
}
public void playMusic(String song){
if (!getIsOn()){
System.out.printf("%s가 꺼져있어 음악을 틀 수 없습니다.\n", getName());
}
else {
System.out.printf("%s에서 %s를 재생합니다.\n", getName(), song);
}
}
@Override
public void runfeature() {
playMusic("Hip-Hop");
}
}
1-6. 스마트 온도 조절기
public class SmartThermostat extends SmartDevice {
SmartThermostat(String name){
super(name);
}
public void setTemperature(int degree){
if (!getIsOn()){
System.out.printf("%s가 꺼져있어 온도를 조절할 수 없습니다.\n", getName());
}
else {
System.out.printf("%s가 온도를 %d로 조정합니다.\n", getName(), degree);
}
}
@Override
public void runfeature() {
setTemperature(28);
}
}