西北工业大学#面向对象编程实验#实验四->第一题

题目一:文件读写

本次实验需要将Unit3-2中的复试系统改为从文件中读取试题信息并将部分学生试卷信息输出到文件中。
目的:增强使用文件读写的能力
背景:在此作业中,您将创建另一个版本的复试系统。在以前的版本中,试题库的试题数据在应用程序中进行了硬编码。 在此版本中,将从文件中加载数据。 而且,用户将能够以以下三种格式之一将学生试卷信息写入文件:纯文本,HTML或XML。部分工作已为您完成,并在学生资料中提供。您将实现加载试题库的试题数据并保存学生试卷信息的代码。

数据文件

试题类型主要分为三种:英语题、数学题和专业课题。testCatalog.dat文件存储着试题库数据。testCatalog.dat中的每一行只包含一个试题。字段由下划线分割,假定字段本身不包含任何下划线。

英语题数据的格式

EnglishTest_code_title_difficultyDegree_scoreCriteria_type

其中:EnglishTest是标识该行试题类型的前缀;code(string)表示英语题的代码;title(string)表示英语题的题干;difficultyDegree(int)表示英试题的难度系数,其后两项均为string类型,分别表示英试题的得分标准和类型。

数学题数据的格式

MathTest_code_title_difficultyDegree_scoreCriteria_photoURL_calculationProcess

其中:MathTest是标识该行试题类型的前缀;code(string)表示数学题代码;title(string)表示数学题题干;difficultyDegree(int)表示数学题的难度系数,其后三项均为string类型,分别表示数学题的得分标准、图片地址和计算过程。

专业题数据的格式

ProfessionalTest_code_title_difficultyDegree_scoreCriteria_programInstruction_programming_photoURL

其中:ProfessionalTest是标识该行试题类型的前缀;code(string)表示专业
题代码;title(string)表示专业题题干;difficultyDegree(int)表示专业题的难度系数,其后四项均为string类型,分别表示专业题的得分标准、程序说明、程序体和图片地址。

类图

在这里插入图片描述

部分类与接口说明

接口FushiTestDatabaseLoader

接口FushiTestDatabaseLoader声明用于生成试题库的方法。
方法:
Catalog loadTestDatabase(String fileName) throws FileNotFoundException,
IOException,
DataFormatException
此方法将指定文件中的信息加载到试题库中并返回试题库。

类DataFormatException

当正在分析的文件中有一行出现以下错误时,将抛出异常。
1)该行没有预期数量的字段;
2)应包含数字的字段却没有包含数字。

类FileFushiTestDatabaseLoader

类FileFushiTestDatabaseLoader实现接口FushiTestDatabaseLoader.它用于从文件中获取试题库。
方法:
1.private EnglishTest readEnglishTest(String line) throws DataFormatException
如果该行没有错误,则此方法返回一个封装英语试题数据的EnglishTest对象。

2.private MathTest readMathTest (String line) throws DataFormatException
如果该行没有错误,则此方法返回一个封装数学试题数据的MathTest对象。

3.private ProfessionalTest readProfessionalTest(String line) throws DataFormatException
如果该行没有错误,则此方法返回一个封装专业题数据的ProfessionalTest对象。

以上方法若有错误均引发一个DataFormatException
4.public TestDatabase loadTestDatabase (String filename) throws FileNotFoundException,
IOException,
DataFormatException
此方法将指定文件中的信息加载到试题库中并试题库。它首先打开文件然后读取并处理文件中的每一行。使用方法String.startsWith确定每行数据的行类型。

类FushiSystem

writeFile方法使用指定的名称创建一个新文件,将指定的字符串写入该文件,然后关闭该文件。
其他方法可直接在实验Unit3-2上进行更改或直接沿用Unit3-2中的方法。

任务:

1.根据题目需求实现复试系统,请添加必要的注释。
2.将试题数据文件路径配置到程序运行参数中。
3.与Unit3-2相同,系统会提示选项列表,供用户选择需要输出的格式。当用户选择特定的格式后,系统会创建相应的包含学生试卷信息的文件。
4.实验需要写一个简单的说明文档,其中包括必要的运行说明及运行结果截图。其中包括系统对不同错误的处理反馈。
5.实验提交内容:项目文件及说明文档。
6.实验的输出结果必须和下面展示的结果一致。(此处学生试卷信息与Unit3-2不同,请注意!)

纯文本:
在这里插入图片描述

HTML:
在这里插入图片描述

在这里插入图片描述
XML:
在这里插入图片描述

代码展示

这次的题目输出较实验三的有所不同,其中Plain和XML输出格式变化不大,所以重点展示HTML格式
由于输出的不同种类的Test,有不一样多的元素,所以使用foreach循环遍历学生的exampaper后,要将testitem中的test向下转型再输出,代码如下:

HTML

