⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filecatalogloader.java

📁 这是我修读美国卡耐基梅隆大学Carnegie Mellon University(CMU)课程ssd3:Object-Oriented Programming and Design时完成的课程设计
💻 JAVA
字号:
import java.util.*;
import java.io.*;

/**
 * Creates a coffee catalog and loads it with data stored in
 * a file.
 *
 * @author 张维
 * @version  1.0.0
 * @see CatalogLoader
 * @see Catalog
 * @see CatalogItem
 * @see Product
 * @see Coffee
 * @see CoffeeBrewer
 */
public class FileCatalogLoader implements CatalogLoader  {

    /* Prefix of a line with product data */
    private final static String PRODUCT_PREFIX = "Product";

    /* Prefix of a line with coffee data */
    private final static String COFFEE_PREFIX = "Coffee";
    
    /* Prefix of a line with brewer data */
    private final static String Brewer_PREFIX = "Brewer";

    /* Delimiter */
    private final static String DELIM = "_";

    /**
     * Loads the information in the specified file into a 
     * catalog and returns the catalog.
     *
     * @param filename  The name of a file that contains catalog
     *                  information.
     * @return a catalog.
     * @throws FileNotFoundException  if the specified file does not
     *                                exist.
     * @throws IOException if there is an error reading the
     *                     information in the specified file.
     * @throws DataFormatException if the file contains malformed
     *                             data.
     */
    public Catalog loadCatalog (String filename) throws IOException,
        FileNotFoundException, DataFormatException {

        Catalog catalog = new Catalog();

        BufferedReader reader =
            new BufferedReader(new FileReader(filename));
        String line =  reader.readLine();

        while (line != null) {

            Product product = null;

            if (line.startsWith(PRODUCT_PREFIX)) {
                product = readProduct(line);
            } else if (line.startsWith(COFFEE_PREFIX)) {
                product = readCoffee(line);
            } else if (line.startsWith(Brewer_PREFIX)) {
                product = readCoffeeBrewer(line);
            }else {

                throw new DataFormatException(line);
            }

            catalog.addProduct(product);

            line =  reader.readLine();
        }

        return catalog;
    }

    /**
     * Extracts the product data in the specified line and returns
     * a {@link Product} object that encapsulates the product data.
     *
     * @param line  a string that contains product data.
     * @return  a <code>Product</code> object that encapsulates the
     *          product data in the specified line.
     * @throws DataFormatException if the line contains errors.
     */
    private Product readProduct (String line) throws DataFormatException {

        StringTokenizer tokenizer = new StringTokenizer(line, DELIM);

        if (tokenizer.countTokens() != 4) {

            throw new DataFormatException(line);
        } else {
            try {

                String prefix = tokenizer.nextToken();

                return new Product(tokenizer.nextToken(),
                                  tokenizer.nextToken(),
                                  Double.parseDouble(tokenizer.nextToken()));
            } catch (NumberFormatException  nfe)  {

                throw new DataFormatException(line);
            }
        }
    }

    /**
     * Extracts the coffee data in the specified line and returns
     * a {@link Coffee} object that encapsulates the coffee data.
     *
     * @param line  a string that contains coffee data.
     * @return  a <code>Coffee</code> object that encapsulates the
     *          coffee data in the specified line.
     * @throws DataFormatException if the line contains errors.
     */
    private Coffee readCoffee (String line)
    throws DataFormatException {

        StringTokenizer tokenizer = new StringTokenizer(line, DELIM);

        if (tokenizer.countTokens() != 10) {

            throw new DataFormatException(line);
        } else {
            try {

                String prefix = tokenizer.nextToken();

                return new  Coffee (tokenizer.nextToken(),
                                  tokenizer.nextToken(),
                                  Double.parseDouble(tokenizer.nextToken()),
                                  tokenizer.nextToken(),
                                  tokenizer.nextToken(),
                                  tokenizer.nextToken(),
                                  tokenizer.nextToken(),
                                  tokenizer.nextToken(),
                                  tokenizer.nextToken());
            } catch (NumberFormatException  nfe)  {

                throw new DataFormatException(line);
            }
        }
    }
    
    /**
     * Extracts the brewer data in the specified line and returns
     * a {@link Coffeebrewer} object that encapsulates the brewer data.
     *
     * @param line  a string that contains brewer data.
     * @return  a <code>Coffeebrewer</code> object that encapsulates the
     *          brewer data in the specified line.
     * @throws DataFormatException if the line contains errors.
     */
    private CoffeeBrewer readCoffeeBrewer (String line)
    throws DataFormatException {

        StringTokenizer tokenizer = new StringTokenizer(line, DELIM);

        if (tokenizer.countTokens() != 7) {

            throw new DataFormatException(line);
        } else {
            try {

                String prefix = tokenizer.nextToken();

                return new  CoffeeBrewer (tokenizer.nextToken(),
                                  tokenizer.nextToken(),
                                  Double.parseDouble(tokenizer.nextToken()),
                                  tokenizer.nextToken(),
                                  tokenizer.nextToken(),
                                  Integer.parseInt(tokenizer.nextToken()));
            } catch (NumberFormatException  nfe)  {

                throw new DataFormatException(line);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -