NCHU-OOP课程与PTA七到八周作业源代码

NCHU-OOP课程与PTA七到八周作业源代码

PTA-oop-7


家居强电电路模拟程序-3:

点击查看代码
package oopPrograms7;

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

        int isEnd = 0;
        String input = "";
        Controller controller = new Controller();

        //循环输入,遇end结束
        while(true) {

            input = in.nextLine();
            controller.getInput().setInput(input);

            switch(controller.getInput().judgeInput()) {
                case 1:
                	controller.inputCircuit();
                    break;
                case 2:
                	controller.inputSeriesCircuit();
                    break;
                case 3:
                    controller.inputCircuitDiagram();
                    break;
                case 4:
                    controller.operation();
                    break;
                case 5:
                	
                	break;
                case -1:
                    isEnd = 1;
                    break;
                default:
//                  controller.showWrongFormat();
                    break;
            }

            //判断是否需要退出输入
            if(isEnd == 1)
                break;

        }

        controller.showInfo();
        
	}

}

//抽象电子元件类
abstract class AbstractElectronic implements Comparable<AbstractElectronic>{
	
	private String name = "";  
	private int num = 0;
	private double current = 0;
	private double v = 0;
	private double vBefore = 0;
	private double vAfter = 0;
	private double r = 0;
	private String state = "";
	
	public String getName() {
		return name;
	}

	public void setName(String newName) {
		name = newName;
	}

	public double getCurrent() {
		return current;
	}

	public void setCurrent(double newCurrent) {
		current = newCurrent;
	}

	public double getV() {
		return v;
	}

	public void setV(double newV) {
		v = newV;
	}

	public double getVBefore() {
		return vBefore;
	}

	public void setVBefore(double newVBefore) {
		vBefore = newVBefore;
	}

	public double getVAfter() {
		return vAfter;
	}

	public void setVAfter(double newVAfter) {
		vAfter = newVAfter;
	}

	public double getR() {
		return r;
	}

	public void setR(double newR) {
		r = newR;
	}
	
	public int getNum() {
		return num;
	}
	
	public void setNum(int num) {
		this.num = num;
	}

	public abstract void renew();

	public String getState() {
		return state;
	}
	
	public void setState(String state) {
		this.state = state;
	}
	
	@Override
	public int compareTo(AbstractElectronic o) {
//		return this.num - o.num;
		return this.name.compareTo(o.name);
	}
	
}

//抽象电灯类
abstract class AbstractLight extends AbstractElectronic {

	private double brightness = 0;

	public double getBrightness() {
		return brightness;
	}

	public void setBrightness(double newBrightness) {
		brightness = newBrightness;
	}

}

//抽象风扇类
abstract class AbstractFan extends AbstractElectronic {

	private double speed = 0;

	public double getSpeed() {
		return speed;
	}

	public void setSpeed(double newSpeed) {
		speed = newSpeed;
	}

}

//抽象开关
abstract class AbstractSwitch extends AbstractElectronic {

	//开关为0时断开
	//开关初始断开
	private int switchStatus = 0;

	public abstract void change();

	public int getSwitchStatus() {
		return switchStatus;
	}

	public void setSwitchStatus(int newSwitchStatus) {
		switchStatus = newSwitchStatus;
	}

}

//抽象连续控制器
abstract class AbstractContinuousGovernor extends AbstractElectronic {

	private double gear = 0;

	public abstract void change(double gear);

	public double getGear() {
		return gear;
	}

	public void setGear(double gear2) {
		gear = gear2;
	}

}

//抽象分档调节器
abstract class AbstractBinningGovernor extends AbstractElectronic {

	private int gear = 0;

	public abstract void gearUp();

	public abstract void gearDown();

	public int getGear() {
		return gear;
	}

	public void setGear(int newGear) {
		gear = newGear;
	}
	
	
}

//抽象窗帘
abstract class AbstractCurtain extends AbstractElectronic {

	private double brightness = 0;
	private double gear = 0;

	public double getBrightness() {
		return brightness;
	}
	
	public void setBrightness(double brightness) {
		this.brightness = brightness;
	}
	
	public double getGear() {
		return gear;
	}

	public void setGear(double newGear) {
		gear = newGear;
	}
	
	
}

//白炽灯
class Incandescent extends AbstractLight {
	
	public void renew() {
		
		//首先更新电压
		setV(getVBefore() - getVAfter());
		
		if(getV() < 10) {
			setBrightness(0);
		}
		else {
			//这里有问题!!!整数做除法一定要和浮点数除!!!210改成210.0!
			setBrightness((getV() - 10) * 150 / 210.0 + 50);
		}
		
		//更新状态
		setState(String.format("%d", (int)getBrightness()));
		
	}

}

//日光灯
class Fluorescent extends AbstractLight {
	
	public void renew() {
		
		//首先更新电压
		setV(getVBefore() - getVAfter());
		
		//更新亮度
		if(this.getV() > 0) {
			setBrightness(180);
		}
		else {
			setBrightness(0);
		}
		
		//更新状态
		setState(String.format("%d", (int)getBrightness()));
		
	}

}

//风扇
class Fan extends AbstractFan {

	public void renew() {
		
		//首先更新电压
		setV(getVBefore() - getVAfter());
		
		//转速
		if(getV() > 150) {
			setSpeed(360);
		}
		else if(getV() >= 80 && getV() <= 150) {
			setSpeed((getV() - 80) * 280 / 70.0 + 80);
		}
		else {
			setSpeed(0);
		}
		
		//更新状态
		setState(String.format("%d",(int)getSpeed()));
		
	}

}

//落地扇
class FloorFan extends AbstractFan {

	public void renew() {
		
		//首先更新电压
		setV(getVBefore() - getVAfter());
		
		//转速
		if(getV() >= 80 && getV() <= 99) {
			setSpeed(80);
		}
		else if(getV() >= 100 && getV() <= 119) {
			setSpeed(160);
		}
		else if(getV() >= 120 && getV() <= 139) {
			setSpeed(260);
		}
		else if(getV() > 140) {
			setSpeed(360);
		}
		else {
			setSpeed(0);
		}
		
		//更新状态
		setState(String.format("%d",(int)getSpeed()));
		
	}

}

//开关
class Switch extends AbstractSwitch {

	public void change() {
		setSwitchStatus((getSwitchStatus() + 1) % 2);
		renew();
	}

	public void renew() {
		
		//断开即输出电压0
		if(getSwitchStatus() == 0) {
			setVAfter(0);
		}
		else {
			setVAfter(getVBefore());
		}
		
		//更新状态
		if(getSwitchStatus() == 0)
			setState("turned on");
		else if(getSwitchStatus() == 1)
			setState("closed");
		
	}

}

//互斥开关
//互斥开关状态为0时接1-2,为1时接1-3
class MutexSwitch extends AbstractSwitch {

	private String[] circuitInfo = {"", ""};
	
	public String[] getCircuitInfo() {
		return circuitInfo;
	}
	
	public void setCircuitInfo(String[] circuitInfo) {
		this.circuitInfo = circuitInfo;
	}
	
	public void change() {
		setSwitchStatus((getSwitchStatus() + 1) % 2);
		renew();
	}

	public void renew() {
		
		setVAfter(getVBefore());
		
		if(this.getSwitchStatus() == 0) {
			setR(5);
		}
		else if(this.getSwitchStatus() == 1) {
			setR(10);
		}
		
		//更新状态
		if(getSwitchStatus() == 0)
			setState("closed");
		else if(getSwitchStatus() == 1)
			setState("turned on");
			
	}

}


//窗帘
class Curtain extends AbstractCurtain{
	
	@Override
	public void renew() {
		
		if(getV() >= 50) {
			
			if(getBrightness() >= 50 && getBrightness() < 100) {
				setGear(0.8);
			}
			else if(getBrightness() >= 100 && getBrightness() < 200) {
				setGear(0.6);
			}
			else if(getBrightness() >= 200 && getBrightness() < 300) {
				setGear(0.4);
			}
			else if(getBrightness() >= 300 && getBrightness() < 400) {
				setGear(0.2);
			}
			else if(getBrightness() >= 400){
				setGear(0);
			}
			else {
				setGear(1);
			}
			
		}
		else {
			setGear(1);
		}
		

		//更新状态
		setState(String.format("%.0f%%", 100 * getGear()));
		
	}
	
}


//连续调速器
class ContinuousGovernor extends AbstractContinuousGovernor {

	public void change(double gear) {
		setGear(gear);
		renew();
	}

	public void renew() {
		
		//更新电压
		setVAfter(getVBefore() * getGear());
		
		//更新状态
		setState(String.format("%.2f", getGear()));
	}

}

//分档调速器
class BinningGovernor extends AbstractBinningGovernor {

	public void gearUp() {
		if (getGear() < 3) {
			setGear(getGear() + 1);
		}
		renew();
	}

	public void gearDown() {
		if (getGear() > 0) {
			setGear(getGear() - 1);
		}
		renew();
	}

	public void renew() {
		
		//更新电压
		if(getGear() == 0)
			setVAfter(0);
		else if(getGear() == 1)
			setVAfter(getVBefore() * 0.3);
		else if(getGear() == 2)
			setVAfter(getVBefore() * 0.6);
		else if(getGear() == 3)
			setVAfter(getVBefore() * 0.9);
		
		//更新状态
		setState(String.format("%d", getGear()));
		
	}

}

class Circuit extends AbstractElectronic{

	private ArrayList<AbstractElectronic> electronics = new ArrayList<AbstractElectronic>();
	
	@Override
	public void renew() {
		
		//更新自己的电压
		this.setV(this.getVBefore() - this.getVAfter());
		
		double RTmp = 0;
		
		//先遍历计算电路总电阻
		for(AbstractElectronic electronic : electronics) {
			RTmp += electronic.getR();
		}
		this.setR(RTmp);
		
		//查找本路上是否有开关以及开关状态
		for(AbstractElectronic electronic : electronics) {
			if(electronic.getName().indexOf("K") >= 0) {
				if(((AbstractSwitch)electronic).getSwitchStatus() == 0) {
					this.setV(0);
					//设置断路的电压为-1
					this.setVAfter(-1);
					this.setVBefore(-1);
					//断开开关的路线也不应该算电阻
					this.setR(999999);
				}
			}
			else if(electronic.getName().indexOf("H") >= 0) {
				
				//如果目前接通的路不是当前这条
				if(!((MutexSwitch)electronic).getCircuitInfo()[((AbstractSwitch)electronic).getSwitchStatus()].equals(this.getName())){
					this.setV(0);
					//设置断路的电压为-1
					this.setVAfter(-1);
					this.setVBefore(-1);
					//断开开关的路线也不应该算电阻
					this.setR(999999);
				}
//				else {
//					if(((AbstractSwitch)electronic).getSwitchStatus() == 0) {
//						this.setR(this.getR() + 5);
//					}
//					else if(((AbstractSwitch)electronic).getSwitchStatus() == 1) {
//						this.setR(this.getR() + 10);
//					}
//				}
				
			}
		}
		
		int cot = 0;
		AbstractElectronic electronicTmp = null;
		
		//再遍历更新电阻和电压,同时更新电器
		for(AbstractElectronic electronic : electronics) {
			
			//当这个串联电路没有断开时
			if(this.getR() < 99999) {
				if(cot == 0) {
					electronic.setVBefore(this.getVBefore());
					if(electronic.getVBefore() > 0) {
						electronic.setV(electronic.getR() / this.getR() * this.getV());
						electronic.setVAfter(electronic.getVBefore() - electronic.getV());
					}
					electronic.renew();
				}
				else {
					electronic.setVBefore(electronicTmp.getVAfter());
					if(electronic.getVBefore() > 0) {
						electronic.setV(electronic.getR() / this.getR() * this.getV());
						electronic.setVAfter(electronic.getVBefore() - electronic.getV());
					}
					electronic.renew();
				}
			}
			else {
				electronic.setVBefore(0);
				electronic.setV(0);
				electronic.setVAfter(0);
				electronic.renew();
			}
			
			cot ++;
			electronicTmp = electronic;
			
		}
		
	}
	