public class HTMLStudentsFormatter implements StudentsFormatter{
    /**
     * singletonInstance 作为调用formatStudents方法的通道
     */
    private static HTMLStudentsFormatter singletonInstance=null;

    /**
     * singletonInstance的无参构造方法
     */
    public HTMLStudentsFormatter(){
    }

    /**
     * @param studentCatalog 传入一个需要输出信息的学生列表
     *
     * @return s 返回输出字符串
     */
    @Override

    public String formatStudents(StudentCatalog studentCatalog) {
        String s="\n<html>\n";
        s=s+"  <body>\n";
        s=s+"    <center><h2>Student Catalog</h2></center>\n";
        for(Student student : studentCatalog.students){
            s=s+"    <hr>\n";
            s=s+"    <h4>"+student.getId()+" "+student.getName()+"</h4>\n";
            s=s+"      <blockquote>\n";
            String tmp1="";
            for(TestItem testitem : student.getExamPaper().testitem){
                String tmp=new String (tmp1+testitem.getTest ().getCode ().charAt (0));
                //通过code属性的第一个字符来判定进行那种Test的输出
                if(tmp.equals ("M")){
                    s=s+"        "+testitem.getTest ().getCode ()+"|"
                            +testitem.getTest ().getTitle ()+"|"
                            +testitem.getTest ().difficultyDegree+"|"
                            +testitem.getTest ().scoreCriteria+"|"
                            +((MathTest)testitem.getTest ()).getPhotoURL ()+"|"
                            +((MathTest)testitem.getTest ()).getcalculationProcess ()
                            +"<br/>";

                }else if(tmp.equals ("P")){
                    s=s+"        "+testitem.getTest ().getCode ()+"|"
                            +testitem.getTest ().getTitle ()+"|"
                            +testitem.getTest ().difficultyDegree+"|"
                            +testitem.getTest ().scoreCriteria+"|"
                            +((ProfessionalTest)testitem.getTest ()).getprograminstruction ()+"|"
                            +((ProfessionalTest)testitem.getTest ()).programming+"|"
                            +((ProfessionalTest)testitem.getTest ()).getpotoURL ()
                            +"<br/>";

                }else if(tmp.equals ("E")){
                    s=s+"        "+testitem.getTest ().getCode ()+"|"
                            +testitem.getTest ().getTitle ()+"|"
                            +testitem.getTest ().difficultyDegree+"|"
                            +testitem.getTest ().scoreCriteria+"|"
                            +((EnglishTest)testitem.getTest ()).gettype ()
                            +"<br/>";
                }
                tmp1="";
                s=s+"\n";
            }
            s=s+"      </blockquote>\n";
        }
        s=s+"  </body>\n";
        s=s+"</html>";
        return s;
    }

    /**
     * @return singletonInstance 当singletonInstance为空时创建对象
     */
    public static HTMLStudentsFormatter getSingletonInstance() {
        if(singletonInstance==null){
            singletonInstance=new HTMLStudentsFormatter();
        }
        return singletonInstance;
    }
}

DataFormatException

//自定义异常基本格式
public class DataFormatException extends Exception{
    public DataFormatException(){
        super();
    }
    public DataFormatException(String message){
        super(message);
    }
}

FileFushiTestDatabaseLoader

import java.io.*;
import java.lang.*;
import java.util.ArrayList;
public class FileFushiTestDatabaseLoader implements FushiTestDatabaseLoader{
    
    private final ArrayList<String> data=new ArrayList<> ();

    /**
     * 
     * @param line 读取testcatalog.dat中的一行字符串
     * @return  返回一个EnglishTest
     */
    private EnglishTest readEnglishTest(String line){
        String[] DATA=line.split ("_");
        if(DATA.length!=6){
            try {
                throw new DataFormatException("This line contains incomplete data");
            } catch (DataFormatException e) {
                e.printStackTrace ();
            }
        }
        int m=Integer.parseInt (DATA[3]);
        return new EnglishTest (DATA[1],DATA[2],m,DATA[4],DATA[5]);
    }

    /**
     * 
     * @param line 读取testcatalog.dat中的一行字符串
     * @return 返回一个EnglishTest
     */
    private MathTest readMathTest(String line){
        String[] DATA=line.split ("_");
        if(DATA.length!=7){
            try {
                throw new DataFormatException("This line contains incomplete data");
            } catch (DataFormatException e) {
                e.printStackTrace ();
            }
        }
        int m=Integer.parseInt (DATA[3]);
        return new MathTest (DATA[1],DATA[2],m,DATA[4],DATA[5],DATA[6]);
    }

    /**
     * 
     * @param line 读取testcatalog.dat中的一行字符串
     * @return 返回一个EnglishTest
     */
    private ProfessionalTest readProfessionalTest(String line){
        String[] DATA=line.split ("_");
        if(DATA.length!=8){
            try {
                throw new DataFormatException("This line contains incomplete data");
            } catch (DataFormatException e) {
                e.printStackTrace ();
            }
        }
        int m=Integer.parseInt (DATA[3]);
        return new ProfessionalTest (DATA[1],DATA[2],m,DATA[4],DATA[5],DATA[6],DATA[7]);
    }

    /**
     * 
     * @param filename 读取的文件名称
     * @return 返回已经初始化的 TestDatabase
     */
    public TestDatabase loadTestDatabase (String filename){
        //使用BR字符流初始化 data (ArrayList<String>)
        BufferedReader BR;
        {
            try {
                BR = new BufferedReader(new FileReader("testCatalog.dat"));
                while(true){
                    try {
                        String line;
                        if ((BR.readLine () !=null)){
                            line=BR.readLine ();
                            data.add(line);
                        }else{
                            break;
                        }
                    }
                    catch (IOException e) {
                        e.printStackTrace ();
                    }
                }
            }
            catch (FileNotFoundException e) {
                e.printStackTrace ();
            }
        }
        TestDatabase testdatabase=new TestDatabase ();
        for(String tmpstring : data){
            String[] DATA=tmpstring.split ("_");
            //switch语句判断是哪种Test类型
            switch (DATA[0]) {
                case "EnglishTest" -> testdatabase.addTest (readEnglishTest (tmpstring));
                case "MathTest" -> testdatabase.addTest (readMathTest (tmpstring));
                case "ProfessionalTest" -> testdatabase.addTest (readProfessionalTest (tmpstring));
            }
        }
        return testdatabase;
    }
}

testCatalog.dat(题目自带)

EnglishTest_E001_Translate the following text into English._2_Smooth,fluent and without language problems or wrong words_C-E
EnglishTest_E002_Translate the following article content._3_Clear logic and no language problems_E-C
EnglishTest_E003_Choose the correct answer based on the content being played._3_Correct answer_Hearing
EnglishTest_E004_Translate the following Chinese in English._2_No grammatical errors_C-E
EnglishTest_E005_Translate the following English in Chinese._2_Sentence fluent_E-C
EnglishTest_E006_Choose the correct answer based on contextual dialogue_2_Right_Hearing
EnglishTest_E007_Translate the following article content._3_Smooth and fluent_C-E
EnglishTest_E008_Translate to Chinese_3_Complete translation_E-C
EnglishTest_E009_Listen to the dialogue and choose Xiao Ming’s weekend schedule._3_Correct answer_Hearing
EnglishTest_E010_Translate the following text into English._2_Use the correct words and smooth_C-E
MathTest_M001_Find the inflection points of the following functions._2_Right_no image_no
MathTest_M002_Which of the following series converge?_3_Right_no image_no
MathTest_M003_Find the differential equation of the following function._3_Necessary problem-solving process_no image_The first step is to find the integral. The second step is to find the value of the constant c.
MathTest_M004_Find the angle between the two planes A and B._2_Correct answer_http://image.com/m004_no
MathTest_M005_Several extreme points in the figure below._3_Right_http://image.com/m005_no
MathTest_M006_Find the inflection points of the following functions._2_Right_no image_no
MathTest_M007_Find the differential equation of the following function._3_Clear problem solving process and correct value_no image_Find the value of the constant b and the differential equation
MathTest_M008_The area enclosed by the following curve and the coordinate axis is?_1_Correct answer_http://image.com/m008_no
MathTest_M009_Find general solutions of differential equations._2_The parameters are correct_no image_Find the value of the parameter
MathTest_M010_Find the function f(x)._2_Right_http://image.com/m010_no
ProfessionalTest_P001_What are the characteristics of JAVA language?_1_Right_Name at least three of the characteristics_no_no image
ProfessionalTest_P002_Fill in the following blanks to realize the calculation of the sum of the numbers between 1-200 that are not divisible by 5._2_Correct result at run_Fill in the code in the blank._no_http://image.com/p002
ProfessionalTest_P003_The time complexity of the algorithm refers to?_1_Correct answer_no_no_no image
ProfessionalTest_P004_The idea of dynamic programming_3_Right_no_no_no image
ProfessionalTest_P005_The design of the database includes two aspects of design content, they are?_2_Similar in meaning_no_no_no image
ProfessionalTest_P006_The difference between java and c._2_At least three points_At least three points_no_no image
ProfessionalTest_P007_The difference between process and thread._3_At least three points_At least three points_no_no image
ProfessionalTest_P008_The time complexity of the following code is?_1_Right_no_no_http://image.com/p008
ProfessionalTest_P009_Benefits of thread pool._3_Can name the key benefits_no_no_no image
ProfessionalTest_P010_Enter 5 numbers to find their maximum and average._3_Correct result at run_Time complexity cannot exceed n._no_no image

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值