abstract class Device {
String id;
double resistance;
Device(String id) {
this.id = id;
}
abstract String getStatus();
abstract double getResistance();
}
class Switch extends Device {
boolean state; // false代表turned on,true代表closed
Switch(String id) {
super(id);
this.state = false;
this.resistance = 0;
}
//切换开关
void toggle() {
this.state = !this.state;
}
@Override
String getStatus() {
return state ? "closed" : "turned on";
}
@Override
double getResistance(){
return this.resistance;
}
}
// 分档调速器类
class SteppedRegulator extends Device {
int currentLevel = 0;
int maxLevel = 3;
double[] outputVoltageRatios = {0.0, 0.3, 0.6, 0.9};
SteppedRegulator(String id) {
super(id);
this.resistance = 0;
}
void increase() {
if (currentLevel < maxLevel) {
currentLevel++;
}
}
void decrease() {
if (currentLevel > 0) {
currentLevel--;
}
}
double getOutputVoltage() {
return outputVoltageRatios[currentLevel];
}
@Override
String getStatus() {
return String.valueOf(currentLevel);
}
@Override
double getResistance(){
return this.resistance;
}
}
// 连续调速器类
class ContinuousRegulator extends Device {
double level = 0.00;
ContinuousRegulator(String id) {
super(id);
this.resistance = 0;
}
void setLevel(double level) {
this.level = Math.floor(level * 100) / 100; // 保留两位小数
}
@Override
String getStatus() {
return String.format("%.2f", level);
}
@Override
double getResistance(){
return this.resistance;
}
}
// 灯类
class Lamp extends Device {
int brightness = 0;
Lamp(String id) {
super(id);
if (id.startsWith("B")) {
this.resistance = 10;
}else if (id.startsWith("R")) {
this.resistance = 5;
}
}
void setBrightness(double voltage) {
if (id.startsWith("B")) {
// 白炽灯
if (voltage <= 9) {
brightness = 0;
} else {
brightness = (int) ((15.0 / 21.0) * (voltage - 10) + 50);
}
} else if (id.startsWith("R")) {
// 日光灯
brightness = voltage > 0 ? 180 : 0;
}
}
@Override
String getStatus() {
return String.valueOf(brightness);
}
@Override
double getResistance(){
return this.resistance;
}
}
// 风扇类
class Fan extends Device {
int speed = 0;
Fan(String id) {
super(id);
this.resistance = 20;
}
void setSpeed(double voltage) {
if (voltage < 80) {
speed = 0;
} else if (id.startsWith("D")) {
// 吊扇
if (voltage >= 150) {
speed = 360;
} else {
speed = (int) ((double) (voltage - 80) / 70 * 280 + 80);
}
} else if (id.startsWith("A")) {
// 落地扇
if (voltage >= 140) {
speed = 360;
} else if (voltage >= 120) {
speed = 260;
} else if (voltage >= 100) {
speed = 160;
} else {
speed = 80;
}
}
}
@Override
String getStatus() {
return String.valueOf(speed);
}
@Override
double getResistance(){
return this.resistance;
}
}
class SelectorSwitch extends Device {
boolean state;
private static final TreeMap<String, SelectorSwitch> H = new TreeMap<>();
SelectorSwitch(String id, String Pin) {
super(id);
if (Pin.equals("2")){
this.resistance = 5;
this.state = true;
H.put(id, this);
}
if (Pin.equals("3")){
this.resistance = 10;
this.state = false;
}
}
public static TreeMap<String, SelectorSwitch> getH() {
return H;
}
void toggle() {
this.state = !this.state;
}
@Override
String getStatus() {
return state ? "closed" : "turned on";
}
@Override
double getResistance(){
return this.resistance;
}
}
//
class CurtainController extends Device{
int openPercentage = 100;
double voltage;
public void setVoltage(double voltage1) {
voltage = voltage1;
}
public double getVoltage() {
return voltage;
}
CurtainController(String id) {
super(id);
this.resistance = 15;
}
void setOpenPercentage(double voltage, double lightIntensity) {
if (voltage < 50) {
openPercentage = 100; // 电压低于50V,窗帘不工作,全开状态
} else {
if (lightIntensity < 50) {
openPercentage = 100;
} else if (lightIntensity < 100) {
openPercentage = 80;
} else if (lightIntensity < 200) {
openPercentage = 60;
} else if (lightIntensity < 300) {
openPercentage = 40;
} else if (lightIntensity < 400) {
openPercentage = 20;
} else {
openPercentage = 0;
}
}
}
@Override
String getStatus() {
return openPercentage + "%";
}
@Override
double getResistance(){
return this.resistance;
}
}
//串联类
class Concatenation extends Device{
final Pattern pattern_C = Pattern.compile("#(T\d+)\:\[(IN|VCC) [A-Z]\d+-\d\]( \[[A-Z]\d+-(\d|OUT) [A-Z]\d+-(\d|IN)\])+ \[[A-Z]\d+-(\d|OUT) (OUT|GND)\]");
private final LinkedHashMap<String, Device> concatenationLinkedHashMap;
private static final TreeMap<String, Switch> K = new TreeMap<>();
private static final TreeMap<String, SteppedRegulator> F = new TreeMap<>();
private static final TreeMap<String, ContinuousRegulator> L = new TreeMap<>();
private static final TreeMap<String, Lamp> B_R = new TreeMap<>();
private static final TreeMap<String, Fan> D_A = new TreeMap<>();
private static final TreeMap<String, CurtainController> S = new TreeMap<>();
double voltage;
public void setVoltage(double voltage1) {
voltage = voltage1;
}
public double getVoltage() {
return voltage;
}
Concatenation(String id) {
super(id);
concatenationLinkedHashMap = new LinkedHashMap<>();
}
public LinkedHashMap<String, Device> getConcatenationLinkedHashMap(){
return concatenationLinkedHashMap;
}
public TreeMap<String, Switch> getK(){
return K;
}
public TreeMap<String, SteppedRegulator> getF(){
return F;
}
public TreeMap<String, ContinuousRegulator> getL(){
return L;
}
public TreeMap<String, Lamp> getB_R(){
return B_R;
}
public TreeMap<String, Fan> getD_A(){
return D_A;
}
public TreeMap<String, CurtainController> getS(){
return S;
}
public LinkedHashMap<String, Device> processLines(String T, Concatenation concatenation, LinkedHashMap<String, Concatenation> ConcatenationMap, LinkedHashMap<String, Parallel> ParallelLinkedHashMap) {
Matcher matcher_C = pattern_C.matcher(T);
if(matcher_C.find()){
Pattern pattern_T = Pattern.compile("\\[(.*?) (.*?)\\]");
Matcher matcher_T = pattern_T.matcher(T);
while (matcher_T.find()) {
String[] parts = matcher_T.group(2).split("-");
String[] parts1 = matcher_T.group(1).split("-");
String name =parts[0];
String name1 = parts1[0];
String Pin = "";
String Pin1 = "";
if (parts.length > 1 && parts[1] != null && !parts[1].isEmpty()) {
Pin = parts[1];
}
// 判断并赋值给 Pin1
if (parts1.length > 1 && parts1[1] != null && !parts1[1].isEmpty()) {
Pin1 = parts1[1];
}
if(name1.startsWith("H")) {
if(Pin1.equals("2") || Pin1.equals("3")){
SelectorSwitch selectorSwitch = new SelectorSwitch(name1,Pin1);
concatenation.concatenationLinkedHashMap.put(name1, selectorSwitch);
}
}
if(name.startsWith("K")) {
Switch mySwitch = new Switch(name);
concatenation.concatenationLinkedHashMap.put(name, mySwitch);
K.put(name, mySwitch);
}else if (name.startsWith("F")) {
SteppedRegulator steppedRegulator = new SteppedRegulator(name);
concatenation.concatenationLinkedHashMap.put(name, steppedRegulator);
F.put(name, steppedRegulator);
}else if (name.startsWith("L")) {
ContinuousRegulator continuousRegulator = new ContinuousRegulator(name);
concatenation.concatenationLinkedHashMap.put(name, continuousRegulator);
L.put(name, continuousRegulator);
}else if (name.startsWith("B")) {
Lamp lamp = new Lamp(name);
concatenation.concatenationLinkedHashMap.put(name, lamp);
B_R.put(name, lamp);
}else if (name.startsWith("R")) {
Lamp lamp = new Lamp(name);
concatenation.concatenationLinkedHashMap.put(name, lamp);
B_R.put(name, lamp);
}else if (name.startsWith("D")) {
Fan fan = new Fan(name);
concatenation.concatenationLinkedHashMap.put(name, fan);
D_A.put(name, fan);
}else if (name.startsWith("A")) {
Fan fan = new Fan(name);
concatenation.concatenationLinkedHashMap.put(name, fan);
D_A.put(name, fan);
}else if(name.startsWith("H")) {
if(Pin.equals("2") || Pin.equals("3")){
SelectorSwitch selectorSwitch = new SelectorSwitch(name,Pin);
concatenation.concatenationLinkedHashMap.put(name, selectorSwitch);
}
}else if(name.startsWith("S")) {
CurtainController curtainController = new CurtainController(name);
concatenation.concatenationLinkedHashMap.put(name, curtainController);
S.put(name, curtainController);
}else if (name.startsWith("M")) {
concatenation.concatenationLinkedHashMap.put(name, ParallelLinkedHashMap.get(name));
}else if (name.startsWith("T")) {
concatenation.concatenationLinkedHashMap.put(name, ConcatenationMap.get(name));
}
}
this.resistance = ResistanceCalculator(concatenation);
}
return concatenationLinkedHashMap;
}
public double ResistanceCalculator(Concatenation concatenation){
for (Map.Entry<String, Device> entry : concatenation.concatenationLinkedHashMap.entrySet()) {
String deviceId = entry.getKey();
Device device = entry.getValue();
resistance += device.resistance;
}
return resistance;
}
public void setResistance(double resistance){
this.resistance = resistance;
}
@Override
String getStatus() {
return null;
}
@Override
double getResistance() {
return 0;
}
}
//并联类
class Parallel extends Device{
final Pattern pattern_P = Pattern.compile("#M\d+\:\[(T\d+)( T\d+)+\]");
private final LinkedHashMap<String, Concatenation> parallelLinkedHashMap;
Parallel(String id) {
super(id);
parallelLinkedHashMap = new LinkedHashMap<>();
}
public LinkedHashMap<String, Concatenation> getParallelLinkedHashMap() {
return parallelLinkedHashMap;
}
public void setResistance(double resistance){
this.resistance = resistance;
}
public double processLines(String M, LinkedHashMap<String, LinkedHashMap<String, Device>> deviceMap, LinkedHashMap<String, Concatenation> ConcatenationMap) {
double resistance = 0;
Matcher matcher_P = pattern_P.matcher(M);
if (matcher_P.find()){
Pattern pattern_M = Pattern.compile("T\\d+");
Matcher matcher_M = pattern_M.matcher(M);
while (matcher_M.find()) {
double resistance1 = 0;
if (deviceMap.containsKey(matcher_M.group())) {
parallelLinkedHashMap.put(matcher_M.group(), ConcatenationMap.get(matcher_M.group()));
LinkedHashMap<String, Device> innerMap = deviceMap.get(matcher_M.group());
for (Map.Entry<String, Device> entry : innerMap.entrySet()) {
String innerKey = entry.getKey();
Device device = entry.getValue();
resistance1 += device.getResistance();
// 在这里使用innerKey和device进行处理
}
resistance += 1 / resistance1;
}
}
resistance = 1 / resistance;
}
return resistance;
}
@Override
String getStatus() {
return null;
}
@Override
double getResistance(){
return resistance;
}
}
标签:return,String,double,resistance,name,Blog,id,第三次 From: https://www.cnblogs.com/yushang257368419/p/18277005