ZBWzhao

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一,前言
本次blog是对Java作业pta第7~8次作业的总结,主要总结最后一题,包括对这二次作业的难度,知识点进行概括,对作业的源码进行分析,并且分享自己的心得与改进建议。
作业知识点,题量,难度总结
第七次作业
第七次作业主要考察了类之间关系的设计,正则表达式的匹配与应用以及开闭原则,相比于上一题主要加入了新的电路和设备
难度:相对于前两次作业难度更大,要考虑到的东西也更多
第八次作业
知识点:电学的一些基本知识,list的使用
难度很大,做半天做不出来

二,设计与分析

7-1 家居强电电路模拟程序-3

代码:

点击查看代码
import java.util.LinkedHashMap;
import java.util.Map;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Comparator;

class Electric {// 主电器
    public String s;// 类型
    public String id;// 类型id
    public double shuV = 220;// 电压
    public String ofopen = "turned on";// 开关K的状态
    public int speed = 0;//
    public double lin = 0.00;//
    public int resistance;// 电阻

    public Electric(String s, String id) {
        this.s = s;
        this.id = id;
    }

    public void display() {

    }

    public void regulate(String vs) {

    }

    public void reshuV(double shuop) {

    }

    public int fanhui() {// 比较器用到的
        if(s.equals("H"))
            return 9;
        else if(s.equals("S"))
            return 8;
        else if (s.equals("K"))
            return 7;
        else if (s.equals("F"))
            return 6;
        else if (s.equals("L"))
            return 5;
        else if (s.equals("B"))
            return 4;
        else if (s.equals("R"))
            return 3;
        else if (s.equals("D"))
            return 2;
        else if (s.equals("A"))
            return 1;
        else
            return 0;
    }
}

class chuanglian extends Electric {// 吊扇


    public chuanglian(String id) {
        super("S", id);
    }

    public void display() {
        double a = 0;
        if (shuV >= 0 && shuV < 50)
            a = 1;
        else if (shuV >= 100 && shuV < 200)
            a = 0.8;
        else if (shuV >=200&&shuV<300)
            a = 0.6;
        else if(shuV>=300&&shuV<400)
            a=0.4;
        else if(shuV>=400)
            a=0;
        System.out.printf("@D%s:%d\n", id, a);
    }

    public void reshuV(double shuop) {
        shuV = shuop;
    }

}

class huchi extends Electric {// 开关

    public huchi(String id) {
        super("H", id);
    }

    public void display() {
        System.out.printf("@H%s:%s\n", id, ofopen);
    }

    public void regulate(String vs) {
    }

    public void reshuV(double shuop) {
        if (ofopen.equals("turned on")) {
            shuV = 0;
            // System.out.println(shuV);
        } else if (ofopen.equals("closed"))
            shuV = shuop;
    }
}

class Kaiguan extends Electric {// 开关

    public Kaiguan(String id) {
        super("K", id);
    }

    public void display() {
        System.out.printf("@K%s:%s\n", id, ofopen);
    }

    public void regulate(String vs) {
    }

    public void reshuV(double shuop) {
        if (ofopen.equals("turned on")) {
            shuV = 0;
            // System.out.println(shuV);
        } else if (ofopen.equals("closed"))
            shuV = shuop;
    }
}

class Fendang extends Electric {// 分档
    public Fendang(String id) {
        super("F", id);
    }

    public void display() {
        System.out.printf("@F%s:%d\n", id, speed);
    }

    public void regulate(String vs) {// 分档调节
        if (vs.equals("+") && speed < 3) {
            speed++;
        } else if (vs.equals("-") && speed > 0) {
            speed--;
        }
    }

    public void reshuV(double shuop) {
        shuV = speed * 0.3 * shuop;
    }
}

class Lianxu extends Electric {// 连续


    public Lianxu(String id) {
        super("L", id);
    }

    public void display() {
        String siop = String.format("@L%s:%.2f", id, lin);
        System.out.printf("%s\n", siop);
    }

    public void regulate(String vs) {
        lin = Double.parseDouble(vs);
    }

    public void reshuV(double shuop) {
        shuV = lin * shuop;
    }
}

class Baichi extends Electric {// 白炽灯

    public Baichi(String id) {
        super("B", id);
    }

    public void display() {
        int a = 0;
        if (shuV >= 0 && shuV <= 9) {
            a = 0;
        } else if (shuV >= 10 && shuV <= 219) {
            a = (int) ((5 * shuV) / 7 + 43);
        } else if (shuV == 220) {
            a = 200;
        }
        System.out.printf("@B%s:%d\n", id, a);
    }

    public void reshuV(double shuop) {
        shuV = shuop;
    }

}

class Riguang extends Electric {// 日光灯


    public Riguang(String id) {
        super("R", id);
    }

    public void display() {
        int a = 0;
        if (shuV == 0)
            a = 0;
        else
            a = 180;
        System.out.printf("@R%s:%d\n", id, a);
    }

    public void reshuV(double shuop) {
        shuV = shuop;
    }
}

class Diaoshan extends Electric {// 吊扇


    public Diaoshan(String id) {
        super("D", id);
    }

    public void display() {
        int a = 0;
        if (shuV >= 0 && shuV < 80)
            a = 0;
        else if (shuV >= 80 && shuV <= 150)
            a = (int) ((shuV - 80) * 4 + 80);
        else if (shuV > 150)
            a = 360;
        System.out.printf("@D%s:%d\n", id, a);
    }

    public void reshuV(double shuop) {
        shuV = shuop;
    }

}

class Luodi extends Electric {// 落地扇

    public Luodi(String id) {
        super("A", id);
    }

    public void display() {
        int a = 0;
        if (shuV >= 0 && shuV < 80)
            a = 0;
        else if (shuV >= 80 && shuV <= 99)
            a = 80;
        else if (shuV >= 100 && shuV <= 119)
            a = 160;
        else if (shuV >= 120 && shuV <= 139)
            a = 260;
        else if (shuV > 140)
            a = 360;
        System.out.printf("@A%s:%d\n", id, a);
    }

    public void reshuV(double shuop) {
        shuV = shuop;
    }

}

class Bi extends Electric {
    public Bi(String ml) {
        super("M", ml);
    }
}

class Chuan extends Electric{
    public Chuan(String ml){
        super("T",ml);
    }
}
public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(System.in);
        String s = "", pop = "", zong = "", chuanxu = "", bingxu = "";
        int i;
        ArrayList<String> connection = new ArrayList<>();
        ArrayList<String> connection1 = new ArrayList<>();
        ArrayList<String> chw = new ArrayList<>();
        while (true) {
            s = in.nextLine().trim();
            if ("end".equals(s))
                break;
            if (s.startsWith("#T"))// 将每一条路线存进去
            {
                Pattern pattern = Pattern.compile("#(.*):(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    chuanxu = String.valueOf(matcher.group(1));
                    pop = String.valueOf(matcher.group(2));
                }
                connection.add(chuanxu + ":" + pop);
            } else if (s.startsWith("#M"))// 将并联的电路放进去,后面进行判断
            {
                Pattern pattern = Pattern.compile("#(.*):(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    bingxu = String.valueOf(matcher.group(1));
                    pop = String.valueOf(matcher.group(2));
                }
                connection1.add(bingxu + " " + pop);
            } else if (s.startsWith("#K"))// 对开关进行储存后面进行更改
            {
                Pattern pattern = Pattern.compile("#(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    pop = String.valueOf(matcher.group(1));
                }
                chw.add(pop);
            } else if (s.startsWith("#F")) {
                Pattern pattern = Pattern.compile("#(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    pop = String.valueOf(matcher.group(1));
                }
                chw.add(pop);
            } else if (s.startsWith("#L")) {
                Pattern pattern = Pattern.compile("#(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    pop = String.valueOf(matcher.group(1));
                }
                chw.add(pop);
            }else if (s.startsWith("#S"))// 对开关进行储存后面进行更改
            {
                Pattern pattern = Pattern.compile("#(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    pop = String.valueOf(matcher.group(1));
                }
                chw.add(pop);
            }
        }
        int kllk = 0, mmm = 0;
        String z1 = "", zm = "";
        Map<String, Map<String, Electric>> mac = new HashMap<>();
        Map<String, Electric> zhu = new LinkedHashMap<>();
        Map<String, Electric> map4 = new HashMap<>();
        for (mmm = 0; mmm < connection.size(); mmm++) {// 将并联的与串联的电路分开储存

            int vb1 = 0, vb2 = 0;
            Map<String, Electric> map1 = new LinkedHashMap<>();
            z1 = connection.get(mmm);

            Pattern pattern = Pattern.compile("(.*):(.*)");
            Matcher matcher = pattern.matcher(z1);
            while (matcher.find()) {
                zm = String.valueOf(matcher.group(1));
                pop = String.valueOf(matcher.group(2));
            }
            String[] partsl = pop.split("]");
            String jk = "", jk1 = "";
            for (kllk = 0; kllk < partsl.length; kllk++) {
                jk1 = partsl[0].trim();
                jk = partsl[kllk].trim();
                // System.out.println(jk);
                if (jk1.startsWith("[IN")) {
                    vb1 = 1;
                    String[] parts4 = jk.split(" ");
                    if (parts4[1].equals("OUT"))
                        break;
                    else {
                        String[] pinyin = parts4[1].split("-");
                        String dkey = pinyin[0].trim();
                        map1 = complain(dkey, parts4[1], map1);
                        //System.out.println(dkey+" "+parts4[1]);
                    }
                } else if (jk1.startsWith("[VCC")) {
                    vb2 = 1;
                    String[] parts4 = jk.split(" ");
                    if (parts4[1].equals("GND"))
                        break;
                    else {
                        String[] pinyin = parts4[1].split("-");
                        String dkey = pinyin[0].trim();
                        map1 = complain(dkey, parts4[1], map1);
                        //System.out.println(dkey+" "+parts4[1]);
                    }
                }
            }

            if (vb1 == 1)
                mac.put(zm, map1);// 并联电路

            if (vb2 == 1) {
                zhu = map1;// 主电路
            }
        }
        Map<String, Map<String, Electric>> maps = new HashMap<>();
        for (mmm = 0; mmm < connection1.size(); mmm++) {// 判断那些事并联电路并存储
            String k11 = connection1.get(mmm).substring(0, 2);
            Pattern pattern = Pattern.compile("(.*)\\s\\[(.*)]");
            Matcher matcher = pattern.matcher(connection1.get(mmm));
            while (matcher.find()) {
                pop = String.valueOf(matcher.group(2));
            }
            String[] partsl = pop.split(" ");
            for (int yq = 0; yq < partsl.length; yq++) {
                for (Map.Entry<String, Map<String, Electric>> entry1 : mac.entrySet()) {
                    if (entry1.getKey().equals(partsl[yq])) {
                        maps.put(k11 + "" + yq, entry1.getValue());
                    }
                }
            }
        }
        //System.out.println(maps.size());
        int flag = 0;
        int flag1 = 0;
        int flag2 = 0;
        int lll = 0, mmm1 = 0;
        double rui = 220;
        for (i = 0; i < chw.size(); i++) {
            String sid = chw.get(i);
            for (Entry<String, Map<String, Electric>> entry : maps.entrySet()){
                if (sid.startsWith("K")) {
                    //System.out.println("1");
                    String diu = sid.substring(0, 2);
                    Electric electric = entry.getValue().get(diu);
                    if (electric != null) {
                        if (electric.ofopen.equals("turned on"))
                            electric.ofopen = "closed";
                        else
                            electric.ofopen = "turned on";
                        break;
                    }
                }
                else if (sid.startsWith("S")) {
                    //System.out.println("1");
                    String diu = sid.substring(0, 2);
                    Electric electric = entry.getValue().get(diu);
                    if (electric != null) {
                        if (electric.ofopen.equals("turned on"))
                            electric.ofopen = "closed";
                        else
                            electric.ofopen = "turned on";
                        break;
                    }
                }
                else if (sid.startsWith("F")) {
                    String diu = sid.substring(0, 2);
                    String vs = sid.substring(2);
                    Electric electric = entry.getValue().get(diu);
                    if (electric != null) {
                        electric.regulate(vs);
                        flag2++;
                        if (vs.equals("+")) {
                            flag++;
                            if (flag >= 3) {
                                flag = 3;
                            }
                        }
                        if (vs.equals("-")) {
                            flag--;
                            if (flag <= 0) {
                                flag = 0;
                            }
                        }
                        break;
                    }
                }
                else if (sid.startsWith("L")) {
                    String diu = sid.substring(0, 2);
                    String vs = sid.substring(3);
                    Electric electric = entry.getValue().get(diu);
                    if (electric != null) {
                        electric.regulate(vs);
                        // electric.reshuV(rui);
                        // rui = electric.shuV;
                        //System.out.println("");
                        break;
                    }
                }
            }
            for (Entry<String, Electric> entry : zhu.entrySet()) {
                if (sid.startsWith("K")) {
                    String diu = sid.substring(0, 2);
                    Electric electric = zhu.get(diu);
                    //electric.display();
                    if (electric != null) {
                        if (electric.ofopen.equals("turned on"))
                            electric.ofopen = "closed";
                        else
                            electric.ofopen = "turned on";
                        break;
                    }
                }
                else if (sid.startsWith("F")) {
                    String diu = sid.substring(0, 2);
                    String vs = sid.substring(2);
                    Electric electric = zhu.get(diu);
                    if (electric != null) {
                        //System.out.println("1");
                        electric.regulate(vs);
                        flag2++;
                        if (vs.equals("+")) {
                            flag++;
                            if (flag >= 3) {
                                flag = 3;
                            }
                        }
                        if (vs.equals("-")) {
                            flag--;
                            if (flag <= 0) {
                                flag = 0;
                            }
                        }
                        break;
                    }
                }
                else if (sid.startsWith("L")) {
                    String diu = sid.substring(0, 2);
                    String vs = sid.substring(3);
                    Electric electric = zhu.get(diu);
                    if (electric != null) {
                        //System.out.println("1");
                        electric.regulate(vs);
                        // electric.reshuV(rui);
                        break;
                    }
                }
            }
        }
        double siop = rui;
        double a[] = new double[maps.size()];// 算电阻
        double b[] = new double[zhu.size()];
        i = 0;
        int hjh=0;
        int iuy=0;
        Map<String, Electric> osp = new HashMap<>();
        for (Entry<String, Map<String, Electric>> entry : maps.entrySet()) {
            flag = 0;
            a[i] = 0;
            for (Entry<String, Electric> entry1 : entry.getValue().entrySet()) {
                if (entry1.getKey().startsWith("K")) {
                    if (entry1.getValue().ofopen.equals("turned on"))
                        flag = 1;
                    if(entry1.getValue().ofopen.equals("closed"))
                        hjh++;
                }
            }
            if(hjh==entry.getValue().size())
                a[i]=0;
            if (flag == 1)
                a[i] = -1;
            else {
                for (Entry<String, Electric> entry1 : entry.getValue().entrySet()) {
                    a[i] = a[i] + 1.0*entry1.getValue().resistance;
                }
            }
            i++;
        }
        double lpl1=0;
        double kapok=0;
        for (i = 0; i < a.length; i++) {

            if(a[i]==0){
                lpl1=0;
                break;
            }
            else{
                if (a[i]==-1)
                    continue;
                else {
                    kapok = 1.0 / a[i] + kapok;
                    lpl1=kapok;
                }

            }
        }
        b[0]=0;
        for (Entry<String, Electric> entry : zhu.entrySet()) {
            flag=0;
            if (entry.getKey().startsWith("K")) {
                if (entry.getValue().ofopen.equals("turned on")){
                    flag = 1;
                }
                if(flag==1){
                    b[0] = 0;break;
                }
            }
            b[0] = b[0] + entry.getValue().resistance;
        }

        double chuanR, bingR;
        chuanR = 1.0*b[0];

        if (lpl1 == 0)
            bingR = 0;
        else
            bingR = 1.0/lpl1;

        double chuanV = 0, bingV = 0;

        chuanV = rui * chuanR / (bingR + chuanR);
        bingV = rui * bingR / (bingR + chuanR);

        Map<String, Electric> maso = new HashMap<>();
        for (Entry<String, Electric> entry : zhu.entrySet()) {
            String parts8 = entry.getKey();
            s = parts8;
            if(b[0]==0&&flag==1){
                siop=0;
                rui=0;
                chuanV=0;
                bingV=0;
            }
            if (s.startsWith("K")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(rui);
                rui = elc1.shuV;
                chuanV = 1.0*rui * chuanR / (bingR + chuanR);
                bingV = 1.0*rui * bingR / (bingR + chuanR);
                maso.put(s, elc1);
            }

            else if (s.startsWith("B")) {
                //System.out.println(10*chuanV/chuanR);
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(10*chuanV/chuanR);
                maso.put(s, elc1);
            }

            else if (s.startsWith("R")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(5*chuanV/chuanR);
                maso.put(s, elc1);
            }

            else if (s.startsWith("D")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(20*chuanV/chuanR);
                maso.put(s, elc1);
                // System.out.println(elc1.shuV);
            }

            else if (s.startsWith("A")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(20*chuanV/chuanR);
                maso.put(s, elc1);
            }

            else if (s.startsWith("S")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(15*chuanV/chuanR);
                maso.put(s, elc1);
            }

            else if (s.startsWith("L")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(rui);
                //System.out.println(rui);
                rui = elc1.shuV;

                chuanV = rui * chuanR / (bingR + chuanR);
                bingV = rui * bingR / (bingR + chuanR);
                //System.out.println(chuanV+" "+bingV);
                maso.put(s, elc1);
            }

            else if (s.startsWith("H")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(rui);
                //System.out.println(rui);
                rui = elc1.shuV;

                chuanV = rui * chuanR / (bingR + chuanR);
                bingV = rui * bingR / (bingR + chuanR);
                //System.out.println(chuanV+" "+bingV);
                maso.put(s, elc1);
            }

            else if (s.startsWith("F")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(rui);
                rui=elc1.shuV;
                //System.out.println(rui);
                chuanV = rui * chuanR / (bingR + chuanR);
                bingV = rui * bingR / (bingR + chuanR);
                maso.put(s, elc1);
            }
            else if (s.startsWith("M")) {
                i=0;
                for (Entry<String, Map<String, Electric>> entry6 : maps.entrySet()) {
                    if(lpl1==0){
                        siop=0;
                        for (Entry<String, Electric> entry1 : osp.entrySet()) {
                            parts8 = entry1.getKey();
                            s = parts8;
                            if (s.startsWith("K")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                if (elc1.ofopen.startsWith("t")) {
                                    siop = 0;
                                }
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("B")) {
                                //System.out.println(10*siop/a[i]);
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                // System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("R")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("D")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                //System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("A")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                // System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("L")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("F")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("S")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("H")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }
                        }
                    }
                    siop = bingV;
                    osp = entry6.getValue();
                    if(a[i]!=-1){
                        for (Entry<String, Electric> entry1 : osp.entrySet()) {
                            parts8 = entry1.getKey();
                            s = parts8;
                            if (s.startsWith("K")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                if (elc1.ofopen.startsWith("t")) {
                                    siop = 0;
                                }
                                //System.out.println(parts8);
                                maso.put(s, elc1);
                                //System.out.println(siop);
                            }

                            else if (s.startsWith("B")) {
                                //System.out.println(10*siop/a[i]);
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(10*siop/a[i]);
                                // System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("R")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(5*siop/a[i]);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("D")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(20*siop/a[i]);
                                //System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("A")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(20*siop/a[i]);
                                // System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("L")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("H")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }
                            
                            else if (s.startsWith("F")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("S")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(15*siop/a[i]);
                                maso.put(s, elc1);
                            }
                        }
                    }
                    else {
                        siop=0;
                        for (Entry<String, Electric> entry1 : osp.entrySet()) {
                            parts8 = entry1.getKey();
                            s = parts8;

                            if (s.startsWith("K")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.shuV=0;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("B")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                // System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("R")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("D")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                //System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("A")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                //System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("L")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("F")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("S")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("H")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }
                        }
                    }
                    i++;
                }
            }
        }

        ArrayList<Electric> arraylist6 = new ArrayList<>();
        for (Map.Entry<String, Electric> pose : maso.entrySet()) {
            arraylist6.add(pose.getValue());
        }

        Comparator<Electric> numberComparator = new Comparator<Electric>() {
            public int compare(Electric o1, Electric o2) {
                int a = o1.fanhui();
                int b = o2.fanhui();
                int c = Integer.parseInt(o1.id);
                int d = Integer.parseInt(o2.id);
                if (a > b)
                    return -1;
                else if (a == b) {
                    if (c > d)
                        return 1;
                    else
                        return -1;
                } else
                    return 1;
            }
        };
        Collections.sort(arraylist6, numberComparator);
        for (Electric suixcs : arraylist6) {
            suixcs.display();
        }
    }

    public static Map<String, Electric> complain(String dkey, String s, Map<String, Electric> map1) {
        String jkj = String.valueOf(s.charAt(1));

        if (s.startsWith("K")) {
            Electric tric = new Kaiguan(jkj);
            tric.resistance = 0;
            map1.put(dkey, tric);
        }

        else if (s.startsWith("F")) {
            Electric tric = new Fendang(jkj);
            tric.resistance = 0;
            map1.put(dkey, tric);
        }

        else if (s.startsWith("L")) {
            Electric tric = new Lianxu(jkj);
            tric.resistance = 0;
            map1.put(dkey, tric);
        }
        else if (s.startsWith("B")) {
            Electric tric = new Baichi(jkj);
            tric.resistance = 10;
            map1.put(dkey, tric);

        } else if (s.startsWith("R")) {
            Electric tric = new Riguang(jkj);
            tric.resistance = 5;
            map1.put(dkey, tric);

        } else if (s.startsWith("D")) {
            Electric tric = new Diaoshan(jkj);
            tric.resistance = 20;
            map1.put(dkey, tric);
        }
        else if (s.startsWith("A")) {
            Electric tric = new Luodi(jkj);
            tric.resistance = 20;
            map1.put(dkey, tric);
        }
        else if (s.startsWith("M")) {
            Electric tric = new Bi(jkj);
            tric.resistance = 0;
            map1.put(dkey,tric);
        }
        else if (s.startsWith("T")) {
            Electric tric = new Chuan(jkj);
            tric.resistance = 0;
            map1.put(dkey,tric);
        }
        else if (s.startsWith("S")) {
            Electric tric = new chuanglian(jkj);
            tric.resistance = 0;
            map1.put(dkey, tric);
        }
        else if (s.startsWith("H")) {
            Electric tric = new huchi(jkj);
            tric.resistance = 0;
            map1.put(dkey, tric);
        }
        return map1;
    }
}

设置了Lamp类,BeControlled类,Curtain类

Lamp类:新增灯具和线路,
BeControlled类:新增电路
Curtain类:设置窗帘

1.写这题时想着利用正则表达式提取设置互斥开关对象的引脚对象,
在生成对象后就对其状态进行设置,如果是1-2引脚就闭合
2.窗帘问题在于与其他电器和线路的关联,需要对数据进行读取然后初始化,要对窗帘的开闭进行计算和遍历

踩坑心得:
1.需要注意互斥开关只输出1-2的引脚的开闭状态,1-3是不输出的,要加以限制
2.线路中的1-2和3-4引脚需要设置搜索,否则最后输出的数据会错误
改进建议:
对于结构上可以进一步优化,对每一种类型的电器进行抽象,后面可以更加简洁的使用这些电器

7-1 家居强电电路模拟程序-4

点击查看代码
import java.util.LinkedHashMap;
import java.util.Map;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Comparator;

class Electric {// 主电器
    public String s;// 类型
    public String id;// 类型id
    public double shuV = 220;// 电压
    public String ofopen = "turned on";// 开关K的状态
    public int speed = 0;//
    public double lin = 0.00;//
    public int resistance;// 电阻

    public Electric(String s, String id) {
        this.s = s;
        this.id = id;
    }

    public void display() {

    }

    public void regulate(String vs) {

    }

    public void reshuV(double shuop) {

    }

    public int fanhui() {// 比较器用到的
        if(s.equals("H"))
            return 9;
        else if(s.equals("S"))
            return 8;
        else if (s.equals("K"))
            return 7;
        else if (s.equals("F"))
            return 6;
        else if (s.equals("L"))
            return 5;
        else if (s.equals("B"))
            return 4;
        else if (s.equals("R"))
            return 3;
        else if (s.equals("D"))
            return 2;
        else if (s.equals("A"))
            return 1;
        else
            return 0;
    }
}

class chuanglian extends Electric {// 吊扇


    public chuanglian(String id) {
        super("S", id);
    }

    public void display() {
        double a = 0;
        if (shuV >= 0 && shuV < 50)
            a = 1;
        else if (shuV >= 100 && shuV < 200)
            a = 0.8;
        else if (shuV >=200&&shuV<300)
            a = 0.6;
        else if(shuV>=300&&shuV<400)
            a=0.4;
        else if(shuV>=400)
            a=0;
        System.out.printf("@D%s:%d\n", id, a);
    }

    public void reshuV(double shuop) {
        shuV = shuop;
    }

}

class huchi extends Electric {// 开关

    public huchi(String id) {
        super("H", id);
    }

    public void display() {
        System.out.printf("@H%s:%s\n", id, ofopen);
    }

    public void regulate(String vs) {
    }

    public void reshuV(double shuop) {
        if (ofopen.equals("turned on")) {
            shuV = 0;
            // System.out.println(shuV);
        } else if (ofopen.equals("closed"))
            shuV = shuop;
    }
}

class Kaiguan extends Electric {// 开关

    public Kaiguan(String id) {
        super("K", id);
    }

    public void display() {
        System.out.printf("@K%s:%s\n", id, ofopen);
    }

    public void regulate(String vs) {
    }

    public void reshuV(double shuop) {
        if (ofopen.equals("turned on")) {
            shuV = 0;
            // System.out.println(shuV);
        } else if (ofopen.equals("closed"))
            shuV = shuop;
    }
}

class Fendang extends Electric {// 分档
    public Fendang(String id) {
        super("F", id);
    }

    public void display() {
        System.out.printf("@F%s:%d\n", id, speed);
    }

    public void regulate(String vs) {// 分档调节
        if (vs.equals("+") && speed < 3) {
            speed++;
        } else if (vs.equals("-") && speed > 0) {
            speed--;
        }
    }

    public void reshuV(double shuop) {
        shuV = speed * 0.3 * shuop;
    }
}

class Lianxu extends Electric {// 连续


    public Lianxu(String id) {
        super("L", id);
    }

    public void display() {
        String siop = String.format("@L%s:%.2f", id, lin);
        System.out.printf("%s\n", siop);
    }

    public void regulate(String vs) {
        lin = Double.parseDouble(vs);
    }

    public void reshuV(double shuop) {
        shuV = lin * shuop;
    }
}

class Baichi extends Electric {// 白炽灯

    public Baichi(String id) {
        super("B", id);
    }

    public void display() {
        int a = 0;
        if (shuV >= 0 && shuV <= 9) {
            a = 0;
        } else if (shuV >= 10 && shuV <= 219) {
            a = (int) ((5 * shuV) / 7 + 43);
        } else if (shuV == 220) {
            a = 200;
        }
        System.out.printf("@B%s:%d\n", id, a);
    }

    public void reshuV(double shuop) {
        shuV = shuop;
    }

}

class Riguang extends Electric {// 日光灯


    public Riguang(String id) {
        super("R", id);
    }

    public void display() {
        int a = 0;
        if (shuV == 0)
            a = 0;
        else
            a = 180;
        System.out.printf("@R%s:%d\n", id, a);
    }

    public void reshuV(double shuop) {
        shuV = shuop;
    }
}

class Diaoshan extends Electric {// 吊扇


    public Diaoshan(String id) {
        super("D", id);
    }

    public void display() {
        int a = 0;
        if (shuV >= 0 && shuV < 80)
            a = 0;
        else if (shuV >= 80 && shuV <= 150)
            a = (int) ((shuV - 80) * 4 + 80);
        else if (shuV > 150)
            a = 360;
        System.out.printf("@D%s:%d\n", id, a);
    }

    public void reshuV(double shuop) {
        shuV = shuop;
    }

}

class Luodi extends Electric {// 落地扇

    public Luodi(String id) {
        super("A", id);
    }

    public void display() {
        int a = 0;
        if (shuV >= 0 && shuV < 80)
            a = 0;
        else if (shuV >= 80 && shuV <= 99)
            a = 80;
        else if (shuV >= 100 && shuV <= 119)
            a = 160;
        else if (shuV >= 120 && shuV <= 139)
            a = 260;
        else if (shuV > 140)
            a = 360;
        System.out.printf("@A%s:%d\n", id, a);
    }

    public void reshuV(double shuop) {
        shuV = shuop;
    }

}

class Bi extends Electric {
    public Bi(String ml) {
        super("M", ml);
    }
}

class Chuan extends Electric{
    public Chuan(String ml){
        super("T",ml);
    }
}
public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(System.in);
        String s = "", pop = "", zong = "", chuanxu = "", bingxu = "";
        int i;
        ArrayList<String> connection = new ArrayList<>();
        ArrayList<String> connection1 = new ArrayList<>();
        ArrayList<String> chw = new ArrayList<>();
        while (true) {
            s = in.nextLine().trim();
            if ("end".equals(s))
                break;
            if (s.startsWith("#T"))// 将每一条路线存进去
            {
                Pattern pattern = Pattern.compile("#(.*):(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    chuanxu = String.valueOf(matcher.group(1));
                    pop = String.valueOf(matcher.group(2));
                }
                connection.add(chuanxu + ":" + pop);
            } else if (s.startsWith("#M"))// 将并联的电路放进去,后面进行判断
            {
                Pattern pattern = Pattern.compile("#(.*):(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    bingxu = String.valueOf(matcher.group(1));
                    pop = String.valueOf(matcher.group(2));
                }
                connection1.add(bingxu + " " + pop);
            } else if (s.startsWith("#K"))// 对开关进行储存后面进行更改
            {
                Pattern pattern = Pattern.compile("#(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    pop = String.valueOf(matcher.group(1));
                }
                chw.add(pop);
            } else if (s.startsWith("#F")) {
                Pattern pattern = Pattern.compile("#(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    pop = String.valueOf(matcher.group(1));
                }
                chw.add(pop);
            } else if (s.startsWith("#L")) {
                Pattern pattern = Pattern.compile("#(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    pop = String.valueOf(matcher.group(1));
                }
                chw.add(pop);
            }else if (s.startsWith("#S"))// 对开关进行储存后面进行更改
            {
                Pattern pattern = Pattern.compile("#(.*)");
                Matcher matcher = pattern.matcher(s);
                while (matcher.find()) {
                    pop = String.valueOf(matcher.group(1));
                }
                chw.add(pop);
            }
        }
        int kllk = 0, mmm = 0;
        String z1 = "", zm = "";
        Map<String, Map<String, Electric>> mac = new HashMap<>();
        Map<String, Electric> zhu = new LinkedHashMap<>();
        Map<String, Electric> map4 = new HashMap<>();
        for (mmm = 0; mmm < connection.size(); mmm++) {// 将并联的与串联的电路分开储存

            int vb1 = 0, vb2 = 0;
            Map<String, Electric> map1 = new LinkedHashMap<>();
            z1 = connection.get(mmm);

            Pattern pattern = Pattern.compile("(.*):(.*)");
            Matcher matcher = pattern.matcher(z1);
            while (matcher.find()) {
                zm = String.valueOf(matcher.group(1));
                pop = String.valueOf(matcher.group(2));
            }
            String[] partsl = pop.split("]");
            String jk = "", jk1 = "";
            for (kllk = 0; kllk < partsl.length; kllk++) {
                jk1 = partsl[0].trim();
                jk = partsl[kllk].trim();
                // System.out.println(jk);
                if (jk1.startsWith("[IN")) {
                    vb1 = 1;
                    String[] parts4 = jk.split(" ");
                    if (parts4[1].equals("OUT"))
                        break;
                    else {
                        String[] pinyin = parts4[1].split("-");
                        String dkey = pinyin[0].trim();
                        map1 = complain(dkey, parts4[1], map1);
                        //System.out.println(dkey+" "+parts4[1]);
                    }
                } else if (jk1.startsWith("[VCC")) {
                    vb2 = 1;
                    String[] parts4 = jk.split(" ");
                    if (parts4[1].equals("GND"))
                        break;
                    else {
                        String[] pinyin = parts4[1].split("-");
                        String dkey = pinyin[0].trim();
                        map1 = complain(dkey, parts4[1], map1);
                        //System.out.println(dkey+" "+parts4[1]);
                    }
                }
            }

            if (vb1 == 1)
                mac.put(zm, map1);// 并联电路

            if (vb2 == 1) {
                zhu = map1;// 主电路
            }
        }
        Map<String, Map<String, Electric>> maps = new HashMap<>();
        for (mmm = 0; mmm < connection1.size(); mmm++) {// 判断那些事并联电路并存储
            String k11 = connection1.get(mmm).substring(0, 2);
            Pattern pattern = Pattern.compile("(.*)\\s\\[(.*)]");
            Matcher matcher = pattern.matcher(connection1.get(mmm));
            while (matcher.find()) {
                pop = String.valueOf(matcher.group(2));
            }
            String[] partsl = pop.split(" ");
            for (int yq = 0; yq < partsl.length; yq++) {
                for (Map.Entry<String, Map<String, Electric>> entry1 : mac.entrySet()) {
                    if (entry1.getKey().equals(partsl[yq])) {
                        maps.put(k11 + "" + yq, entry1.getValue());
                    }
                }
            }
        }
        //System.out.println(maps.size());
        int flag = 0;
        int flag1 = 0;
        int flag2 = 0;
        int lll = 0, mmm1 = 0;
        double rui = 220;
        for (i = 0; i < chw.size(); i++) {
            String sid = chw.get(i);
            for (Entry<String, Map<String, Electric>> entry : maps.entrySet()){
                if (sid.startsWith("K")) {
                    //System.out.println("1");
                    String diu = sid.substring(0, 2);
                    Electric electric = entry.getValue().get(diu);
                    if (electric != null) {
                        if (electric.ofopen.equals("turned on"))
                            electric.ofopen = "closed";
                        else
                            electric.ofopen = "turned on";
                        break;
                    }
                }
                else if (sid.startsWith("S")) {
                    //System.out.println("1");
                    String diu = sid.substring(0, 2);
                    Electric electric = entry.getValue().get(diu);
                    if (electric != null) {
                        if (electric.ofopen.equals("turned on"))
                            electric.ofopen = "closed";
                        else
                            electric.ofopen = "turned on";
                        break;
                    }
                }
                else if (sid.startsWith("F")) {
                    String diu = sid.substring(0, 2);
                    String vs = sid.substring(2);
                    Electric electric = entry.getValue().get(diu);
                    if (electric != null) {
                        electric.regulate(vs);
                        flag2++;
                        if (vs.equals("+")) {
                            flag++;
                            if (flag >= 3) {
                                flag = 3;
                            }
                        }
                        if (vs.equals("-")) {
                            flag--;
                            if (flag <= 0) {
                                flag = 0;
                            }
                        }
                        break;
                    }
                }
                else if (sid.startsWith("L")) {
                    String diu = sid.substring(0, 2);
                    String vs = sid.substring(3);
                    Electric electric = entry.getValue().get(diu);
                    if (electric != null) {
                        electric.regulate(vs);
                        // electric.reshuV(rui);
                        // rui = electric.shuV;
                        //System.out.println("");
                        break;
                    }
                }
            }
            for (Entry<String, Electric> entry : zhu.entrySet()) {
                if (sid.startsWith("K")) {
                    String diu = sid.substring(0, 2);
                    Electric electric = zhu.get(diu);
                    //electric.display();
                    if (electric != null) {
                        if (electric.ofopen.equals("turned on"))
                            electric.ofopen = "closed";
                        else
                            electric.ofopen = "turned on";
                        break;
                    }
                }
                else if (sid.startsWith("F")) {
                    String diu = sid.substring(0, 2);
                    String vs = sid.substring(2);
                    Electric electric = zhu.get(diu);
                    if (electric != null) {
                        //System.out.println("1");
                        electric.regulate(vs);
                        flag2++;
                        if (vs.equals("+")) {
                            flag++;
                            if (flag >= 3) {
                                flag = 3;
                            }
                        }
                        if (vs.equals("-")) {
                            flag--;
                            if (flag <= 0) {
                                flag = 0;
                            }
                        }
                        break;
                    }
                }
                else if (sid.startsWith("L")) {
                    String diu = sid.substring(0, 2);
                    String vs = sid.substring(3);
                    Electric electric = zhu.get(diu);
                    if (electric != null) {
                        //System.out.println("1");
                        electric.regulate(vs);
                        // electric.reshuV(rui);
                        break;
                    }
                }
            }
        }
        double siop = rui;
        double a[] = new double[maps.size()];// 算电阻
        double b[] = new double[zhu.size()];
        i = 0;
        int hjh=0;
        int iuy=0;
        Map<String, Electric> osp = new HashMap<>();
        for (Entry<String, Map<String, Electric>> entry : maps.entrySet()) {
            flag = 0;
            a[i] = 0;
            for (Entry<String, Electric> entry1 : entry.getValue().entrySet()) {
                if (entry1.getKey().startsWith("K")) {
                    if (entry1.getValue().ofopen.equals("turned on"))
                        flag = 1;
                    if(entry1.getValue().ofopen.equals("closed"))
                        hjh++;
                }
            }
            if(hjh==entry.getValue().size())
                a[i]=0;
            if (flag == 1)
                a[i] = -1;
            else {
                for (Entry<String, Electric> entry1 : entry.getValue().entrySet()) {
                    a[i] = a[i] + 1.0*entry1.getValue().resistance;
                }
            }
            i++;
        }
        double lpl1=0;
        double kapok=0;
        for (i = 0; i < a.length; i++) {

            if(a[i]==0){
                lpl1=0;
                break;
            }
            else{
                if (a[i]==-1)
                    continue;
                else {
                    kapok = 1.0 / a[i] + kapok;
                    lpl1=kapok;
                }

            }
        }
        b[0]=0;
        for (Entry<String, Electric> entry : zhu.entrySet()) {
            flag=0;
            if (entry.getKey().startsWith("K")) {
                if (entry.getValue().ofopen.equals("turned on")){
                    flag = 1;
                }
                if(flag==1){
                    b[0] = 0;break;
                }
            }
            b[0] = b[0] + entry.getValue().resistance;
        }

        double chuanR, bingR;
        chuanR = 1.0*b[0];

        if (lpl1 == 0)
            bingR = 0;
        else
            bingR = 1.0/lpl1;

        double chuanV = 0, bingV = 0;

        chuanV = rui * chuanR / (bingR + chuanR);
        bingV = rui * bingR / (bingR + chuanR);

        Map<String, Electric> maso = new HashMap<>();
        for (Entry<String, Electric> entry : zhu.entrySet()) {
            String parts8 = entry.getKey();
            s = parts8;
            if(b[0]==0&&flag==1){
                siop=0;
                rui=0;
                chuanV=0;
                bingV=0;
            }
            if (s.startsWith("K")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(rui);
                rui = elc1.shuV;
                chuanV = 1.0*rui * chuanR / (bingR + chuanR);
                bingV = 1.0*rui * bingR / (bingR + chuanR);
                maso.put(s, elc1);
            }

            else if (s.startsWith("B")) {
                //System.out.println(10*chuanV/chuanR);
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(10*chuanV/chuanR);
                maso.put(s, elc1);
            }

            else if (s.startsWith("R")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(5*chuanV/chuanR);
                maso.put(s, elc1);
            }

            else if (s.startsWith("D")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(20*chuanV/chuanR);
                maso.put(s, elc1);
                // System.out.println(elc1.shuV);
            }

            else if (s.startsWith("A")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(20*chuanV/chuanR);
                maso.put(s, elc1);
            }

            else if (s.startsWith("S")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(15*chuanV/chuanR);
                maso.put(s, elc1);
            }

            else if (s.startsWith("L")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(rui);
                //System.out.println(rui);
                rui = elc1.shuV;

                chuanV = rui * chuanR / (bingR + chuanR);
                bingV = rui * bingR / (bingR + chuanR);
                //System.out.println(chuanV+" "+bingV);
                maso.put(s, elc1);
            }

            else if (s.startsWith("H")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(rui);
                //System.out.println(rui);
                rui = elc1.shuV;

                chuanV = rui * chuanR / (bingR + chuanR);
                bingV = rui * bingR / (bingR + chuanR);
                //System.out.println(chuanV+" "+bingV);
                maso.put(s, elc1);
            }

            else if (s.startsWith("F")) {
                Electric elc1 = zhu.get(parts8);
                elc1.reshuV(rui);
                rui=elc1.shuV;
                //System.out.println(rui);
                chuanV = rui * chuanR / (bingR + chuanR);
                bingV = rui * bingR / (bingR + chuanR);
                maso.put(s, elc1);
            }
            else if (s.startsWith("M")) {
                i=0;
                for (Entry<String, Map<String, Electric>> entry6 : maps.entrySet()) {
                    if(lpl1==0){
                        siop=0;
                        for (Entry<String, Electric> entry1 : osp.entrySet()) {
                            parts8 = entry1.getKey();
                            s = parts8;
                            if (s.startsWith("K")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                if (elc1.ofopen.startsWith("t")) {
                                    siop = 0;
                                }
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("B")) {
                                //System.out.println(10*siop/a[i]);
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                // System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("R")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("D")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                //System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("A")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                // System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("L")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("F")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("S")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("H")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }
                        }
                    }
                    siop = bingV;
                    osp = entry6.getValue();
                    if(a[i]!=-1){
                        for (Entry<String, Electric> entry1 : osp.entrySet()) {
                            parts8 = entry1.getKey();
                            s = parts8;
                            if (s.startsWith("K")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                if (elc1.ofopen.startsWith("t")) {
                                    siop = 0;
                                }
                                //System.out.println(parts8);
                                maso.put(s, elc1);
                                //System.out.println(siop);
                            }

                            else if (s.startsWith("B")) {
                                //System.out.println(10*siop/a[i]);
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(10*siop/a[i]);
                                // System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("R")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(5*siop/a[i]);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("D")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(20*siop/a[i]);
                                //System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("A")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(20*siop/a[i]);
                                // System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("L")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("H")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }
                            
                            else if (s.startsWith("F")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("S")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(15*siop/a[i]);
                                maso.put(s, elc1);
                            }
                        }
                    }
                    else {
                        siop=0;
                        for (Entry<String, Electric> entry1 : osp.entrySet()) {
                            parts8 = entry1.getKey();
                            s = parts8;

                            if (s.startsWith("K")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.shuV=0;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("B")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                // System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("R")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("D")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                //System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("A")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                //System.out.println(elc1.shuV);
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("L")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("F")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("S")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }

                            else if (s.startsWith("H")) {
                                Electric elc1 = osp.get(parts8);
                                elc1.reshuV(siop);
                                siop = elc1.shuV;
                                maso.put(s, elc1);
                            }
                        }
                    }
                    i++;
                }
            }
        }

        ArrayList<Electric> arraylist6 = new ArrayList<>();
        for (Map.Entry<String, Electric> pose : maso.entrySet()) {
            arraylist6.add(pose.getValue());
        }

        Comparator<Electric> numberComparator = new Comparator<Electric>() {
            public int compare(Electric o1, Electric o2) {
                int a = o1.fanhui();
                int b = o2.fanhui();
                int c = Integer.parseInt(o1.id);
                int d = Integer.parseInt(o2.id);
                if (a > b)
                    return -1;
                else if (a == b) {
                    if (c > d)
                        return 1;
                    else
                        return -1;
                } else
                    return 1;
            }
        };
        Collections.sort(arraylist6, numberComparator);
        for (Electric suixcs : arraylist6) {
            suixcs.display();
        }
    }

    public static Map<String, Electric> complain(String dkey, String s, Map<String, Electric> map1) {
        String jkj = String.valueOf(s.charAt(1));

        if (s.startsWith("K")) {
            Electric tric = new Kaiguan(jkj);
            tric.resistance = 0;
            map1.put(dkey, tric);
        }

        else if (s.startsWith("F")) {
            Electric tric = new Fendang(jkj);
            tric.resistance = 0;
            map1.put(dkey, tric);
        }

        else if (s.startsWith("L")) {
            Electric tric = new Lianxu(jkj);
            tric.resistance = 0;
            map1.put(dkey, tric);
        }
        else if (s.startsWith("B")) {
            Electric tric = new Baichi(jkj);
            tric.resistance = 10;
            map1.put(dkey, tric);

        } else if (s.startsWith("R")) {
            Electric tric = new Riguang(jkj);
            tric.resistance = 5;
            map1.put(dkey, tric);

        } else if (s.startsWith("D")) {
            Electric tric = new Diaoshan(jkj);
            tric.resistance = 20;
            map1.put(dkey, tric);
        }
        else if (s.startsWith("A")) {
            Electric tric = new Luodi(jkj);
            tric.resistance = 20;
            map1.put(dkey, tric);
        }
        else if (s.startsWith("M")) {
            Electric tric = new Bi(jkj);
            tric.resistance = 0;
            map1.put(dkey,tric);
        }
        else if (s.startsWith("T")) {
            Electric tric = new Chuan(jkj);
            tric.resistance = 0;
            map1.put(dkey,tric);
        }
        else if (s.startsWith("S")) {
            Electric tric = new chuanglian(jkj);
            tric.resistance = 0;
            map1.put(dkey, tric);
        }
        else if (s.startsWith("H")) {
            Electric tric = new huchi(jkj);
            tric.resistance = 0;
            map1.put(dkey, tric);
        }
        return map1;
    }
}

添加ParseInput 类

ParseInput 类:作为新的电路电器加入,判断串并联电路并计算电压
Dianqi类:作为其他几个电器的主类使用抽象;
POPusity类::开关类

两个大类表示控制设备和被控制设备,这两个大类中将其相同变量表示出,如有特殊变量,在自己类中继续表示即可。使用ArrayList制造链表

踩坑心得:
互斥开关因为有3个引脚,所以互斥开关引脚的电势要Override父类的计算引脚电势的方法,这时候互斥开关再设计成开关和电阻的形式就比较麻烦了,可以直接设计成一个有电阻的开关,互斥开关引脚的电势就可以直接等于这两个开关引脚的电势。
改进建议:
二极管如果两端电压相等并且没有电流流过可以分以下两种情况输出:如果两端电压为0
——————————————————————————————————————————————
总结:Java学习主要还是要靠自己,通过在网上自己查找可以看到更多,更简便的代码,通过学习思路也能让自己的能力更上一层楼,可以每次写一个大题时先写大纲,在大纲上写下自己需要的所有的东西,然后组合起来,先搭建框架,再往里面逐渐添加,写代码的过程虽然枯燥,但是提升自己的快乐也更加珍贵

posted on 2024-06-30 08:25  ZBWzhao  阅读(6)  评论(0编辑  收藏  举报