	//Getter,Setter方法们
	public ArrayList<AbstractElectronic> getElectronics() {
		return electronics;
	}

	public void setElectronics(ArrayList<AbstractElectronic> newElectronics) {
		electronics = newElectronics;
	}


}


class SeriesCircuit extends AbstractElectronic{
	
	private ArrayList<AbstractElectronic> circuits = new ArrayList<AbstractElectronic>();


	@Override
	public void renew() {
	
		
		//更新自己的电压
		this.setV(this.getVBefore() - this.getVAfter());
		
		int cot = 0;
		
		double RTmp = 0;
		
//		//先查找有没有短路的
//		for(AbstractElectronic circuit : circuits) {
//			if(circuit.getR() == 0 && circuit.getV() != 0) {
//				cot ++;
//			}
//		}
//		if(cot > 0) {
//			for(AbstractElectronic circuit : circuits) {
//				circuit.setV(0);
//				circuit.setVAfter(0);
//				circuit.setVBefore(0);
//			}
//		}
		
//		cot = 0;
//		//再查找这些串联电路是不是全断开的
//		for(AbstractElectronic circuit : circuits) {
//			if(circuit.getV() == 0)
//				cot ++;
//		}
//		if(cot == circuits.size()) {
//			this.setV(0);
//			this.setVAfter(0);
//			this.setVBefore(0);
//		}
		
		//先更新内部的串联电路,目的是计算电阻
		for(AbstractElectronic electronic : circuits) {
			electronic.renew();
		}
		
		//计算总电阻
		for(AbstractElectronic electronic : circuits) {
			if(electronic.getR() < 999999)
				RTmp += 1 / electronic.getR();
		}
		this.setR(1 / RTmp);
		
		//更新每个串联的电压
		for(AbstractElectronic electronic : circuits) {
			electronic.setVBefore(this.getVBefore());
			electronic.setVAfter(this.getVAfter());
			electronic.setV(this.getV());
			electronic.renew();
		}
		
		//最后再次更新,目的是更新所有的电压,状态等
		for(AbstractElectronic electronic : circuits) {
			electronic.renew();
		}
		
	}
	
	
	//getter,setter方法们
	public ArrayList<AbstractElectronic> getCircuits() {
		return circuits;
	}
	
	public void setCircuits(ArrayList<AbstractElectronic> circuits) {
		this.circuits = circuits;
	}
	
}

class CircuitDiagram extends AbstractElectronic{

	//存放已有的所有元件,串联电路和并联电路
	//遍历查找元件,同时利用java中对象名是引用(指针)的特性,直接修改
	private ArrayList<AbstractElectronic> electronics = new ArrayList<AbstractElectronic>();
	private ArrayList<AbstractElectronic> circuits = new ArrayList<AbstractElectronic>();
	private ArrayList<AbstractElectronic> seriesCircuit = new ArrayList<AbstractElectronic>();

	
	//真正电路图
	private ArrayList<AbstractElectronic> diagram = new ArrayList<AbstractElectronic>();

	
	//更新整体
	@Override
	public void renew() {
		
		//更新自己的电压
		this.setVBefore(220);
		this.setVAfter(0);
		this.setV(220);
		
		
		//先更新整个电路图中的所有元件计算电阻
		for(AbstractElectronic electronic : diagram) {
			electronic.renew();
		}
		
		
		double RTmp = 0;
		//先遍历计算电路总电阻
		for(AbstractElectronic electronic : diagram) {
			RTmp += electronic.getR();
		}
		this.setR(RTmp);
		
		
		//查找主路上是否有开关以及开关状态
		for(AbstractElectronic electronic : diagram) {
			if(electronic.getName().indexOf("K") >= 0) {
				if(((AbstractSwitch)electronic).getSwitchStatus() == 0) {
					this.setV(0);
					this.setVAfter(0);
					this.setVBefore(0);
					this.setR(999999);
				}
			}
			else if(electronic.getName().indexOf("H") >= 0) {
				
				//如果目前接通的路不是当前这条
				if(!((MutexSwitch)electronic).getCircuitInfo()[((AbstractSwitch)electronic).getSwitchStatus()].equals(this.getName())){
					this.setV(0);
					//设置断路的电压为-1
					this.setVAfter(-1);
					this.setVBefore(-1);
					//断开开关的路线也不应该算电阻
					this.setR(999999);
				}
			}
		}
		
		int cot = 0;
		AbstractElectronic electronicTmp = null;
		
		//再遍历更新电阻和电压
		for(AbstractElectronic electronic : diagram) {
			
			if(this.getR() < 999999) {
				if(cot == 0) {
					electronic.setVBefore(this.getVBefore());
					if(electronic.getVBefore() > 0) {
						electronic.setV(electronic.getR() / this.getR() * this.getV());
						electronic.setVAfter(electronic.getVBefore() - electronic.getV());
					}
					electronic.renew();
				}
				else {
					electronic.setVBefore(electronicTmp.getVAfter());
					if(electronic.getVBefore() > 0) {
						electronic.setV(electronic.getR() / this.getR() * this.getV());
						electronic.setVAfter(electronic.getVBefore() - electronic.getV());
					}
					else {
						
					}
					electronic.renew();
				}
			}
			else {
				electronic.setVAfter(0);
				electronic.setVBefore(0);
				electronic.setV(0);
			}
			
			
			//特殊判断是不是调速器,如果是需要直接更改整条路的输入输出电压
			//不然在这种前者输出等于后者输入,电压按比例分的逻辑下没法正确实现
            if(electronic.getName().indexOf("L") >= 0 || electronic.getName().indexOf("F") >= 0){
                this.setV(electronic.getVAfter());
                this.setVBefore(electronic.getVAfter());
            }
			
			cot ++;
			electronicTmp = electronic;
			
		}
		
		//最后再次更新,目的是更新所有的电压,状态等
		for(AbstractElectronic electronic : diagram) {
			electronic.renew();
		}
		
		//遍历此时所有灯,记录总亮度,赋给窗帘
		double brightnessTmp = 0;
		for(AbstractElectronic electronic : electronics) {
			try {
				brightnessTmp += ((AbstractLight)electronic).getBrightness();
			} catch (ClassCastException e) {
				// TODO: handle exception
			}
		}
		for(AbstractElectronic electronic : electronics) {
			try {
				((AbstractCurtain)electronic).setBrightness(brightnessTmp);
				electronic.renew();
			} catch (ClassCastException e) {
				// TODO: handle exception
			}
		}
		
	}

	
	//按顺序展示信息
	public void showInfo() {
		
		ArrayList<AbstractElectronic> electronicsTmp = new ArrayList<AbstractElectronic>();
		
		String[] names = {"K", "F", "L", "B", "R", "D", "A","H","S"};
		
		//在目前所有的电子元件中按顺序找到元件并输出
		for(int i = 0; i < names.length; i++) {
			
			ArrayList<AbstractElectronic> electronicsTmp2 = new ArrayList<AbstractElectronic>();
			
			for(AbstractElectronic electronic : electronics) {
				if(electronic.getName().indexOf(names[i]) >= 0) {
					electronicsTmp2.add(electronic);
				}
			}
			
			Collections.sort(electronicsTmp2);
			
			electronicsTmp.addAll(electronicsTmp2);
			
		}
		
		for(AbstractElectronic electronic : electronicsTmp) {
			
			System.out.println("@" + electronic.getName() + ":" + electronic.getState());
			
		}
		
	}
	
	//Getter,Setter方法们
	public ArrayList<AbstractElectronic> getCircuits() {
		return circuits;
	}
	
	public void setCircuits(ArrayList<AbstractElectronic> circuits) {
		this.circuits = circuits;
	}

	public ArrayList<AbstractElectronic> getElectronics() {
		return electronics;
	}

	public void setElectronics(ArrayList<AbstractElectronic> electronics) {
		this.electronics = electronics;
	}

	public ArrayList<AbstractElectronic> getSeriesCircuit() {
		return seriesCircuit;
	}

	public void setSeriesCircuit(ArrayList<AbstractElectronic> seriesCircuit) {
		this.seriesCircuit = seriesCircuit;
	}

	public ArrayList<AbstractElectronic> getDiagram() {
		return diagram;
	}

	public void setDiagram(ArrayList<AbstractElectronic> diagram) {
		this.diagram = diagram;
	}
	
}






class Input {

	private String input;

	public Input() {
		// TODO: implement
	}

	public Input(String input) {
		this.input = input;
	}
	
	public String getInput() {
		return input;
	}
	
	public void setInput(String input) {
		this.input = input;
	}
	
	//判断输入类型
	//1输入的是单条串联,2是并联电路组成信息,3是整个电路图链接组成信息,4是对元件操作信息,-1终止,else格式错误
	public int judgeInput() {
		
		if(input.matches("#T[0-9]+:(\\[IN\\s+([K|F|L|B|R|D|A|S|H|T][0-9]+-[1|2|3]){1}\\]\\s*){1}(\\[([K|F|L|B|R|D|A|S|H|T][0-9]+-[1|2|3]\\s*){2,}\\]\\s*){0,}(\\[([K|F|L|B|R|D|A|S|H|T][0-9]+-[1|2|3]){1,}\\s+(OUT)\\]\\s*){1}"))
            return 1;	
		//这里用\\s*显然是有问题的,当两个中间没空格就会误判为正确的同时在分割的时候也会出错
        else if(input.matches("#M[0-9]+:(\\[(T[0-9]+\\s*){1,}\\])"))
            return 2;
		//史上最长正则
        else if(input.matches("#T[0-9]+:(\\[VCC\\s+((([K|F|L|B|R|D|A|S|H][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT)))\\s*){1,}\\]\\s*){1}(\\[((([K|F|L|B|R|D|A|S|H][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT)))\\s*){2,}\\]\\s*){0,}(\\[((([K|F|L|B|R|D|A|S|H][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT)))\\s*){1,}\\s+(GND)\\]\\s*){1}"))
//        else if(input.matches("#T[0-9]+:\\[VCC.+GND\\]"))
            return 3;
        else if(input.matches("#((K[0-9]+)|(L[0-9]+:[0-9|.]+)|((F[0-9]+\\+)|(F[0-9]+-))|(H[0-9]+))"))
            return 4;
        else if(input.indexOf("end") >= 0)
            return -1;
        else
            return 0;
	}
	
	//返回单条串联电路和其中各个元件信息(没有删除重复)
	public String[] splitCircuit() {
		
		input = input.replaceAll("\\[|\\]", " ");
		input = input.replaceAll(":|#", "");
		
		//去掉开头结尾空格,防止数组分割多了
        input = input.replaceAll("^\\s+", "");
        input = input.replaceAll("\\s+$", "");
		
        //将各个元件名字分割
		String[] inputTmp = input.split("\\s+");
		
//		//去掉-和后面内容
//		for(int i = 0;i < inputTmp.length;i ++) {
//			
//			//这样会删除H的引脚信息,所以特殊判断下
//			if(!inputTmp[i].matches("H[0-9]+-[2|3]")) {
//				
//				inputTmp[i] = inputTmp[i].replaceAll("-.*","");
//			}
//			
//		}
		
		return inputTmp;
	}
	
	//返回并联电路组成信息
	public String[] splitSeriesCircuit(){
		
		//去掉分割符
		input = input.replaceAll("#|:", "");
		input = input.replaceAll("\\[|\\]", " ");
		//去掉前后空格防止分多了
		input = input.replaceAll("^\\s+", "");
		input = input.replaceAll("\\s+$", "");
		
		String[] inputTmp = input.split("\\s+");
		
		return inputTmp;
	}
	
