You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.1 KiB
117 lines
3.1 KiB
/**
|
|
* 智能音箱类
|
|
* 实现了SmartDevice, PowerControl和AudioControl接口
|
|
*/
|
|
public class SmartSpeaker implements SmartDevice, PowerControl, AudioControl {
|
|
private String deviceId;
|
|
private String deviceName;
|
|
private boolean isPowerOn;
|
|
private int volume;
|
|
private boolean isPlaying;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param deviceId 设备ID
|
|
* @param deviceName 设备名称
|
|
*/
|
|
public SmartSpeaker(String deviceId, String deviceName) {
|
|
this.deviceId = deviceId;
|
|
this.deviceName = deviceName;
|
|
this.isPowerOn = false;
|
|
this.volume = 30; // 默认音量30%
|
|
this.isPlaying = false;
|
|
}
|
|
|
|
@Override
|
|
public String getDeviceId() {
|
|
return deviceId;
|
|
}
|
|
|
|
@Override
|
|
public String getDeviceName() {
|
|
return deviceName;
|
|
}
|
|
|
|
@Override
|
|
public String getDeviceType() {
|
|
return "Speaker";
|
|
}
|
|
|
|
@Override
|
|
public String getStatusDescription() {
|
|
return deviceName + " (ID: " + deviceId + ") - 状态: " +
|
|
(isPowerOn ? "开启" : "关闭") + ", 音量: " + volume + "%, " +
|
|
(isPlaying ? "正在播放" : "未播放");
|
|
}
|
|
|
|
@Override
|
|
public void turnOn() {
|
|
if (!isPowerOn) {
|
|
isPowerOn = true;
|
|
System.out.println(deviceName + " 已开启");
|
|
} else {
|
|
System.out.println(deviceName + " 已经是开启状态");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void turnOff() {
|
|
if (isPowerOn) {
|
|
isPowerOn = false;
|
|
isPlaying = false; // 关闭电源时停止播放
|
|
System.out.println(deviceName + " 已关闭");
|
|
} else {
|
|
System.out.println(deviceName + " 已经是关闭状态");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean isPowerOn() {
|
|
return isPowerOn;
|
|
}
|
|
|
|
@Override
|
|
public void playAudio() {
|
|
if (isPowerOn) {
|
|
isPlaying = true;
|
|
System.out.println(deviceName + " 开始播放音频");
|
|
} else {
|
|
System.out.println(deviceName + " 未开启电源,无法播放音频");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void pauseAudio() {
|
|
if (isPowerOn && isPlaying) {
|
|
isPlaying = false;
|
|
System.out.println(deviceName + " 暂停播放音频");
|
|
} else {
|
|
System.out.println(deviceName + " 未播放音频,无法暂停");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void stopAudio() {
|
|
if (isPowerOn && isPlaying) {
|
|
isPlaying = false;
|
|
System.out.println(deviceName + " 停止播放音频");
|
|
} else {
|
|
System.out.println(deviceName + " 未播放音频,无法停止");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void setVolume(int volume) {
|
|
if (volume >= 0 && volume <= 100) {
|
|
this.volume = volume;
|
|
System.out.println(deviceName + " 音量已设置为: " + volume + "%");
|
|
} else {
|
|
System.out.println(deviceName + " 音量必须在 0-100% 之间");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getVolume() {
|
|
return volume;
|
|
}
|
|
} |