东北大学 Java新手练习 实验2 Using File I/O in the Gourmet Coffee System

文档源码见chenjinsui.xyz

需求分析

过去的卖咖啡系统将Catalog信息硬编码在程序中,而这次改为了在文件中。
要求我们从文件中读取catalog信息并返回一个对象。并能够将catalog信息以三种方式写在文件中。

FileCatalogLoader类的实现

这个类有三个私有方法和一个公有方法
私有方法readxxx作用分别是读取coffee、coffeeBrewer、product
公有方法loadCatalog作用是根据文件加载catalog并返回

由于每一行是一个完整的产品信息,所以在loadCatalog中逐行读取文件,每读到一行就储存在字符串中,并判断这行是什么产品,再调用对应的readxxx方法对字符串进行解析。

	/**
     * @param fileName the name of file with catalog information
     * @return a catalog object loaded by the specified file
     * @throws FileNotFoundException if file is not found
     * @throws IOException           if an I/O error occurs.
     * @throws DataFormatException   if there is wrong format in file
     */
    @Override
    public Catalog loadCatalog(String fileName) throws FileNotFoundException, IOException, DataFormatException {
     

        Catalog catalog = new Catalog();

        BufferedReader br = new BufferedReader(new FileReader(fileName));

        String line;
        while ((line = br.readLine()) != null) {
     
            if (line.startsWith("Coffee")) {
     
                catalog.addProduct(readCoffee(line));
            } else if (line.startsWith("Brewer")) {
     
                catalog.addProduct(readCoffeeBrewer(line));
            } else if (line.startsWith("Product")) {
     
                catalog.addProduct(readProduct(line));
            } else {
     
                throw new DataFormatException("Wrong type of product");
            }
        }

        br.close();
        return catalog;
    }

readxxx方法以readCoffee为例
用StringTokenizer类的对象将字符串根据下划线切分开并用变量分别接收。
再构造Coffee对象并返回。

引用Java SE 15 & JDK 15官方文档中对nextToken方法的描述
东北大学 Java新手练习 实验2 Using File I/O in the Gourmet Coffee System_第1张图片
该方法可能会抛出NoSuchElementException异常。
所以如果捕获到这个异常,说明catalog.dat中的格式有误,此时抛出自定义的异常DataFormatException。

	/**
     * @param line A line of the file with catalog information.The line contains a piece of information of a single coffee.
     * @return a coffee object instantiated with the line
     * @throws DataFormatException if there is wrong format of coffee in file
     */
    private Coffee readCoffee(String line) throws DataFormatException {
     
        try {
     
            StringTokenizer st = new StringTokenizer(line, "_");
            st.nextToken(); // to treat prefix of no use
            String code = st.nextToken();
            String description = st.nextToken();
            double price = Double.parseDouble(st.nextToken());
            String origin = st.nextToken();
            String roast = st.nextToken();
            String flavor = st.nextToken();
            String aroma = st.nextToken();
            String acidity = st.nextToken();
            String body = st.nextToken();
            return new Coffee(code, description, price, origin, roast, flavor, aroma, acidity, body);
        } catch (NoSuchElementException e) {
     
            throw new DataFormatException("wrong format of coffee in file");
        }

    }

GourmetCoffee类的补全

writeFile方法将指定字符串写入指定文件中。

	/**
     * Creates a new file (which has the specified name) and writes
     * the specified string to the new file.
     *
     * @param filename name of the file that will store the data
     * @param content  data to be stored
     */
    private void writeFile(String filename, String content)
            throws IOException {
     

        BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
        bw.write(content);
        bw.close();

    }

注意事项

注意及时关闭输入输出流,不然会浪费资源

GourmetCoffee.java在IDE中直接运行可能会出现问题,建议在命令行中运行>java GourmetCoffee catalog.dat或者投机取巧,稍微修改一下主方法的args变量。

你可能感兴趣的:(Java学习,东北大学,#,作业,java)