	//返回整个电路图链接组成信息
	public String[] splitCircuitDiagram() {
		input = input.replaceAll("\\[|\\]", " ");
		input = input.replaceAll(":|#", "");
		
		//去掉开头结尾空格,防止数组分割多了
        input = input.replaceAll("^\\s+", "");
        input = input.replaceAll("\\s+$", "");
		
        //将各个元件名字分割
		String[] inputTmp = input.split("\\s+");
		
//		//去掉-和后面内容
//		for(int i = 0;i < inputTmp.length;i ++) {
//			inputTmp[i] = inputTmp[i].replaceAll("-.*","");
//		}
		
		return inputTmp;
	}
	
	//返回对元件的操作信息
	public String[] operation() {
		
		input = input.replaceAll("#","");
		String[] inputTmp = input.split(":");
		return inputTmp;
		
	}
	
	
}

class Controller {
	
	private CircuitDiagram circuitDiagram = new CircuitDiagram();
	private Input input = new Input();

	public Controller() {
		
	}
	
	
	//创建元件并放入单条串联电路(以下方法的操作均是对circuitDiagram进行的)
	public void inputCircuit() {
		
		String[] inputTmp = input.splitCircuit();
		Circuit circuitTmp = new Circuit();
		
		circuitTmp.setName(inputTmp[0]);
		String inputTmp2 = inputTmp[0].replaceAll("T", "");
		circuitTmp.setNum(Integer.parseInt(inputTmp2));
		
		//跳过IN等符号
		for(int i = 2; i < inputTmp.length; i += 2) {
			
			AbstractElectronic electronic = null;
			
			int hasFound = -1;

			String inputTmp3 = inputTmp[i].replaceAll("-.*","");
			
			//先在已经有的电子元件中查找,没有查到再新建
			for(AbstractElectronic electronic2 : this.circuitDiagram.getElectronics()) {
				
				if(electronic2.getName().equals(inputTmp3)) {
					hasFound = 1;
					electronic = electronic2;
					
					//特殊判断下是不是互斥开关
					if(inputTmp[i].indexOf("H") >= 0) {
						
						
						if(inputTmp[i + 1].matches("H[0-9]+-2") || inputTmp[i].matches("H[0-9]+-2")) {
							((MutexSwitch)electronic).getCircuitInfo()[0] = inputTmp[0];
						}
						else if(inputTmp[i + 1].matches("H[0-9]+-3") || inputTmp[i].matches("H[0-9]+-3")){
							((MutexSwitch)electronic).getCircuitInfo()[1] = inputTmp[0];
						}
						
					}
					
					//放入这段电路
					circuitTmp.getElectronics().add(electronic);
					break;
				}
				
			}
			
			//没找到就新建
			if(hasFound == -1) {
				
				if(inputTmp[i].indexOf("K") >= 0) {
					electronic = new Switch();
				}
				else if(inputTmp[i].indexOf("F") >= 0) {
					electronic = new BinningGovernor();
				}
				else if(inputTmp[i].indexOf("L") >= 0) {
					electronic = new ContinuousGovernor();
				}
				else if(inputTmp[i].indexOf("B") >= 0) {
					electronic = new Incandescent();
					electronic.setR(10);
				}
				else if(inputTmp[i].indexOf("R") >= 0) {
					electronic = new Fluorescent();
					electronic.setR(5);
				}
				else if(inputTmp[i].indexOf("D") >= 0) {
					electronic = new Fan();
					electronic.setR(20);
				}
				else if(inputTmp[i].indexOf("A") >= 0) {
					electronic = new FloorFan();
					electronic.setR(20);
				}
				else if(inputTmp[i].indexOf("S") >= 0) {
					electronic = new Curtain();
					electronic.setR(15);
				}
				//为互斥开关时需要看下是哪个接口
				else if(inputTmp[i].indexOf("H") >= 0) {
					
					electronic = new MutexSwitch();
					
					if(inputTmp[i + 1].matches("H[0-9]+-2") || inputTmp[i].matches("H[0-9]+-2")) {
						((MutexSwitch)electronic).getCircuitInfo()[0] = inputTmp[0];
					}
					else if(inputTmp[i + 1].matches("H[0-9]+-3") || inputTmp[i].matches("H[0-9]+-3")){
						((MutexSwitch)electronic).getCircuitInfo()[1] = inputTmp[0];
					}
					
				}
				//串联组成时由串联组成的时候
				else if(inputTmp[i].indexOf("T") >= 0) {
					int hasFound1 = -1;
					for(AbstractElectronic circuit : this.getCircuitDiagram().getCircuits()) {
						if(circuit.getName().equals(inputTmp3)) {
							hasFound1 = 1;
							electronic = circuit;
							break;
						}
					}
					
					//没找到时
					if(hasFound1 < 0) {
						
					}
					
				}
				
//				//除了互斥开关,其他都要把-后去掉
//				if(!inputTmp[i].matches("H[0-9]+-[2|3]")) {
					inputTmp[i] = inputTmp[i].replaceAll("-.*","");
//				}
				
				//需要先判断下不是In和out等,不然转数字会错
				if(inputTmp[i].indexOf("IN") < 0 && inputTmp[i].indexOf("OUT") < 0) {
					//设置名字和编号
					electronic.setName(inputTmp[i]);
					inputTmp[i] = inputTmp[i].replaceAll("K|F|L|B|R|D|A|S|H|T", "");
					electronic.setNum(Integer.parseInt(inputTmp[i]));
					
					circuitTmp.getElectronics().add(electronic);
					
					//元件放入
					circuitDiagram.getElectronics().add(electronic);
				}
				
				
			}
			
			
			
		}
		
		//更新下总电阻
		circuitTmp.renew();
		
		//单条串联放入
		circuitDiagram.getCircuits().add(circuitTmp);
		
	}
	
	//寻找串联电路放入并联中
	public void inputSeriesCircuit() {

		String[] inputTmp = input.splitSeriesCircuit();
		SeriesCircuit seriesCircuitTmp = new SeriesCircuit();
		
		seriesCircuitTmp.setName(inputTmp[0]);
		inputTmp[0] = inputTmp[0].replaceAll("M", "");
		seriesCircuitTmp.setNum(Integer.parseInt(inputTmp[0]));
		
		for(int i = 1; i < inputTmp.length; i ++) {
			
			int hasFound = 0;
			
			//对于每一个放在并联电路中的串联查找并放入
			for(AbstractElectronic circuit : circuitDiagram.getCircuits()) {
				if(circuit.getName().equals(inputTmp[i])) {
					hasFound = 1;
					seriesCircuitTmp.getCircuits().add(circuit);
					break;
				}
			}
			
			//对没找到的处理
			if(hasFound == 0) {
				
			}
			
			
		}
		
		//更新信息
		seriesCircuitTmp.renew();
		
		//并联电路放入
		circuitDiagram.getSeriesCircuit().add(seriesCircuitTmp);
		
	}
	
	//创建整个电路图
	public void inputCircuitDiagram() {
		
		String[] inputTmp = input.splitCircuitDiagram();
		Circuit circuitTmp = new Circuit();
		
//		circuitTmp.setName(inputTmp[0]);
//		inputTmp[0] = inputTmp[0].replaceAll("T", "");
//		circuitTmp.setNum(Integer.parseInt(inputTmp[0]));
		
		circuitTmp.setName(inputTmp[0]);
		String inputTmp2 = inputTmp[0].replaceAll("T", "");
		circuitTmp.setNum(Integer.parseInt(inputTmp2));
		
		
		//跳过VCC等符号
		for(int i = 2; i < inputTmp.length; i += 2) {
			
			AbstractElectronic electronic = null;
			
			int hasFound = -1;
			String inputTmp3 = inputTmp[i].replaceAll("-.*","");
			
			//先在已经有的电子元件中查找,没有查到再新建
			for(AbstractElectronic electronic2 : this.circuitDiagram.getElectronics()) {
				
				
				if(electronic2.getName().equals(inputTmp3)) {
					hasFound = 1;
					electronic = electronic2;
					
					//特殊判断下是不是互斥开关
					if(inputTmp[i].indexOf("H") >= 0) {
						
						if(inputTmp[i + 1].matches("H[0-9]+-2") || inputTmp[i].matches("H[0-9]+-2")) {
							((MutexSwitch)electronic).getCircuitInfo()[0] = inputTmp[0];
						}
						else if(inputTmp[i + 1].matches("H[0-9]+-3") || inputTmp[i].matches("H[0-9]+-3")){
							((MutexSwitch)electronic).getCircuitInfo()[1] = inputTmp[0];
						}
						
					}
					
					//放入这段电路
					circuitTmp.getElectronics().add(electronic);
					break;
				}
				
			}
			
			if(hasFound < 0) {
				
				if(inputTmp[i].indexOf("K") >= 0) {
					electronic = new Switch();
				}
				else if(inputTmp[i].indexOf("F") >= 0) {
					electronic = new BinningGovernor();
				}
				else if(inputTmp[i].indexOf("L") >= 0) {
					electronic = new ContinuousGovernor();
				}
				else if(inputTmp[i].indexOf("B") >= 0) {
					electronic = new Incandescent();
					electronic.setR(10);
				}
				else if(inputTmp[i].indexOf("R") >= 0) {
					electronic = new Fluorescent();
					electronic.setR(5);
				}
				else if(inputTmp[i].indexOf("D") >= 0) {
					electronic = new Fan();
					electronic.setR(20);
				}
				else if(inputTmp[i].indexOf("A") >= 0) {
					electronic = new FloorFan();
					electronic.setR(20);
				}
				else if(inputTmp[i].indexOf("S") >= 0) {
					electronic = new Curtain();
					electronic.setR(15);
				}
				else if(inputTmp[i].indexOf("M") >= 0) {
					
					int hasfound = 0;
					
					for(AbstractElectronic electronic1 : circuitDiagram.getSeriesCircuit()) {
						if(electronic1.getName().equals(inputTmp3)){
							electronic = electronic1;
							hasfound = 1;
							break;
						}
					}
					
					if(hasfound == 0) {
						
					}
				}
				//为互斥开关时需要看下是哪个接口
				else if(inputTmp[i].indexOf("H") >= 0) {
					
					electronic = new MutexSwitch();
					
					if(inputTmp[i + 1].matches("H[0-9]+-2") || inputTmp[i].matches("H[0-9]+-2")) {
						((MutexSwitch)electronic).getCircuitInfo()[0] = inputTmp[0];
					}
					else if(inputTmp[i + 1].matches("H[0-9]+-3") || inputTmp[i].matches("H[0-9]+-3")){
						((MutexSwitch)electronic).getCircuitInfo()[1] = inputTmp[0];
					}
					
				}
				else if(inputTmp[i].indexOf("T") >= 0) {
					
					int hasfound = 0;
					
					for(AbstractElectronic electronic1 : circuitDiagram.getCircuits()) {
						if(electronic1.getName().equals(inputTmp3)){
							electronic = electronic1;
							hasfound = 1;
							break;
						}
					}
					
					if(hasfound == 0) {
						//应该给一个状态,让下述的放入不进行,否则会放入null出错
					}
					
				}
				
//				//除了互斥开关,其他都要把-后去掉
//				if(!inputTmp[i].matches("H[0-9]+-[2|3]")) {
					inputTmp[i] = inputTmp[i].replaceAll("-.*","");
//				}
				
				
				//需要先判断下不是Vcc和Gnd等,不然转数字会错
				if(inputTmp[i].indexOf("VCC") < 0 && inputTmp[i].indexOf("GND") < 0) {
					
					//设置名字编号
					electronic.setName(inputTmp[i]);
					inputTmp[i] = inputTmp[i].replaceAll("K|F|L|B|R|D|A|M|T|S|H", "");
					electronic.setNum(Integer.parseInt(inputTmp[i]));
					
					circuitTmp.getElectronics().add(electronic);
					
					//元件放入
					circuitDiagram.getElectronics().add(electronic);
					
				}
				
			}
			
			
			
		}
		
		//将本条电路放入,作为最终的电路图
		circuitDiagram.setDiagram(circuitTmp.getElectronics());
		circuitDiagram.setName(circuitTmp.getName());

//		//更新信息
//		circuitDiagram.renew();
		
	}
	
