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.

121 lines
3.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 智能窗帘类
* 实现SmartDevice和CurtainControl接口
*/
public class SmartCurtain implements SmartDevice, CurtainControl {
private String deviceId;
private String deviceName;
private int position; // 0-1000表示完全关闭100表示完全打开
private boolean isPowerOn;
/**
* 构造函数
* @param deviceId 设备ID
* @param deviceName 设备名称
*/
public SmartCurtain(String deviceId, String deviceName) {
this.deviceId = deviceId;
this.deviceName = deviceName;
this.position = 0; // 默认关闭状态
this.isPowerOn = false;
}
@Override
public String getDeviceId() {
return deviceId;
}
@Override
public String getDeviceName() {
return deviceName;
}
@Override
public String getDeviceType() {
return "智能窗帘";
}
@Override
public String getStatusDescription() {
if (!isPowerOn) {
return "设备已关闭";
}
if (isFullyOpen()) {
return "窗帘完全打开";
}
if (isFullyClosed()) {
return "窗帘完全关闭";
}
return "窗帘打开 " + position + "%";
}
@Override
public void openCurtain() {
if (!isPowerOn) {
System.out.println(deviceName + " 未开启电源,无法操作");
return;
}
this.position = 100;
System.out.println(deviceName + " 窗帘已完全打开");
}
@Override
public void closeCurtain() {
if (!isPowerOn) {
System.out.println(deviceName + " 未开启电源,无法操作");
return;
}
this.position = 0;
System.out.println(deviceName + " 窗帘已完全关闭");
}
@Override
public void setCurtainPosition(int position) {
if (!isPowerOn) {
System.out.println(deviceName + " 未开启电源,无法操作");
return;
}
// 确保位置值在有效范围内
this.position = Math.max(0, Math.min(100, position));
System.out.println(deviceName + " 窗帘位置已设置为 " + this.position + "%");
}
@Override
public int getCurtainPosition() {
return position;
}
@Override
public boolean isFullyOpen() {
return position >= 100;
}
@Override
public boolean isFullyClosed() {
return position <= 0;
}
/**
* 开启设备电源
*/
public void turnOn() {
this.isPowerOn = true;
System.out.println(deviceName + " 电源已开启");
}
/**
* 关闭设备电源
*/
public void turnOff() {
this.isPowerOn = false;
System.out.println(deviceName + " 电源已关闭");
}
/**
* 检查设备电源状态
* @return 如果设备开启则返回true
*/
public boolean isPowerOn() {
return isPowerOn;
}
}