oop-second-blog pear

1.前言

1.一些碎碎念

​ 这三次作业在我看来都是比较具有挑战性的,以题促学,为了写出这三道题目,我们需要去学习更多java中独特的语法和基本结构,同时这也加深了我对java语言中的基础知识的理解和掌握,做得到了从“会听”到“会用”的进步。令我受益匪浅,也掌握了一些对字符串的基本处理操作,这是我以前学c语言时未曾深入研究学习到的。

2.分析

1.第四次题目集

本次题目集变化不大,新增了两种题目的处理,分别是选择题和填空题,故在处理上与普通题目逻辑相似,只是需要新建几个类对新增题目进行处理,新增的题目异常情况的处理与一般题目相同,故不太需要进行新的异常处理

2.第五次题目集

新题目,对电路进行设计,此题目前还较为简单,没有串并联的混合情况,目前只需要考虑单个电器多个控制器在电路中的情况,其他并无什么可说的

3.第六次题目集

题目迭代,新增了串并联混连电路的设计,且有主电路和多个分电路,电器新增电阻,需要计算每个电器的分压,处理难度提升,需要新增串联电路类便于处理

2.设计与分析

1.答题程序最终版

复杂度分析:| Complexity metrics | ���� | 9 6�� 2024 11:08:17 CST | | |
|----------------------------------------------------------------------------------------------------------------------------|---------|-------------------------|-------|------|
| Method | CogC | ev(G) | iv(G) | v(G) |
| "AnswerSheet.AnswerSheet(String, String)" | 0 | 1 | 1 | 1 |
| "AnswerSheet.answerCheck1(String, String)" | 0 | 1 | 1 | 1 |
| "AnswerSheet.answerCheck2(String, String)" | 18 | 7 | 3 | 7 |
| "AnswerSheet.answerCheck3(String, String)" | 5 | 3 | 2 | 3 |
| AnswerSheet.getNum() | 0 | 1 | 1 | 1 |
| AnswerSheet.getSid() | 0 | 1 | 1 | 1 |
| AnswerSheet.mapPrint() | 1 | 1 | 2 | 2 |
| "AnswerSheet.resultOfAnswerSheet(List, List, List, List, List, List)" | 109 | 17 | 30 | 37 |
| "AnswerSheet.setAnswerMap(String, String)" | 0 | 1 | 1 | 1 |
| "GapFill.GapFill(String, String, String)" | 0 | 1 | 1 | 1 |
| MCQs.MCQs() | 0 | 1 | 1 | 1 |
| "MCQs.MCQs(String, String)" | 0 | 1 | 1 | 1 |
| Main.main(String[]) | 76 | 6 | 28 | 29 |
| Question.Question() | 0 | 1 | 1 | 1 |
| "Question.Question(String, String, String)" | 0 | 1 | 1 | 1 |
| Question.check(String) | 0 | 1 | 1 | 1 |
| Question.getAnswer() | 0 | 1 | 1 | 1 |
| Question.getContent() | 0 | 1 | 1 | 1 |
| Question.getNum() | 0 | 1 | 1 | 1 |
| "SCQs.SCQs(String, String, String)" | 0 | 1 | 1 | 1 |
| "Student.Student(String, String)" | 0 | 1 | 1 | 1 |
| Student.getName() | 0 | 1 | 1 | 1 |
| Student.getSid() | 0 | 1 | 1 | 1 |
| TestPaper.TestPaper(String) | 0 | 1 | 1 | 1 |
| "TestPaper.addQuestion(String, String)" | 0 | 1 | 1 | 1 |
| TestPaper.calculateScore() | 1 | 1 | 2 | 2 |
| TestPaper.checkFullScore() | 2 | 1 | 3 | 3 |
| TestPaper.checkTestPaper(List) | 9 | 4 | 5 | 6 |
| TestPaper.getScoreQuestions() | 0 | 1 | 1 | 1 |
| TestPaper.getTestNum() | 0 | 1 | 1 | 1 |
| TestPaper.setTestNum(String) | 0 | 1 | 1 | 1 |
| scoreQuestion.getNum() | 0 | 1 | 1 | 1 |
| scoreQuestion.getScore() | 0 | 1 | 1 | 1 |
| "scoreQuestion.scoreQuestion(String, String)" | 0 | 1 | 1 | 1 |
| scoreQuestion.setNum(String) | 0 | 1 | 1 | 1 |
| scoreQuestion.setScore(String) | 0 | 1 | 1 | 1 |
| | | | | |
| Class | OCavg | OCmax | WMC | |
| AnswerSheet | 5.67 | 34 | 51 | |
| GapFill | 1.00 | 1 | 1 | |
| MCQs | 1.00 | 1 | 2 | |
| Main | 29.00 | 29 | 29 | |
| Question | 1.00 | 1 | 6 | |
| SCQs | 1.00 | 1 | 1 | |
| Student | 1.00 | 1 | 3 | |
| TestPaper | 2.00 | 6 | 16 | |
| scoreQuestion | 1.00 | 1 | 5 | |
| | | | | |
| Package | v(G)avg | v(G)tot | | |
| | 3.25 | 117 | | |
| | | | | |
| Module | v(G)avg | v(G)tot | | |
| untitled12 | 3.25 | 117 | | |
| | | | | |
| Project | v(G)avg | v(G)tot | | |
| project | 3.25 | 117 | | |