	//对元件进行操作
	public void operation() {
		
		String[] inputTmp = input.operation();
		
		//对开关或者互斥开关
		if(inputTmp[0].indexOf("K") >= 0 || inputTmp[0].indexOf("H") >= 0) {
			
			int hasFound = 0;
			
			for(AbstractElectronic electronic : circuitDiagram.getElectronics()) {
				if(electronic.getName().equals(inputTmp[0])){
					((AbstractSwitch)electronic).change();
					hasFound = 1;
				}
			}
			
			if(hasFound == 0) {
				
			}
		}
		//对换挡调速
		if(inputTmp[0].indexOf("F") >= 0) {
			
			int hasFound = 0;
			
			String inputTmp2 = inputTmp[0].replaceAll("\\+|-", "");
			
			for(AbstractElectronic electronic : circuitDiagram.getElectronics()) {
				if(electronic.getName().equals(inputTmp2)){
					if(inputTmp[0].indexOf("+") >= 0)
						((AbstractBinningGovernor)electronic).gearUp();
					else if(inputTmp[0].indexOf("-") >= 0)
						((AbstractBinningGovernor)electronic).gearDown();
					hasFound = 1;
				}
			}
			
			if(hasFound == 0) {
				
			}
		}
		//对连续调速
		if(inputTmp[0].indexOf("L") >= 0) {
			
			int hasFound = 0;
			
			for(AbstractElectronic electronic : circuitDiagram.getElectronics()) {
				if(electronic.getName().equals(inputTmp[0])){
					((AbstractContinuousGovernor)electronic).setGear(Double.parseDouble(inputTmp[1]));
					hasFound = 1;
				}
			}
			
			if(hasFound == 0) {
				
			}
		}
		
//        //更新信息
//        circuitDiagram.renew();
		
	}
	
	//展示信息
	public void showInfo() {
		
        //更新信息
        circuitDiagram.renew();
		
		circuitDiagram.showInfo();
	}
	
	
	//getter,setter方法们

	public CircuitDiagram getCircuitDiagram() {
		return circuitDiagram;
	}

	public void setCircuitDiagram(CircuitDiagram circuitDiagram) {
		this.circuitDiagram = circuitDiagram;
	}

	public Input getInput() {
		return input;
	}
	
	public void setInput(Input input) {
		this.input = input;
	}
	
	
	
}


PTA-oop-8


家居强电电路模拟程序-4:


  本题代码没有满分!

点击查看代码
package oopPrograms8;

import java.util.*;

public class Main8 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

        int isEnd = 0;
        String input = "";
        Controller controller = new Controller();

        //循环输入,遇end结束
        while(true) {

            input = in.nextLine();
            controller.getInput().setInput(input);

            switch(controller.getInput().judgeInput()) {
                case 1:
                	controller.inputCircuit();
                    break;
                case 2:
                	controller.inputSeriesCircuit();
                    break;
                case 3:
                    controller.inputCircuitDiagram();
                    break;
                case 4:
                    controller.operation();
                    break;
                case 5:
                	
                	break;
                case -1:
                    isEnd = 1;
                    break;
                default:
//                  controller.showWrongFormat();
                    break;
            }

            //判断是否需要退出输入
            if(isEnd == 1)
                break;

        }

        controller.showInfo();
        
	}

}

//抽象电子元件类
abstract class AbstractElectronic implements Comparable<AbstractElectronic>{
	
	private String name = "";  
	private int num = 0;
	private double current = 0;
	private double v = 0;
	private double vBefore = 0;
	private double vAfter = 0;
	private double r = 0;
	private String state = "";
	private double maxCurrent = 0;
	private int accessState = 0;//介入状态,0时1-2,1时2-1
	
	
	public String getName() {
		return name;
	}

	public void setName(String newName) {
		name = newName;
	}

	public double getCurrent() {
		return current;
	}

	public void setCurrent(double newCurrent) {
		current = newCurrent;
	}

	public double getV() {
		return v;
	}

	public void setV(double newV) {
		v = newV;
	}

	public double getVBefore() {
		return vBefore;
	}

	public void setVBefore(double newVBefore) {
		vBefore = newVBefore;
	}

	public double getVAfter() {
		return vAfter;
	}

	public void setVAfter(double newVAfter) {
		vAfter = newVAfter;
	}

	public double getR() {
		return r;
	}

	public void setR(double newR) {
		r = newR;
	}
	
	public int getNum() {
		return num;
	}
	
	public void setNum(int num) {
		this.num = num;
	}

	public abstract void renew();

	public String getState() {
		return state;
	}
	
	public void setState(String state) {
		this.state = state;
	}
	
	@Override
	public int compareTo(AbstractElectronic o) {
//		return this.num - o.num;
		return this.name.compareTo(o.name);
	}

	public double getMaxCurrent() {
		return maxCurrent;
	}

	public void setMaxCurrent(double maxCurrent) {
		this.maxCurrent = maxCurrent;
	}
	
	public int getAccessState() {
		return accessState;
	}
	
	public void setAccessState(int accessState) {
		this.accessState = accessState;
	}
	
}

//抽象电灯类
abstract class AbstractLight extends AbstractElectronic {

	private double brightness = 0;

	public double getBrightness() {
		return brightness;
	}

	public void setBrightness(double newBrightness) {
		brightness = newBrightness;
	}

}

//抽象风扇类
abstract class AbstractFan extends AbstractElectronic {

	private double speed = 0;

	public double getSpeed() {
		return speed;
	}

	public void setSpeed(double newSpeed) {
		speed = newSpeed;
	}

}

//抽象开关
abstract class AbstractSwitch extends AbstractElectronic {

	//开关为0时断开
	//开关初始断开
	private int switchStatus = 0;

	public abstract void change();

	public int getSwitchStatus() {
		return switchStatus;
	}

	public void setSwitchStatus(int newSwitchStatus) {
		switchStatus = newSwitchStatus;
	}

}

//抽象连续控制器
abstract class AbstractContinuousGovernor extends AbstractElectronic {

	private double gear = 0;

	public abstract void change(double gear);

	public double getGear() {
		return gear;
	}

	public void setGear(double gear2) {
		gear = gear2;
	}

}

//抽象分档调节器
abstract class AbstractBinningGovernor extends AbstractElectronic {

	private int gear = 0;

	public abstract void gearUp();

	public abstract void gearDown();

	public int getGear() {
		return gear;
	}

	public void setGear(int newGear) {
		gear = newGear;
	}
	
	
}

//抽象窗帘
abstract class AbstractCurtain extends AbstractElectronic {

	private double brightness = 0;
	private double gear = 0;

	public double getBrightness() {
		return brightness;
	}
	
	public void setBrightness(double brightness) {
		this.brightness = brightness;
	}
	
	public double getGear() {
		return gear;
	}

	public void setGear(double newGear) {
		gear = newGear;
	}
	
	
}

//白炽灯
class Incandescent extends AbstractLight {
	
	private double maxCurrent = 9;
	
	public void renew() {
		
		//首先更新电压
		setV(getVBefore() - getVAfter());
		
		if(getV() < 10) {
			setBrightness(0);
		}
		else {
			//这里有问题!!!整数做除法一定要和浮点数除!!!210改成210.0!
			setBrightness((getV() - 10) * 150 / 210.0 + 50);
		}
		
		//更新状态
		setState(String.format("%d", (int)getBrightness()));
		
		//输出电压
		if(getAccessState() == 0) {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			//这里之所以要加0.1是因为在把9999999当断路的过程会导致有可能某个电压会小0.0001之类的,会导致小数直接被舍去,应该加上
			setState(getState() + " " + String.format("%d", (int)(getVBefore() + 0.01)) + "-" + String.format("%d", (int)(getVAfter() + 0.01)));
		}
		else {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVAfter() + 0.01)) + "-" + String.format("%d", (int)(getVBefore() + 0.01)));
		}
		
		
		
		//输出超出电流状态
		if(this.getCurrent() > this.maxCurrent) {
			setState(getState().replaceAll(" exceeding current limit error", ""));
			setState(getState() + " exceeding current limit error");
		}
		
	}

}

//日光灯
class Fluorescent extends AbstractLight {
	
	private double maxCurrent = 5;
	
	public void renew() {
		
		//首先更新电压
		setV(getVBefore() - getVAfter());
		
		//更新亮度
		if(this.getV() > 0.01) {
			setBrightness(180);
		}
		else {
			setBrightness(0);
		}
		
		//更新状态
		setState(String.format("%d", (int)getBrightness()));
		
		//输出电压
		if(getAccessState() == 0) {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVBefore() + 0.01)) + "-" + String.format("%d", (int)(getVAfter() + 0.01)));
		}
		else {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVAfter() + 0.01)) + "-" + String.format("%d", (int)(getVBefore() + 0.01)));
		}
		
		//输出超出电流状态
		if(this.getCurrent() > this.maxCurrent) {
			setState(getState().replaceAll(" exceeding current limit error", ""));
			setState(getState() + " exceeding current limit error");
		}
		
	}

}


//风扇
class Fan extends AbstractFan {

	private double maxCurrent = 12;
	
	public void renew() {
		
		//首先更新电压
//		setV(getVBefore() - getVAfter());
		
		//转速
		if(getV() > 150) {
			setSpeed(360);
		}
		else if(getV() >= 80 && getV() <= 150) {
			setSpeed((getV() - 80) * 280 / 70.0 + 80);
		}
		else {
			setSpeed(0);
		}
		
		//更新状态
		setState(String.format("%d",(int)getSpeed()));
		
		//输出电压
		if(getAccessState() == 0) {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVBefore() + 0.01)) + "-" + String.format("%d", (int)(getVAfter() + 0.01)));
		}
		else {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVAfter() + 0.01)) + "-" + String.format("%d", (int)(getVBefore() + 0.01)));
		}
		
		//输出超出电流状态
		if(this.getCurrent() > this.maxCurrent) {
			setState(getState().replaceAll(" exceeding current limit error", ""));
			setState(getState() + " exceeding current limit error");
		}
		
	}

}

//落地扇
class FloorFan extends AbstractFan {

	private double maxCurrent = 14;
	
	public void renew() {
		
		//首先更新电压
		setV(getVBefore() - getVAfter());
		
		//转速
		if(getV() >= 80 && getV() <= 99) {
			setSpeed(80);
		}
		else if(getV() >= 100 && getV() <= 119) {
			setSpeed(160);
		}
		else if(getV() >= 120 && getV() <= 139) {
			setSpeed(260);
		}
		else if(getV() > 140) {
			setSpeed(360);
		}
		else {
			setSpeed(0);
		}
		
		//更新状态
		setState(String.format("%d",(int)getSpeed()));
		
		//输出电压
		if(getAccessState() == 0) {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVBefore() + 0.01)) + "-" + String.format("%d", (int)(getVAfter() + 0.01)));
		}
		else {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVAfter() + 0.01)) + "-" + String.format("%d", (int)(getVBefore() + 0.01)));
		}
		
		//输出超出电流状态
		if(this.getCurrent() > this.maxCurrent) {
			setState(getState().replaceAll(" exceeding current limit error", ""));
			setState(getState() + " exceeding current limit error");
		}
		
	}

}

//开关
class Switch extends AbstractSwitch {

	private double maxCurrent = 20;
	
	public void change() {
		setSwitchStatus((getSwitchStatus() + 1) % 2);
		renew();
	}

	public void renew() {
		
		//断开即输出电压0
		if(getSwitchStatus() == 0) {
//			setVAfter(0);
			setR(999999999);
		}
		else {
			setVAfter(getVBefore());
			setR(0);
		}
		
		//更新状态
		if(getSwitchStatus() == 0)
			setState("turned on");
		else if(getSwitchStatus() == 1)
			setState("closed");
		
		//输出电压
		if(getAccessState() == 0) {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVBefore() + 0.01)) + "-" + String.format("%d", (int)(getVAfter() + 0.01)));
		}
		else {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVAfter() + 0.01)) + "-" + String.format("%d", (int)(getVBefore() + 0.01)));
		}
		
		//输出超出电流状态
		if(this.getCurrent() > this.maxCurrent) {
			setState(getState().replaceAll(" exceeding current limit error", ""));
			setState(getState() + " exceeding current limit error");
		}
		
	}

}

//互斥开关
//互斥开关状态为0时接1-2,为1时接1-3
//1-2,1-3或1-3,1-2时接通状态0,2-1,3-1或3-1,2-1时接通状态1
class MutexSwitch extends AbstractSwitch {

	private double maxCurrent = 20;
	
	private String[] circuitInfo = {"", ""};
	//三个引脚电压
	private double[] Vs = {0,0,0};
	
	public String[] getCircuitInfo() {
		return circuitInfo;
	}
	
	public void setCircuitInfo(String[] circuitInfo) {
		this.circuitInfo = circuitInfo;
	}
	
	public void change() {
		setSwitchStatus((getSwitchStatus() + 1) % 2);
		renew();
	}

	public void renew() {
		
//		setVAfter(getVBefore());
		
		if(this.getSwitchStatus() == 0) {
			setR(5);
		}
		else if(this.getSwitchStatus() == 1) {
			setR(10);
		}
		
		//更新状态
		if(getSwitchStatus() == 0)
			setState("closed");
		else if(getSwitchStatus() == 1)
			setState("turned on");
		
		//更新三个引脚电压
		if(getSwitchStatus() == 0) {
			if(getAccessState() == 0) {
				Vs[0] = getVBefore();
				Vs[1] = getVAfter();
			}
			else if(getAccessState() == 1) {
				Vs[1] = getVBefore();
				Vs[0] = getVAfter();
			}
		}
		else if(getSwitchStatus() == 1) {
			if(getAccessState() == 0) {
				Vs[0] = getVBefore();
				Vs[2] = getVAfter();
			}
			else if(getAccessState() == 1) {
				Vs[2] = getVBefore();
				Vs[0] = getVAfter();
			}
		}
		
		//输出电压
		setState(getState().replaceAll("[0-9]+-[0-9]+-[0-9]+", ""));
		setState(getState() + " " + String.format("%d", (int)(Vs[0] + 0.01)) + "-" + String.format("%d", (int)(Vs[1] + 0.01)) + "-" + String.format("%d", (int)(Vs[2] + 0.01)));
		
		//输出超出电流状态
		if(this.getCurrent() > this.maxCurrent) {
			setState(getState().replaceAll(" exceeding current limit error", ""));
			setState(getState() + " exceeding current limit error");
		}
		
	}

	public double[] getVs() {
		return Vs;
	}

	public void setVs(double[] vs) {
		Vs = vs;
	}
	
	

}


//二极管
class Diode extends AbstractSwitch{

	private double maxCurrent = 8;
	
	@Override
	public void renew() {
		
		if(getSwitchStatus() == 1)
			setVAfter(getVBefore());
		else 
			setR(999999999);
//			setVAfter(0);;
		
//		if(this.getSwitchStatus() == 0) {
//			setR(99999999999);
//		}
//		else if(this.getSwitchStatus() == 1) {
//			setR(0);
//		}
		
		//更新状态
		if(getSwitchStatus() == 0)
			setState("cutoff");
		else if(getSwitchStatus() == 1)
			setState("conduction");
		
		
		//输出电压
		if(getAccessState() == 0) {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVBefore() + 0.01)) + "-" + String.format("%d", (int)(getVAfter() + 0.01)));
		}
		else {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVAfter() + 0.01)) + "-" + String.format("%d", (int)(getVBefore() + 0.01)));
		}
		
		//输出超出电流状态
		if(this.getCurrent() > this.maxCurrent) {
			setState(getState().replaceAll(" exceeding current limit error", ""));
			setState(getState() + " exceeding current limit error");
		}
		
		
	}

	@Override
	public void change() {
		// TODO Auto-generated method stub
		
	}
	
	
}

//窗帘
class Curtain extends AbstractCurtain{
	
	private double maxCurrent = 12;
	
	@Override
	public void renew() {
		
		if(getV() >= 50) {
			
			if(getBrightness() >= 50 && getBrightness() < 100) {
				setGear(0.8);
			}
			else if(getBrightness() >= 100 && getBrightness() < 200) {
				setGear(0.6);
			}
			else if(getBrightness() >= 200 && getBrightness() < 300) {
				setGear(0.4);
			}
			else if(getBrightness() >= 300 && getBrightness() < 400) {
				setGear(0.2);
			}
			else if(getBrightness() >= 400){
				setGear(0);
			}
			else {
				setGear(1);
			}
			
		}
		else {
			setGear(0);
		}
		

		//更新状态
		setState(String.format("%.0f%%", 100 * getGear()));
		
		
		//输出电压
		if(getAccessState() == 0) {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVBefore() + 0.01)) + "-" + String.format("%d", (int)(getVAfter() + 0.01)));
		}
		else {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVAfter() + 0.01)) + "-" + String.format("%d", (int)(getVBefore() + 0.01)));
		}
		
		//输出超出电流状态
		if(this.getCurrent() > this.maxCurrent) {
			setState(getState().replaceAll(" exceeding current limit error", ""));
			setState(getState() + " exceeding current limit error");
		}
		
	}
	
}


//连续调速器
class ContinuousGovernor extends AbstractContinuousGovernor {

	private double maxCurrent = 18;
	
	public void change(double gear) {
		setGear(gear);
		renew();
	}

	public void renew() {
		
		//更新电压
		setVAfter(getVBefore() * getGear());
		
		//更新状态
		setState(String.format("%.2f", getGear()));
		
		//输出电压
		if(getAccessState() == 0) {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVBefore() + 0.01)) + "-" + String.format("%d", (int)(getVAfter() + 0.01)));
		}
		else {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVAfter() + 0.01)) + "-" + String.format("%d", (int)(getVBefore() + 0.01)));
		}
		
		//输出超出电流状态
		if(this.getCurrent() > this.maxCurrent) {
			setState(getState().replaceAll(" exceeding current limit error", ""));
			setState(getState() + " exceeding current limit error");
		}
		
	}

}

//分档调速器
class BinningGovernor extends AbstractBinningGovernor {

	private double maxCurrent = 18;
	
	public void gearUp() {
		if (getGear() < 3) {
			setGear(getGear() + 1);
		}
		renew();
	}

	public void gearDown() {
		if (getGear() > 0) {
			setGear(getGear() - 1);
		}
		renew();
	}

	public void renew() {
		
		//更新电压
		if(getGear() == 0)
			setVAfter(0);
		else if(getGear() == 1)
			setVAfter(getVBefore() * 0.3);
		else if(getGear() == 2)
			setVAfter(getVBefore() * 0.6);
		else if(getGear() == 3)
			setVAfter(getVBefore() * 0.9);
		
		//更新状态
		setState(String.format("%d", getGear()));
		
		//输出电压
		if(getAccessState() == 0) {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVBefore() + 0.01)) + "-" + String.format("%d", (int)(getVAfter() + 0.01)));
		}
		else {
			setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
			setState(getState() + " " + String.format("%d", (int)(getVAfter() + 0.01)) + "-" + String.format("%d", (int)(getVBefore() + 0.01)));
		}
		
		//输出超出电流状态
		if(this.getCurrent() > this.maxCurrent) {
			setState(getState().replaceAll(" exceeding current limit error", ""));
			setState(getState() + " exceeding current limit error");
		}
		
	}

}

class Circuit extends AbstractElectronic{

	private ArrayList<AbstractElectronic> electronics = new ArrayList<AbstractElectronic>();
	
	@Override
	public void renew() {
		
		//更新自己的电压
		this.setV(this.getVBefore() - this.getVAfter());
		
		double RTmp = 0;
		
		//先遍历计算电路总电阻
		for(AbstractElectronic electronic : electronics) {
			RTmp += electronic.getR();
		}
		this.setR(RTmp);
		
		//查找本路上是否有开关以及开关状态
		for(AbstractElectronic electronic : electronics) {
			if(electronic.getName().indexOf("K") >= 0 || electronic.getName().indexOf("P") >= 0) {
				if(((AbstractSwitch)electronic).getSwitchStatus() == 0) {
					//断开开关的路线也不应该算电阻
					this.setR(999999999);
				}
			}
			else if(electronic.getName().indexOf("H") >= 0) {
				
				//如果目前接通的路不是当前这条
				if(!((MutexSwitch)electronic).getCircuitInfo()[((AbstractSwitch)electronic).getSwitchStatus()].equals(this.getName())){
					
					//断开开关的路线也不应该算电阻
					this.setR(999999999);
				}
				
			}
			
			
		}
		
		int cot = 0;
		AbstractElectronic electronicTmp = null;
		
		//再遍历更新电阻和电压,同时更新电器
		for(AbstractElectronic electronic : electronics) {
			
			//特殊判断互斥!!!当前没有接到这条电路不要更新互斥的电压
			//不然没有接入电路的也会影响
			if(electronic.getName().indexOf("H") >= 0 && !((MutexSwitch)electronic).getCircuitInfo()[((MutexSwitch)electronic).getSwitchStatus()].equals(this.getName())) {
				
			}
//			else if(electronic.getName().indexOf("P") >= 0 && (electronic.getVAfter() - electronic.getVBefore()) < 0.001 && (electronic.getVAfter() - electronic.getVBefore()) > -0.001 && electronic.getCurrent() < 0.01) {
//				if(electronic.getVAfter() < 0.01) {
//					if(electronic.getAccessState() == 0) {
//						((AbstractSwitch)electronic).setSwitchStatus(1);
//						electronic.renew();
//					}
//					else {
//						((AbstractSwitch)electronic).setSwitchStatus(0);
//						electronic.renew();
//					}
//				}
//				else {
//					((AbstractSwitch)electronic).setSwitchStatus(1);
//					electronic.renew();
//				}
//			}
			else {
				if(cot == 0) {
					electronic.setVBefore(this.getVBefore());
					if(electronic.getVBefore() > 0) {
						electronic.setV(electronic.getR() / this.getR() * this.getV());
						electronic.setVAfter(electronic.getVBefore() - electronic.getV());
					}
					electronic.renew();
				}
				else {
					electronic.setVBefore(electronicTmp.getVAfter());
					if(electronic.getVBefore() > 0) {
						electronic.setV(electronic.getR() / this.getR() * this.getV());
						electronic.setVAfter(electronic.getVBefore() - electronic.getV());
					}
					electronic.renew();
				}
			}
			
			cot ++;
			electronicTmp = electronic;
			
		}
		
		//更新电流并赋值给电路上每个元件
		setCurrent(getV() / getR());
		
		for(AbstractElectronic electronic : electronics) {
			
			//特殊判断互斥!!!当前没有接到这条电路不要更新
			if(electronic.getName().indexOf("H") >= 0) {
				if(((MutexSwitch)electronic).getCircuitInfo()[((MutexSwitch)electronic).getSwitchStatus()].equals(getName())) {
					electronic.setCurrent(getCurrent());
					
				}
				//短路电流等于本条路
	            else if(electronic.getName().indexOf("M") >= 0 || electronic.getName().indexOf("T") >= 0){
	                if(electronic.getR() == 0){
	                    electronic.setCurrent(this.getCurrent());
	                }
	            }
				else {
					//顺便更新下那个不在线路上的接口的电阻
					//没在线上的值就等于0或本线路输入电压
					//线路名称和第一个一样时接在了1-2上,再通过接通的正反性判断放的位置
					if(((MutexSwitch)electronic).getCircuitInfo()[0].equals(this.getName())) {
						if(((MutexSwitch)electronic).getAccessState() == 0) {
							((MutexSwitch)electronic).getVs()[1] = this.getVAfter();
						}
						else if(((MutexSwitch)electronic).getAccessState() == 1) {
							((MutexSwitch)electronic).getVs()[1] = this.getVBefore();
						}
					}
					else if(((MutexSwitch)electronic).getCircuitInfo()[1].equals(this.getName())) {
						if(((MutexSwitch)electronic).getAccessState() == 0) {
							((MutexSwitch)electronic).getVs()[2] = this.getVAfter();
						}
						else if(((MutexSwitch)electronic).getAccessState() == 1) {
							((MutexSwitch)electronic).getVs()[2] = this.getVBefore();
						}
					}
				}
			}
			else {
				electronic.setCurrent(getCurrent());
			}
			
			electronic.renew();
			
		}
		
		
		//整条路断路时,特殊判断开关,让开关的输出电压和电路输出一样
        if(this.getR() > 9999999){
            for(int i = electronics.size() - 1; i >= 0; i --) {
                AbstractElectronic electronic = electronics.get(i);
//                electronic.setVAfter(this.getVAfter());
//                electronic.setVBefore(this.getVAfter());
                electronic.setV(0);
                electronic.renew();
                if(electronic.getName().indexOf("K") >= 0 ) {
                    if(((AbstractSwitch)electronic).getSwitchStatus() == 0 || electronic.getName().indexOf("P") >= 0){
                        electronic.renew();
                        electronic.setVAfter(this.getVAfter());
                        //更新状态
//                        if(((AbstractSwitch)electronic).getSwitchStatus() == 0)
//                            setState("turned on");
//                        else if(((AbstractSwitch)electronic).getSwitchStatus() == 1)
//                            setState("closed");
//
//                        if(electronic.getAccessState() == 0) {
//                            electronic.setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
//                            electronic.setState(getState() + " " + String.format("%d", (int)(electronic.getVBefore() + 0.01)) + "-" + String.format("%d", (int)(electronic.getVAfter() + 0.01)));
//                        }
//                        else {
//                            electronic.setState(getState().replaceAll("[0-9]+-[0-9]+", ""));
//                            electronic.setState(getState() + " " + String.format("%d", (int)(electronic.getVAfter() + 0.01)) + "-" + String.format("%d", (int)(electronic.getVBefore() + 0.01)));
//                        }
                        electronic.renew();
                        break;
                    }
                }
            }
        }
 
		
	}
	