public void resultOfAnswerSheet(List<Question>allQuestion,List<TestPaper>allTestPaper,List<Student>allStudent,List<String>deleteQuestions,List<SCQs>allSCQs,List<GapFill>allGap){
        List<scoreQuestion> question=new ArrayList<>();
        int testPaperExist=0;
        //根据号码找到相应的测试卷
        for (TestPaper a:allTestPaper){
            if(a.getTestNum().equals(this.num)){
                question=a.getScoreQuestions();
                testPaperExist=1;
                break;
            }
        }
        if (testPaperExist==0)
        {
            System.out.println("The test paper number does not exist");
        }

        //打印testPaper的题目分数
//        for(scoreQuestion test:question){
//            System.out.println(test.getNum()+"-"+test.getScore());
//        }
        else if(testPaperExist==1)
        {
            int score=0;

            int []eachScore=new int[question.size()];
            int count=1;

            //因为答卷使用的是TreeMap故取出来的键对值是按顺序排列的
            for(scoreQuestion test:question){
                String num=test.getNum();
                String sigalScore= test.getScore();
                String QuestionContent=null;
                String Questionanswer=null;
                int flag=0;
                int mark=1;//题目没有被删除
                int tag=1;
                int type=0;
                //有没有被删除,只需要对delete进行遍历即可

                for (String element:deleteQuestions){
                    if(num.equals(element)){
                        //System.out.printf("the question %s invalid~0\n",element);
                        flag=1;
                        mark=0;//题目被删除
                        break;
                    }
                }

                //需要寻找三次
                if(flag!=1)//此时若题目未被删除flag=0,找到了题目flag=2
                {
                    //普通题
                    for(Question test1:allQuestion){
                        if (test1.getNum().equals(num)){
                            QuestionContent= test1.getContent();
                            Questionanswer=test1.getAnswer();
                            type=1;
                            flag=2;
                            break;
                        }
                    }
                    //选择题
                    for(SCQs test2:allSCQs)
                    {
                        if(test2.num.equals(num))
                        {
                            QuestionContent=test2.content;
                            Questionanswer=test2.answer;
                            type=2;
                            flag=2;
                            break;
                        }
                    }
                    //填空题
                    for(GapFill test3:allGap){
                        if(test3.num.equals(num)){
                            QuestionContent=test3.content;
                            Questionanswer=test3.answer;
                            type=3;
                            flag=2;
                            break;
                        }
                    }
                }

                //若此时flag=0既不是删除也找不到题目即为
                if(flag==0){
                    //System.out.println("non-existent question~0");
                    tag=0;
                }

                //若在题目集中找到题目
                boolean answerExist=false;
                for(Map.Entry<String,String> entry:this.answerMap.entrySet()){
                    if(count==Integer.parseInt(entry.getKey())){
                        answerExist=true;
                        if(mark!=0&&tag!=0){
                            if(type==1){
                                System.out.printf("%s~%s~%s\n",QuestionContent,entry.getValue(),answerCheck1(Questionanswer,entry.getValue()));
                                if (Questionanswer.equals(entry.getValue())){
                                    score+=Integer.parseInt(sigalScore);
                                    eachScore[count-1]=Integer.parseInt(sigalScore);
                                }
                            } else if (type==2) {
                                //System.out.println("用户答案"+entry.getValue()+" "+"标准答案"+QuestionAnswer);
                                String move=entry.getValue().trim();
                                System.out.printf("%s~%s~%s\n",QuestionContent,move,answerCheck2(Questionanswer,entry.getValue()));
                                if(answerCheck2(Questionanswer,entry.getValue()).equals("true")){
                                    score+=Integer.parseInt(sigalScore);
                                    eachScore[count-1]=Integer.parseInt(sigalScore);
                                }else if(answerCheck2(Questionanswer,entry.getValue()).equals("partially correct")){
                                    score+=Integer.parseInt(sigalScore)/2;
                                    eachScore[count-1]=Integer.parseInt(sigalScore)/2;
                                }
                            } else if (type==3) {
                                System.out.printf("%s~%s~%s\n",QuestionContent,entry.getValue(),answerCheck3(Questionanswer,entry.getValue()));
                                if(answerCheck3(Questionanswer,entry.getValue()).equals("true")){
                                    score+=Integer.parseInt(sigalScore);
                                    eachScore[count-1]=Integer.parseInt(sigalScore);
                                }else if(answerCheck3(Questionanswer,entry.getValue()).equals("partially correct")){
                                    score+=Integer.parseInt(sigalScore)/2;
                                    eachScore[count-1]=Integer.parseInt(sigalScore)/2;
                                }
                            }


                        }
                        break;
                    }
                }
                if(!answerExist){
                    System.out.println("answer is null");
                } else if (answerExist&&mark==0) {
                    System.out.printf("the question %s invalid~0\n",num);
                } else if (answerExist&&tag==0) {
                    System.out.println("non-existent question~0");
                }

                count++;

            }
            //找寻考生姓名
            String yourName=null;
            int yourNameExist=0;
            for(Student stu2:allStudent)
            {
                if(this.sid.equals(stu2.sid)){
                    yourName= stu2.getName();
                    yourNameExist=1;
                }
            }
            if(yourNameExist==0){
                yourName="not found";
                System.out.printf("%s %s\n",this.sid,yourName);
            }

            //格式约束
            else{
                System.out.printf("%s %s:",this.sid,yourName);
                for(int x:eachScore){
                    System.out.printf(" %d",x);
                }
                System.out.printf("~%d\n",score);
            }


        }

    }

解释:本次题目新增了判断逻辑,我是先进行了一个类的判断,判断题目类型进入相应的判断逻辑,同样寻找题目存在时,需要在三个list寻找三次,此处有点繁琐,并且这个方法有点过于冗长,打印卷面信息部分其实可以拆分出一个方法来进行实现

优点时:

  1. 结构清晰:代码按照功能分块,包括题目的查找、题目有效性的判断、不同类型题目的打分逻辑以及对学生信息的查找,每个部分相对独立,易于理解和维护。
  2. 注释完整:代码中包含了大量的注释,有助于理解每个部分的功能和作用。
  3. 异常处理:代码中对一些异常情况进行了处理,如测试卷不存在、题目不存在或被删除等,保证了程序的健壮性。

有以下缺点:

  1. 代码重复:在判断题目类型并进行打分的部分,使用了多个if-else语句,导致代码重复,不易维护。
  2. 可读性较差:代码中存在大量的嵌套循环和条件判断,使得代码的可读性较差,不易理解。
  3. 效率较低:在查找题目和答案时,使用了多个循环遍历,效率较低,可以考虑使用哈希表等数据结构进行优化。
  4. 硬编码:代码中存在一些硬编码,如题目的类型和分数,不利于代码的扩展和维护。

2.家居强电路一

Complexity metrics ���� 9 6�� 2024 11:49:21 CST
Method CogC ev(G) iv(G) v(G)
Main.main(String[]) 64 3 41 43
continuousController.continuousController() 0 1 1 1
"continuousController.continuousController(double, double, String, double)" 0 1 1 1
continuousController.getOutput() 0 1 1 1
controlDevice.controlDevice() 0 1 1 1
"controlDevice.controlDevice(double, double, String)" 0 1 1 1
controlledDevice.controlledDevice() 0 1 1 1
"controlledDevice.controlledDevice(double, double, int)" 0 1 1 1
electricalEquipment.electricalEquipment() 0 1 1 1
"electricalEquipment.electricalEquipment(double, double)" 0 1 1 1
fan.calculateSpeed() 3 3 1 3
fan.fan() 0 1 1 1
"fan.fan(double, double, int, double)" 0 1 1 1
fluorescentLamp.calculateBrightness() 2 2 1 2
fluorescentLamp.fluorescentLamp() 0 1 1 1
"fluorescentLamp.fluorescentLamp(double, double, int, double)" 0 1 1 1
incandescentLamp.calculateBrightness() 3 3 1 3
incandescentLamp.incandescentLamp() 0 1 1 1
"incandescentLamp.incandescentLamp(double, double, int, double)" 0 1 1 1
stepController.getOutput() 4 1 1 5
stepController.stepController() 0 1 1 1
"stepController.stepController(double, double, String, int)" 0 1 1 1
switchOn.getOutput() 2 1 1 2
switchOn.switchOn() 0 1 1 1
"switchOn.switchOn(double, double, String, int)" 0 1 1 1
Class OCavg OCmax WMC
Main 37.00 37 37
continuousController 1.00 1 3
controlDevice 1.00 1 2
controlledDevice 1.00 1 2
electricalEquipment 1.00 1 2
fan 1.67 3 5
fluorescentLamp 1.33 2 4
incandescentLamp 1.67 3 5
stepController 2.33 5 7
switchOn 1.33 2 4
Package v(G)avg v(G)tot
3.08 77
Module v(G)avg v(G)tot
untitled12 3.08 77
Project v(G)avg v(G)tot
project 3.08 77

$$

$$

public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int count=1;
        int mode=0;

        switchOn k1=new switchOn(220,0,"unKnow",-1);
        continuousController L1=new continuousController();
        L1.pin1=220;
        stepController F1=new stepController();

        F1.pin1=220;
        ArrayList<electricalEquipment> arrayList=new ArrayList<>();

        while(true){
            //程序的结束条件
            String line=input.nextLine();


            if(line.equals("end")){
                break;
            }
            //控制器的选择
            if(count==1){
                line=line.substring(1, line.length()-1);
                String[] parts = line.split("\\s|-");
                if(parts[1].charAt(0)=='K'){
//                    System.out.println("开关控制");
                    k1.id=parts[1];
                    mode=1;
                } else if (parts[1].charAt(0)=='F') {
//                    System.out.println("分档调速器控制");
                    F1.id=parts[1];
                    mode=2;

                } else if (parts[1].charAt(0)=='L') {
//                    System.out.println("连续调速器控制");
                    L1.id=parts[1];
                    mode=3;
                }
            } else if (count==2) {
                line=line.substring(1, line.length()-1);
                String[] parts = line.split("\\s|-");
                if(parts[2].charAt(0)=='B'){
                    incandescentLamp b1=new incandescentLamp();
                    b1.id=parts[2];
//                    System.out.println("控制白炽灯");
                    arrayList.add(b1);
                } else if (parts[2].charAt(0)=='R') {
                    fluorescentLamp r1=new fluorescentLamp();
                    r1.id=parts[2];
//                    System.out.println("控制日光灯");
                    arrayList.add(r1);
                } else if (parts[2].charAt(0)=='D') {
                    fan f1=new fan();
                    f1.id=parts[2];
                    arrayList.add(f1);
//                    System.out.println("控制吊扇");
                }
            }

            //对控制器的操作部分
            if(mode==1&&count>3&&!line.equals("end")){
//                System.out.println("进入开关操作部分"+line);
                line=line.substring(1);
                if(line.equals(k1.id)){
                    k1.state*=(-1);
                    k1.pin2= k1.getOutput();
                    electricalEquipment test=arrayList.get(0);
                    if(test instanceof incandescentLamp){
                        incandescentLamp a=(incandescentLamp) test;
                        a.pin1= k1.pin2;
                        a.brightness=a.calculateBrightness();
//                        System.out.println(a.brightness);
                    } else if (test instanceof fluorescentLamp) {
                        fluorescentLamp a=(fluorescentLamp) test;
                        a.pin1= k1.pin2;;
                        a.brightness= a.calculateBrightness();
//                        System.out.println(a.brightness);
                    } else if (test instanceof fan) {
                        fan a=(fan) test;
                        a.pin1= k1.pin2;
                        a.speed=a.calculateSpeed();
//                        System.out.println(a.speed);
                    }

                }
            }

            if(mode==2&&count>3&&!line.equals("end")){
//                System.out.println("进入分档调节器操作部分"+line);
                line=line.substring(3);
                if(line.equals("+")){
                    F1.step+=1;
                    F1.pin2= F1.getOutput();
                    electricalEquipment test=arrayList.get(0);
                    if(test instanceof incandescentLamp)
                    {
                        incandescentLamp a=(incandescentLamp) test;
                        a.pin1= F1.pin2;
                        a.brightness=a.calculateBrightness();
//                        System.out.println(a.brightness);
                    } else if (test instanceof fluorescentLamp) {
                        fluorescentLamp a=(fluorescentLamp) test;
                        a.pin1= F1.pin2;
                        a.brightness=a.calculateBrightness();
//                        System.out.println(a.brightness);
                    } else if (test instanceof fan) {
                        fan a=(fan) test;
                        a.pin1= F1.pin2;
                        a.speed=a.calculateSpeed();
//                        System.out.println(a.speed);
                    }
                }else if(line.equals("-")){
                    F1.step-=1;
                    F1.pin2= F1.getOutput();
                    electricalEquipment test=arrayList.get(0);
                    if(test instanceof incandescentLamp)
                    {
                        incandescentLamp a=(incandescentLamp) test;
                        a.pin1= F1.pin2;
                        a.brightness=a.calculateBrightness();
//                        System.out.println(a.brightness);
                    } else if (test instanceof fluorescentLamp) {
                        fluorescentLamp a=(fluorescentLamp) test;
                        a.pin1= F1.pin2;
                        a.brightness=a.calculateBrightness();
//                        System.out.println(a.brightness);
                    } else if (test instanceof fan) {
                        fan a=(fan) test;
                        a.pin1= F1.pin2;
                        a.speed=a.calculateSpeed();
//                        System.out.println(a.speed);
                    }

                }

            }


            if(mode==3&&count>3&&!line.equals("end")){
//                 System.out.println("进入连续调节器操作部分"+line);
                line=line.substring(4);
                L1.step=Double.parseDouble(line);
                L1.pin2= L1.getOutput();
                electricalEquipment test=arrayList.get(0);
                if(test instanceof incandescentLamp){
                    incandescentLamp a=(incandescentLamp) test;
                    a.pin1= L1.pin2;
                    a.brightness= a.calculateBrightness();
//                    System.out.println(a.brightness);
                } else if (test instanceof fluorescentLamp) {
                    fluorescentLamp a=(fluorescentLamp) test;
                    a.pin1= L1.pin2;
                    a.brightness= a.calculateBrightness();
//                    System.out.println(a);
                } else if (test instanceof fan) {
                    fan a=(fan) test;
                    a.pin1= L1.pin2;
                    a.speed= a.calculateSpeed();
//                    System.out.println(a.speed);
                }

            }


            count++;//循环次数加一
        }
        if(mode==1) {
            if (k1.state == 1) {
                System.out.printf("@%s:closed\n", k1.id);
            } else if (k1.state==-1) {
                System.out.printf("@%s:turned on\n", k1.id);
            }
        } else if (mode==2) {
            System.out.printf("@%s:%d\n",F1.id,F1.step);
        }else if(mode==3){
            System.out.printf("@%S:%.2f\n",L1.id,L1.step);
        }

        electricalEquipment test=arrayList.get(0);
        if(test instanceof incandescentLamp){
            incandescentLamp a=(incandescentLamp) test;
            System.out.printf("@%s:%d\n",a.id,(int)a.brightness);
        } else if (test instanceof fluorescentLamp) {
            fluorescentLamp a=(fluorescentLamp) test;
            System.out.printf("@%s:%d\n",a.id,(int)a.brightness);
        } else if (test instanceof fan) {
            fan a=(fan) test;
            System.out.printf("@%s:%d\n",a.id,(int)a.speed);
        }


    }

解释:

优点:

  1. 模块化:代码通过创建不同的类来表示不同的设备(如开关、灯泡、风扇)和控制设备(如开关控制器、步进控制器、连续控制器),这有助于代码的模块化和重用。
  2. 可扩展性:通过使用数组列表来存储设备,可以很容易地添加新的设备类型,而无需修改现有代码。
  3. 分层设计:代码将输入处理、设备控制输出和设备状态更新分成了不同的部分,这有助于维护和调试。

缺点:

  1. 缺乏错误处理:代码没有对可能出现的错误(如无效输入、设备控制失败等)进行适当处理,这可能导致程序在运行时崩溃或产生不可预知的行为。
  2. 代码重复:在处理不同类型的设备时,代码中有大量的重复逻辑,这违反了DRY(Don’t Repeat Yourself)原则,增加了维护难度。
  3. 硬编码:代码中的一些值(如设备的ID、控制器的类型等)是硬编码的,这限制了代码的灵活性和可配置性。
  4. 缺乏单元测试:代码没有提供任何单元测试,这使得难以确保每个部分都按预期工作。

3.家居强电路二

Complexity metrics ���� 9 6�� 2024 12:03:29 CST
Method CogC ev(G) iv(G) v(G)
AFan.AFan() 0 1 1 1
"AFan.AFan(double, double, String, double, double)" 0 1 1 1
DL.DL() 0 1 1 1
DL.allR() 3 1 1 3
DL2.DL2() 0 1 1 1
DL2.allR() 1 1 1 2
Main.main(String[]) 67 3 27 28
continuousController.continuousController() 0 1 1 1
"continuousController.continuousController(double, double, String, double)" 0 1 1 1
continuousController.getOutput() 0 1 1 1
controlDevice.controlDevice() 0 1 1 1
"controlDevice.controlDevice(double, double, String)" 0 1 1 1
controlledDevice.controlledDevice() 0 1 1 1
"controlledDevice.controlledDevice(double, double, String, double, double)" 0 1 1 1
electricalEquipment.electricalEquipment() 0 1 1 1
"electricalEquipment.electricalEquipment(double, double)" 0 1 1 1
fan.calculateSpeed() 3 3 1 3
fan.fan() 0 1 1 1
"fan.fan(double, double, String, double, double)" 0 1 1 1
fluorescentLamp.calculateBrightness() 2 2 1 2
fluorescentLamp.fluorescentLamp() 0 1 1 1
"fluorescentLamp.fluorescentLamp(double, double, String, double, double)" 0 1 1 1
incandescentLamp.calculateBrightness() 3 3 1 3
incandescentLamp.incandescentLamp() 0 1 1 1
"incandescentLamp.incandescentLamp(double, double, String, double, double)" 0 1 1 1
stepController.getOutput() 4 1 1 5
stepController.stepController() 0 1 1 1
"stepController.stepController(double, double, String, int)" 0 1 1 1
switchOn.getOutput() 2 1 1 2
switchOn.switchOn() 0 1 1 1
"switchOn.switchOn(double, double, String, int)" 0 1 1 1
Class OCavg OCmax WMC
AFan 1.00 1 2
DL 2.00 3 4
DL2 1.50 2 3
Main 28.00 28 28
continuousController 1.00 1 3
controlDevice 1.00 1 2
controlledDevice 1.00 1 2
electricalEquipment 1.00 1 2
fan 1.67 3 5
fluorescentLamp 1.33 2 4
incandescentLamp 1.67 3 5
stepController 2.33 5 7
switchOn 1.33 2 4
Package v(G)avg v(G)tot
2.29 71
Module v(G)avg v(G)tot
oop1 2.29 71
Project v(G)avg v(G)tot
project 2.29 71
 while (true){
            //程序的结束条件
            String line=input.nextLine();
            if(line.equals("end")){
                break;
            }

            //无乱序,处理分支电路
            if(line.charAt(1)=='T'){
                DL dl=new DL();
                dl.id=line.substring(1,3);
                electricalEquipments.add(dl);//把这个电路存起来

                line=line.substring(4);

                //匹配设备
                Pattern pattern1 = Pattern.compile("([A-Z][0-9])");
                Matcher matcher1 = pattern1.matcher(line);

                Set<String> id=new HashSet<>();
                while (matcher1.find()){
                    id.add(matcher1.group(1));
                }

//                for(String test:id){
//                    System.out.println(test);
//                }
                
                //前三位为控制器开关,分档调节,连续调节,将每一个元器件存入到该条电路中
                for(String test:id){
                    if(test.charAt(0)=='K'){
                        switchOn k=new switchOn(0,0,test,-1);
                        dl.electricalEquipments.add(k);
                        equipments.add(k);
                        switchOns.add(k);
                    } else if (test.charAt(0)=='F') {
                        stepController s=new stepController(0,0,test,0);
                        dl.electricalEquipments.add(s);
                        equipments.add(s);
                    } else if (test.charAt(0)=='L') {
                       continuousController c=new continuousController(0,0,test,0);
                        dl.electricalEquipments.add(c);
                        equipments.add(c);
                    } else if (test.charAt(0)=='B') {
                        incandescentLamp i=new incandescentLamp(0,0,test,0,10);
                        dl.electricalEquipments.add(i);
                        equipments.add(i);
                    } else if (test.charAt(0)=='R') {
                        incandescentLamp i=new incandescentLamp(0,0,test,0,5);
                        dl.electricalEquipments.add(i);
                        equipments.add(i);
                    } else if (test.charAt(0)=='D') {
                        fan f=new fan(0,0,test,0,20);
                        dl.electricalEquipments.add(f);
                        equipments.add(f);
                    }else if(test.charAt(0)=='A'){
                        AFan a=new AFan(0,0,test,0,20);
                        dl.electricalEquipments.add(a);
                        equipments.add(a);
                    }

                }
                dl.allR();

            } else if (line.charAt(1)=='M') {

                DL2 dl2=new DL2();
                dl2.id=line.substring(1,3);
                electricalEquipments.add(dl2);
                line=line.substring(4);
                Pattern pattern1 = Pattern.compile("([A-Z][0-9])");
                Matcher matcher1 = pattern1.matcher(line);
                Set<String> id=new HashSet<>();
                while (matcher1.find()){
                    id.add(matcher1.group(1));
                }
                int count=0;
                for(String test:id){
                    for(electricalEquipment test1:electricalEquipments){
                        if (test1 instanceof DL){
                            if(((DL) test1).id.equals(test)){
                                dl2.DLs.add((DL) test1);
                                System.out.println("该条串联线路电阻"+((DL) test1).r);
                                count++;
                            }
                        }
                    }
                }
                System.out.println("找到串联电路的条数"+count);
                if(count== id.size()){
                    System.out.println("并联成功");
                    dl2.allR();
                    System.out.println("并联电路的电阻"+dl2.r);
                }
            }else if(line.charAt(1)=='K'){
                line=line.substring(1);
                for(switchOn test:switchOns){
                    if(line.equals(((switchOn) test).id)){
                        test.state=-1* test.state;
                        System.out.println("改变了开关状态");
                    }
                }
            }
        }

解释:

优点:

  1. 模块化:代码通过创建不同的类来表示电路的不同部分(如开关、灯泡、风扇等),这有助于代码的模块化和重用。
  2. 可扩展性:通过使用列表来存储电路和设备,可以很容易地添加新的设备类型和电路布局,而无需修改现有代码。
  3. 分层设计:代码将输入处理、电路状态更新和输出显示分成了不同的部分,这有助于维护和调试。

缺点:

  1. 缺乏错误处理:代码没有对可能出现的错误(如无效输入、电路配置错误等)进行适当处理,这可能导致程序在运行时崩溃或产生不可预知的行为。
  2. 在处理不同类型的设备时,代码中有大量的重复逻辑,这违反了DRY(Don’t Repeat Yourself)原则,增加了维护难度。

4.改进建议

可以改进的地方很多,我的代码仍然不够完善

  1. 添加错误处理:对可能出现的错误进行捕获和处理,以提高程序的健壮性。
  2. 重构代码:将重复的逻辑提取到单独的方法中,以减少代码重复和提高可读性。
  3. 添加测试点:方便对报错的异常处理进行修复
  4. 模块化:一个主函数里处理了太多内容,应该拆分书写为每个类的方法
  5. 文本处理:创建一个解析器类来处理输入字符串,而不是在主循环中直接解析。使用正则表达式来验证输入格式,确保输入符合预期。

5.总结

这三次作业要处理的信息以及类与类的关联性都大大增加,无疑增加了不少难度,电路串并联的引入更要考虑每一个电器假如时对整体电路的影响,以及串联电路作为一个电器的思想接入电路,都要求我们更加熟悉和设计类与类之间的关系,用到java的多态和继承,大大提升了我们解决复杂问题的能力。

posted @   Fernsakura  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示