	//Getter,Setter方法们
	public ArrayList<AbstractElectronic> getElectronics() {
		return electronics;
	}

	public void setElectronics(ArrayList<AbstractElectronic> newElectronics) {
		electronics = newElectronics;
	}


}


class SeriesCircuit extends AbstractElectronic{
	
	private ArrayList<AbstractElectronic> circuits = new ArrayList<AbstractElectronic>();


	@Override
	public void renew() {
	
		
		//更新自己的电压
		this.setV(this.getVBefore() - this.getVAfter());
		
		int cot = 0;
		
		double RTmp = 0;
		
		
		//先更新内部的串联电路,目的是计算电阻
		for(AbstractElectronic electronic : circuits) {
			electronic.renew();
		}
		
		
		
		//计算总电阻
		for(AbstractElectronic electronic : circuits) {
			if(electronic.getR() < 999999999)
				RTmp += 1 / electronic.getR();
		}
		this.setR(1 / RTmp);
		if(getR() > 999999999) {
			setR(999999999);
		}
		
		//查找有没有短路的
        for(AbstractElectronic circuit : circuits) {
            if(circuit.getR() < 1) {
                this.setVBefore(getVAfter());
                this.setR(0);
                this.setR(0);
                circuit.setCurrent(this.getCurrent());
            }
        }
        
		  //查找是否都断开
		  cot = 0;
		  for(AbstractElectronic circuit : circuits) {
		      if(circuit.getR() > 999999) {
		    	  circuit.setVBefore(this.getVBefore());
		    	  circuit.setVAfter(this.getVAfter());
		      	cot++;
		      }
		  }
		  if(cot == this.circuits.size()) {
		  	this.setR(999999999);
		  }
		
		//更新每个串联的电压
		for(AbstractElectronic electronic : circuits) {
			electronic.setVBefore(this.getVBefore());
			electronic.setVAfter(this.getVAfter());
			electronic.setV(this.getV());
			electronic.renew();
		}
		
		//最后再次更新,目的是更新所有的电压,状态等
		for(AbstractElectronic electronic : circuits) {
			electronic.renew();
		}
		
	}
	
	
	//getter,setter方法们
	public ArrayList<AbstractElectronic> getCircuits() {
		return circuits;
	}
	
	public void setCircuits(ArrayList<AbstractElectronic> circuits) {
		this.circuits = circuits;
	}
	
}

class CircuitDiagram extends AbstractElectronic{

	//存放已有的所有元件,串联电路和并联电路
	//遍历查找元件,同时利用java中对象名是引用(指针)的特性,直接修改
	private ArrayList<AbstractElectronic> electronics = new ArrayList<AbstractElectronic>();
	private ArrayList<AbstractElectronic> circuits = new ArrayList<AbstractElectronic>();
	private ArrayList<AbstractElectronic> seriesCircuit = new ArrayList<AbstractElectronic>();

	
	//真正电路图
	private ArrayList<AbstractElectronic> diagram = new ArrayList<AbstractElectronic>();

	
	//更新整体
	@Override
	public void renew() {
		
		//更新自己的电压
		this.setVBefore(220);
		this.setVAfter(0);
		this.setV(220);
		
		
		//先更新整个电路图中的所有元件计算电阻
		for(AbstractElectronic electronic : diagram) {
			electronic.renew();
		}
		
		
		double RTmp = 0;
		//先遍历计算电路总电阻
		for(AbstractElectronic electronic : diagram) {
			RTmp += electronic.getR();
		}
		this.setR(RTmp);
		
		
		//查找主路上是否有开关以及开关状态
		for(AbstractElectronic electronic : diagram) {
			if(electronic.getName().indexOf("K") >= 0 || electronic.getName().indexOf("P") >= 0) {
				if(((AbstractSwitch)electronic).getSwitchStatus() == 0) {
					this.setR(999999999);
				}
			}
			else if(electronic.getName().indexOf("H") >= 0) {
				
				//如果目前接通的路不是当前这条
				if(!((MutexSwitch)electronic).getCircuitInfo()[((AbstractSwitch)electronic).getSwitchStatus()].equals(this.getName())){
					this.setR(999999999);
				}
			}
		}
		
		int cot = 0;
		AbstractElectronic electronicTmp = null;
		
		//再遍历更新电阻和电压
		for(AbstractElectronic electronic : diagram) {
			
			//特殊判断互斥!!!当前没有接到这条电路不要更新互斥的电压
			//不然没有接入电路的也会影响
			if(electronic.getName().indexOf("H") >= 0 && !((MutexSwitch)electronic).getCircuitInfo()[((MutexSwitch)electronic).getSwitchStatus()].equals(this.getName())) {
				
			}
			else {
				
				if(cot == 0) {
					electronic.setVBefore(this.getVBefore());
					if(electronic.getVBefore() > 0) {
						electronic.setV(electronic.getR() / this.getR() * this.getV());
						electronic.setVAfter(electronic.getVBefore() - electronic.getV());
					}
					electronic.renew();
				}
				else {
					electronic.setVBefore(electronicTmp.getVAfter());
					if(electronic.getVBefore() > 0) {
						electronic.setV(electronic.getR() / this.getR() * this.getV());
						electronic.setVAfter(electronic.getVBefore() - electronic.getV());
					}
					else {
						
					}
					electronic.renew();
				}
				
			}
			
			
			//特殊判断是不是调速器,如果是需要直接更改整条路的输入输出电压
			//不然在这种前者输出等于后者输入,电压按比例分的逻辑下没法正确实现
            if(electronic.getName().indexOf("L") >= 0 || electronic.getName().indexOf("F") >= 0){
                this.setV(electronic.getVAfter());
                this.setVBefore(electronic.getVAfter());
            }
			
			cot ++;
			electronicTmp = electronic;
			
		}
		
		//再遍历计算电路总电阻(为了处理短路的情况)
        RTmp = 0;
        for(AbstractElectronic electronic : diagram) {
            RTmp += electronic.getR();
        }
        this.setR(RTmp);

        //再遍历更新电阻和电压
        cot = 0;
        for(AbstractElectronic electronic : diagram) {

            //特殊判断互斥!!!当前没有接到这条电路不要更新互斥的电压
            //不然没有接入电路的也会影响
            if(electronic.getName().indexOf("H") >= 0 && !((MutexSwitch)electronic).getCircuitInfo()[((MutexSwitch)electronic).getSwitchStatus()].equals(this.getName())) {

            }
            else {

                if(cot == 0) {
//					System.out.print("" + electronic.getVBefore() + "");
                    if(electronic.getVBefore() > 0) {
                        electronic.setV(electronic.getR() / this.getR() * this.getV());
                        electronic.setVAfter(electronic.getVBefore() - electronic.getV());
                    }
                    electronic.renew();
                }
                else {
                    electronic.setVBefore(electronicTmp.getVAfter());
                    if(electronic.getVBefore() > 0) {
                        electronic.setV(electronic.getR() / this.getR() * this.getV());
                        electronic.setVAfter(electronic.getVBefore() - electronic.getV());
                    }
                    else {

                    }
                    electronic.renew();
                }

            }

            cot ++;
            electronicTmp = electronic;

        }

		
		//最后再次更新,目的是更新所有的电压,状态等
		for(AbstractElectronic electronic : diagram) {
			electronic.renew();
		}
		
		
		
		//更新电流并赋值给电路上每个元件
		setCurrent(getV() / getR());
		
		for(AbstractElectronic electronic : electronics) {
			
			//特殊判断互斥!!!当前没有接到这条电路不要更新
			if(electronic.getName().indexOf("H") >= 0) {
				if(((MutexSwitch)electronic).getCircuitInfo()[((MutexSwitch)electronic).getSwitchStatus()].equals(getName())) {
					electronic.setCurrent(getCurrent());
					
				}
				else {
					//顺便更新下那个不在线路上的接口的电阻
					//没在线上的值就等于0或本线路输入电压
					//线路名称和第一个一样时接在了1-2上,再通过接通的正反性判断放的位置
					if(((MutexSwitch)electronic).getCircuitInfo()[0].equals(this.getName())) {
						if(((MutexSwitch)electronic).getAccessState() == 0) {
							((MutexSwitch)electronic).getVs()[1] = this.getVAfter();
						}
						else if(((MutexSwitch)electronic).getAccessState() == 1) {
							((MutexSwitch)electronic).getVs()[1] = this.getVBefore();
						}
					}
					else if(((MutexSwitch)electronic).getCircuitInfo()[1].equals(this.getName())) {
						if(((MutexSwitch)electronic).getAccessState() == 0) {
							((MutexSwitch)electronic).getVs()[2] = this.getVAfter();
						}
						else if(((MutexSwitch)electronic).getAccessState() == 1) {
							((MutexSwitch)electronic).getVs()[2] = this.getVBefore();
						}
					}
				}
			}
			else {
				electronic.setCurrent(getCurrent());
			}
			
			electronic.renew();
			
		}
		
		//遍历此时所有灯,记录总亮度,赋给窗帘
		double brightnessTmp = 0;
		for(AbstractElectronic electronic : electronics) {
			try {
				brightnessTmp += ((AbstractLight)electronic).getBrightness();
			} catch (ClassCastException e) {
				// TODO: handle exception
			}
		}
		for(AbstractElectronic electronic : electronics) {
			try {
				((AbstractCurtain)electronic).setBrightness(brightnessTmp);
				electronic.renew();
			} catch (ClassCastException e) {
				// TODO: handle exception
			}
		}
		
	       
		
	}

	
	//按顺序展示信息
	public void showInfo() {
		
		
		if(this.getR() < 1) {
			
			System.out.println("short circuit error");
			
		}
		else {
			
			ArrayList<AbstractElectronic> electronicsTmp = new ArrayList<AbstractElectronic>();
			
			String[] names = {"K", "F", "L", "B", "R", "D", "A","H","S", "P"};
			
			//在目前所有的电子元件中按顺序找到元件并输出
			for(int i = 0; i < names.length; i++) {
				
				ArrayList<AbstractElectronic> electronicsTmp2 = new ArrayList<AbstractElectronic>();
				
				for(AbstractElectronic electronic : electronics) {
					if(electronic.getName().indexOf(names[i]) >= 0) {
						electronicsTmp2.add(electronic);
					}
				}
				
				Collections.sort(electronicsTmp2);
				
				electronicsTmp.addAll(electronicsTmp2);
				
			}
			
			for(AbstractElectronic electronic : electronicsTmp) {
				
				System.out.println("@" + electronic.getName() + ":" + electronic.getState());
				
			}
			
		}
		
	}
	
	//Getter,Setter方法们
	public ArrayList<AbstractElectronic> getCircuits() {
		return circuits;
	}
	
	public void setCircuits(ArrayList<AbstractElectronic> circuits) {
		this.circuits = circuits;
	}

	public ArrayList<AbstractElectronic> getElectronics() {
		return electronics;
	}

	public void setElectronics(ArrayList<AbstractElectronic> electronics) {
		this.electronics = electronics;
	}

	public ArrayList<AbstractElectronic> getSeriesCircuit() {
		return seriesCircuit;
	}

	public void setSeriesCircuit(ArrayList<AbstractElectronic> seriesCircuit) {
		this.seriesCircuit = seriesCircuit;
	}

	public ArrayList<AbstractElectronic> getDiagram() {
		return diagram;
	}

	public void setDiagram(ArrayList<AbstractElectronic> diagram) {
		this.diagram = diagram;
	}
	
}






class Input {

	private String input;

	public Input() {
		// TODO: implement
	}

	public Input(String input) {
		this.input = input;
	}
	
	public String getInput() {
		return input;
	}
	
	public void setInput(String input) {
		this.input = input;
	}
	
	//判断输入类型
	//1输入的是单条串联,2是并联电路组成信息,3是整个电路图链接组成信息,4是对元件操作信息,-1终止,else格式错误
	public int judgeInput() {
		
//		if(input.matches("#T[0-9]+:(\\[IN\\s+(([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT))){1}\\]\\s*){1}(\\[(([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3]\\s*)|((M|T)[0-9]+-(IN|OUT))){2,}\\]\\s*){0,}(\\[(([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT))){1,}\\s+(OUT)\\]\\s*){1}"))
//		if(input.matches("#T[0-9]+:(\\[IN\\s+(([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT))){1}\\]\\s*){1}(\\[(([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT))\\s*){2,}\\]\\s*){0,}(\\[(([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT))){1,}\\s+(OUT)\\]\\s*){1}"))
		//这个正则在测试工具上没问题啊,但是这里通不过,只能改下了
//		if(input.matches("#T[0-9]+:(\\[IN\\s+(([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT))){1}\\]\\s*){1}(\\[((([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT)))\\s*){2,}\\]\\s*){0,}(\\[(([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT))){1,}\\s+(OUT)\\]\\s*){1}"))
        if(input.matches("#T[0-9]+:\\[IN.*OUT\\]"))
			return 1;	
		//这里用\\s*显然是有问题的,当两个中间没空格就会误判为正确的同时在分割的时候也会出错
        else if(input.matches("#M[0-9]+:(\\[(T[0-9]+\\s*){1,}\\])"))
            return 2;
		//史上最长正则
//        else if(input.matches("#T[0-9]+:(\\[VCC\\s+((([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT)))\\s*){1,}\\]\\s*){1}(\\[((([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT)))\\s*){2,}\\]\\s*){0,}(\\[((([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT)))\\s*){1,}\\s+(GND)\\]\\s*){1}"))
        else if(input.matches("#T[0-9]+:\\[VCC.+GND\\]"))
//        else if(input.matches("#T[0-9]+:(\\[VCC\\s+(([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT))){1}\\]\\s*){1}(\\[((([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT)))\\s*){2,}\\]\\s*){0,}(\\[(([K|F|L|B|R|D|A|S|H|P][0-9]+-[1|2|3])|((M|T)[0-9]+-(IN|OUT))){1,}\\s+(GND)\\]\\s*){1}"))
            return 3;
        else if(input.matches("#((K[0-9]+)|(L[0-9]+:[0-9|.]+)|((F[0-9]+\\+)|(F[0-9]+-))|(H[0-9]+))"))
            return 4;
        else if(input.indexOf("end") >= 0)
            return -1;
        else
            return 0;
	}
	
	//返回单条串联电路和其中各个元件信息(没有删除重复)
	public String[] splitCircuit() {
		
		input = input.replaceAll("\\[|\\]", " ");
		input = input.replaceAll(":|#", "");
		
		//去掉开头结尾空格,防止数组分割多了
        input = input.replaceAll("^\\s+", "");
        input = input.replaceAll("\\s+$", "");
		
        //将各个元件名字分割
		String[] inputTmp = input.split("\\s+");
		
//		//去掉-和后面内容
//		for(int i = 0;i < inputTmp.length;i ++) {
//			
//			//这样会删除H的引脚信息,所以特殊判断下
//			if(!inputTmp[i].matches("H[0-9]+-[2|3]")) {
//				
//				inputTmp[i] = inputTmp[i].replaceAll("-.*","");
//			}
//			
//		}
		
		return inputTmp;
	}
	
	//返回并联电路组成信息
	public String[] splitSeriesCircuit(){
		
		//去掉分割符
		input = input.replaceAll("#|:", "");
		input = input.replaceAll("\\[|\\]", " ");
		//去掉前后空格防止分多了
		input = input.replaceAll("^\\s+", "");
		input = input.replaceAll("\\s+$", "");
		
		String[] inputTmp = input.split("\\s+");
		
		return inputTmp;
	}
	
	//返回整个电路图链接组成信息
	public String[] splitCircuitDiagram() {
		input = input.replaceAll("\\[|\\]", " ");
		input = input.replaceAll(":|#", "");
		
		//去掉开头结尾空格,防止数组分割多了
        input = input.replaceAll("^\\s+", "");
        input = input.replaceAll("\\s+$", "");
		
        //将各个元件名字分割
		String[] inputTmp = input.split("\\s+");
		
//		//去掉-和后面内容
//		for(int i = 0;i < inputTmp.length;i ++) {
//			inputTmp[i] = inputTmp[i].replaceAll("-.*","");
//		}
		
		return inputTmp;
	}
	
	//返回对元件的操作信息
	public String[] operation() {
		
		input = input.replaceAll("#","");
		String[] inputTmp = input.split(":");
		return inputTmp;
		
	}
	
	
}

class Controller {
	
	private CircuitDiagram circuitDiagram = new CircuitDiagram();
	private Input input = new Input();

	public Controller() {
		
	}
	
	
	//创建元件并放入单条串联电路(以下方法的操作均是对circuitDiagram进行的)
	public void inputCircuit() {
		
		String[] inputTmp = input.splitCircuit();
		Circuit circuitTmp = new Circuit();
		
		circuitTmp.setName(inputTmp[0]);
		String inputTmp2 = inputTmp[0].replaceAll("T", "");
		circuitTmp.setNum(Integer.parseInt(inputTmp2));
		
		//跳过IN等符号
		for(int i = 2; i < inputTmp.length; i += 2) {
			
			AbstractElectronic electronic = null;
			
			int hasFound = -1;

			String inputTmp3 = inputTmp[i].replaceAll("-.*","");
			
			//先在已经有的电子元件中查找,没有查到再新建
			for(AbstractElectronic electronic2 : this.circuitDiagram.getElectronics()) {
				
				if(electronic2.getName().equals(inputTmp3)) {
					hasFound = 1;
					electronic = electronic2;
					
					//特殊判断下是不是互斥开关
					if(inputTmp[i].indexOf("H") >= 0) {
						
						
						if(inputTmp[i + 1].matches("H[0-9]+-2") || inputTmp[i].matches("H[0-9]+-2")) {
							((MutexSwitch)electronic).getCircuitInfo()[0] = inputTmp[0];
						}
						else if(inputTmp[i + 1].matches("H[0-9]+-3") || inputTmp[i].matches("H[0-9]+-3")){
							((MutexSwitch)electronic).getCircuitInfo()[1] = inputTmp[0];
						}
						
					}
					
					//放入这段电路
					circuitTmp.getElectronics().add(electronic);
					break;
				}
				
			}
			
			//没找到就新建
			if(hasFound == -1) {
				
				if(inputTmp[i].indexOf("K") >= 0) {
					electronic = new Switch();
				}
				else if(inputTmp[i].indexOf("F") >= 0) {
					electronic = new BinningGovernor();
				}
				else if(inputTmp[i].indexOf("L") >= 0) {
					electronic = new ContinuousGovernor();
				}
				else if(inputTmp[i].indexOf("B") >= 0) {
					electronic = new Incandescent();
					electronic.setR(10);
				}
				else if(inputTmp[i].indexOf("R") >= 0) {
					electronic = new Fluorescent();
					electronic.setR(5);
				}
				else if(inputTmp[i].indexOf("D") >= 0) {
					electronic = new Fan();
					electronic.setR(20);
				}
				else if(inputTmp[i].indexOf("A") >= 0) {
					electronic = new FloorFan();
					electronic.setR(20);
				}
				else if(inputTmp[i].indexOf("S") >= 0) {
					electronic = new Curtain();
					electronic.setR(15);
				}
				else if(inputTmp[i].indexOf("P") >= 0) {
					electronic = new Diode();
					
					if(inputTmp[i + 1].matches("P[0-9]+-2")) {
						((Diode)electronic).setSwitchStatus(1);
					}
					else {
						((Diode)electronic).setSwitchStatus(0);
					}
					
				}
				//为互斥开关时需要看下是哪个接口
				//这个时候是还没有这个互斥开关的时候,可以直接看出接通状态是四个中的哪一种
				else if(inputTmp[i].indexOf("H") >= 0) {
					
					electronic = new MutexSwitch();
					
					if(inputTmp[i + 1].matches("H[0-9]+-2")) {
						((MutexSwitch)electronic).getCircuitInfo()[0] = inputTmp[0];
						((MutexSwitch)electronic).setAccessState(0);
					}
					else if(inputTmp[i].matches("H[0-9]+-2")) {
						((MutexSwitch)electronic).getCircuitInfo()[0] = inputTmp[0];
						((MutexSwitch)electronic).setAccessState(1);
					}
					else if(inputTmp[i + 1].matches("H[0-9]+-3")) {
						((MutexSwitch)electronic).getCircuitInfo()[1] = inputTmp[0];
						((MutexSwitch)electronic).setAccessState(0);
					}
					else if(inputTmp[i].matches("H[0-9]+-3")) {
						((MutexSwitch)electronic).getCircuitInfo()[1] = inputTmp[0];
						((MutexSwitch)electronic).setAccessState(1);
					}
					
				}
				//串联组成时由串联组成的时候
				else if(inputTmp[i].indexOf("T") >= 0) {
					int hasFound1 = -1;
					for(AbstractElectronic circuit : this.getCircuitDiagram().getCircuits()) {
						if(circuit.getName().equals(inputTmp3)) {
							hasFound1 = 1;
							electronic = circuit;
							break;
						}
					}
					
					//没找到时
					if(hasFound1 < 0) {
						
					}
					
				}
				
				
				//记录接入状态
				//除了互斥开关!!!
				if(inputTmp[i].matches("[^H][0-9]+-1")) {
					electronic.setAccessState(0);
				}
				else if(inputTmp[i].matches("[^H][0-9]+-2")){
					electronic.setAccessState(1);
				}
				
//				//除了互斥开关,其他都要把-后去掉
//				if(!inputTmp[i].matches("H[0-9]+-[2|3]")) {
					inputTmp[i] = inputTmp[i].replaceAll("-.*","");
//				}
				
				//需要先判断下不是In和out等,不然转数字会错
				if(inputTmp[i].indexOf("IN") < 0 && inputTmp[i].indexOf("OUT") < 0) {
					//设置名字和编号
					electronic.setName(inputTmp[i]);
					inputTmp[i] = inputTmp[i].replaceAll("K|F|L|B|R|D|A|S|H|T|P", "");
					electronic.setNum(Integer.parseInt(inputTmp[i]));
					
					circuitTmp.getElectronics().add(electronic);
					
					//元件放入
					circuitDiagram.getElectronics().add(electronic);
				}
				
				
			}
			
			
			
		}
		
		//更新下总电阻
		circuitTmp.renew();
		
		//单条串联放入
		circuitDiagram.getCircuits().add(circuitTmp);
		//也放入所有元件集合中
		circuitDiagram.getElectronics().add(circuitTmp);
		
	}
	
	//寻找串联电路放入并联中
	public void inputSeriesCircuit() {

		String[] inputTmp = input.splitSeriesCircuit();
		SeriesCircuit seriesCircuitTmp = new SeriesCircuit();
		
		seriesCircuitTmp.setName(inputTmp[0]);
		inputTmp[0] = inputTmp[0].replaceAll("M", "");
		seriesCircuitTmp.setNum(Integer.parseInt(inputTmp[0]));
		
		for(int i = 1; i < inputTmp.length; i ++) {
			
			int hasFound = 0;
			
			//对于每一个放在并联电路中的串联查找并放入
			for(AbstractElectronic circuit : circuitDiagram.getCircuits()) {
				if(circuit.getName().equals(inputTmp[i])) {
					hasFound = 1;
					seriesCircuitTmp.getCircuits().add(circuit);
					break;
				}
			}
			
			//对没找到的处理
			if(hasFound == 0) {
				
			}
			
			
		}
		
		//更新信息
		seriesCircuitTmp.renew();
		
		//并联电路放入
		circuitDiagram.getSeriesCircuit().add(seriesCircuitTmp);
		//也放入所有元件集合中
		circuitDiagram.getElectronics().add(seriesCircuitTmp);
		
	}
	
	//创建整个电路图
	public void inputCircuitDiagram() {
		
		String[] inputTmp = input.splitCircuitDiagram();
		Circuit circuitTmp = new Circuit();
		
//		circuitTmp.setName(inputTmp[0]);
//		inputTmp[0] = inputTmp[0].replaceAll("T", "");
//		circuitTmp.setNum(Integer.parseInt(inputTmp[0]));
		
		circuitTmp.setName(inputTmp[0]);
		String inputTmp2 = inputTmp[0].replaceAll("T", "");
		circuitTmp.setNum(Integer.parseInt(inputTmp2));
		
		
		//跳过VCC等符号
		for(int i = 2; i < inputTmp.length; i += 2) {
			
			AbstractElectronic electronic = null;
			
			int hasFound = -1;
			String inputTmp3 = inputTmp[i].replaceAll("-.*","");
			
			//先在已经有的电子元件中查找,没有查到再新建
			for(AbstractElectronic electronic2 : this.circuitDiagram.getElectronics()) {
				
				
				if(electronic2.getName().equals(inputTmp3)) {
					hasFound = 1;
					electronic = electronic2;
					
					//特殊判断下是不是互斥开关
					if(inputTmp[i].indexOf("H") >= 0) {
						
						if(inputTmp[i + 1].matches("H[0-9]+-2") || inputTmp[i].matches("H[0-9]+-2")) {
							((MutexSwitch)electronic).getCircuitInfo()[0] = inputTmp[0];
						}
						else if(inputTmp[i + 1].matches("H[0-9]+-3") || inputTmp[i].matches("H[0-9]+-3")){
							((MutexSwitch)electronic).getCircuitInfo()[1] = inputTmp[0];
						}
						
					}
					
					//放入这段电路
					circuitTmp.getElectronics().add(electronic);
					break;
				}
				
			}
			
			if(hasFound < 0) {
				
				if(inputTmp[i].indexOf("K") >= 0) {
					electronic = new Switch();
				}
				else if(inputTmp[i].indexOf("F") >= 0) {
					electronic = new BinningGovernor();
				}
				else if(inputTmp[i].indexOf("L") >= 0) {
					electronic = new ContinuousGovernor();
				}
				else if(inputTmp[i].indexOf("B") >= 0) {
					electronic = new Incandescent();
					electronic.setR(10);
				}
				else if(inputTmp[i].indexOf("R") >= 0) {
					electronic = new Fluorescent();
					electronic.setR(5);
				}
				else if(inputTmp[i].indexOf("D") >= 0) {
					electronic = new Fan();
					electronic.setR(20);
				}
				else if(inputTmp[i].indexOf("A") >= 0) {
					electronic = new FloorFan();
					electronic.setR(20);
				}
				else if(inputTmp[i].indexOf("S") >= 0) {
					electronic = new Curtain();
					electronic.setR(15);
				}
				else if(inputTmp[i].indexOf("M") >= 0) {
					
					int hasfound = 0;
					
					for(AbstractElectronic electronic1 : circuitDiagram.getSeriesCircuit()) {
						if(electronic1.getName().equals(inputTmp3)){
							electronic = electronic1;
							hasfound = 1;
							break;
						}
					}
					
					if(hasfound == 0) {
						
					}
				}
				//为互斥开关时需要看下是哪个接口
				else if(inputTmp[i].indexOf("H") >= 0) {
					
					electronic = new MutexSwitch();
					
					if(inputTmp[i + 1].matches("H[0-9]+-2")) {
						((MutexSwitch)electronic).getCircuitInfo()[0] = inputTmp[0];
						((MutexSwitch)electronic).setAccessState(0);
					}
					else if(inputTmp[i].matches("H[0-9]+-2")) {
						((MutexSwitch)electronic).getCircuitInfo()[0] = inputTmp[0];
						((MutexSwitch)electronic).setAccessState(1);
					}
					else if(inputTmp[i + 1].matches("H[0-9]+-3")) {
						((MutexSwitch)electronic).getCircuitInfo()[1] = inputTmp[0];
						((MutexSwitch)electronic).setAccessState(0);
					}
					else if(inputTmp[i].matches("H[0-9]+-3")) {
						((MutexSwitch)electronic).getCircuitInfo()[1] = inputTmp[0];
						((MutexSwitch)electronic).setAccessState(1);
					}
					
				}
				else if(inputTmp[i].indexOf("P") >= 0) {
					electronic = new Diode();
					
					if(inputTmp[i + 1].matches("P[0-9]+-2")) {
						((Diode)electronic).setSwitchStatus(1);
					}
					else {
						((Diode)electronic).setSwitchStatus(0);
					}
					
				}
				else if(inputTmp[i].indexOf("T") >= 0) {
					
					int hasfound = 0;
					
					for(AbstractElectronic electronic1 : circuitDiagram.getCircuits()) {
						if(electronic1.getName().equals(inputTmp3)){
							electronic = electronic1;
							hasfound = 1;
							break;
						}
					}
					
					if(hasfound == 0) {
						//应该给一个状态,让下述的放入不进行,否则会放入null出错
					}
					
				}
				
				//记录接入状态
				//除了互斥开关!!!
				if(inputTmp[i].matches("[^H][0-9]+-1")) {
					electronic.setAccessState(0);
				}
				else if(inputTmp[i].matches("[^H][0-9]+-2")){
					electronic.setAccessState(1);
				}
				
//				//除了互斥开关,其他都要把-后去掉
//				if(!inputTmp[i].matches("H[0-9]+-[2|3]")) {
					inputTmp[i] = inputTmp[i].replaceAll("-.*","");
//				}
				
				
				//需要先判断下不是Vcc和Gnd等,不然转数字会错
				if(inputTmp[i].indexOf("VCC") < 0 && inputTmp[i].indexOf("GND") < 0) {
					
					//设置名字编号
					electronic.setName(inputTmp[i]);
					inputTmp[i] = inputTmp[i].replaceAll("K|F|L|B|R|D|A|M|T|S|H|P", "");
					electronic.setNum(Integer.parseInt(inputTmp[i]));
					
					circuitTmp.getElectronics().add(electronic);
					
					//元件放入
					circuitDiagram.getElectronics().add(electronic);
					
				}
				
			}
			
			
			
		}
		
		//将本条电路放入,作为最终的电路图
		circuitDiagram.setDiagram(circuitTmp.getElectronics());
		circuitDiagram.setName(circuitTmp.getName());

//		//更新信息
//		circuitDiagram.renew();
		
	}
	
	//对元件进行操作
	public void operation() {
		
		String[] inputTmp = input.operation();
		
		//对开关或者互斥开关
		if(inputTmp[0].indexOf("K") >= 0 || inputTmp[0].indexOf("H") >= 0) {
			
			int hasFound = 0;
			
			for(AbstractElectronic electronic : circuitDiagram.getElectronics()) {
				if(electronic.getName().equals(inputTmp[0])){
					((AbstractSwitch)electronic).change();
					hasFound = 1;
				}
			}
			
			if(hasFound == 0) {
				
			}
		}
		//对换挡调速
		if(inputTmp[0].indexOf("F") >= 0) {
			
			int hasFound = 0;
			
			String inputTmp2 = inputTmp[0].replaceAll("\\+|-", "");
			
			for(AbstractElectronic electronic : circuitDiagram.getElectronics()) {
				if(electronic.getName().equals(inputTmp2)){
					if(inputTmp[0].indexOf("+") >= 0)
						((AbstractBinningGovernor)electronic).gearUp();
					else if(inputTmp[0].indexOf("-") >= 0)
						((AbstractBinningGovernor)electronic).gearDown();
					hasFound = 1;
				}
			}
			
			if(hasFound == 0) {
				
			}
		}
		//对连续调速
		if(inputTmp[0].indexOf("L") >= 0) {
			
			int hasFound = 0;
			
			for(AbstractElectronic electronic : circuitDiagram.getElectronics()) {
				if(electronic.getName().equals(inputTmp[0])){
					((AbstractContinuousGovernor)electronic).setGear(Double.parseDouble(inputTmp[1]));
					hasFound = 1;
				}
			}
			
			if(hasFound == 0) {
				
			}
		}
		
//        //更新信息
//        circuitDiagram.renew();
		
	}
	
	//展示信息
	public void showInfo() {
		
        //更新信息
        circuitDiagram.renew();
		
		circuitDiagram.showInfo();
	}
	
	
	//getter,setter方法们

	public CircuitDiagram getCircuitDiagram() {
		return circuitDiagram;
	}

	public void setCircuitDiagram(CircuitDiagram circuitDiagram) {
		this.circuitDiagram = circuitDiagram;
	}

	public Input getInput() {
		return input;
	}
	
	public void setInput(Input input) {
		this.input = input;
	}
	
	
	
}

posted @ 2024-06-29 15:31  cdz_hy  阅读(55)  评论(0)    收藏  举报