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

📄 filecatalogloader.java

📁 SSD3 Exercise Six Ex 6
💻 JAVA
字号:
import java.util.*;
import java.io.*;


public class FileCatalogLoader implements CatalogLoader{

	private final static String PRODUCT_PREFIX = "Product";
	
	private final static String COFFEE_PREFIX = "Coffee";
	
	private final static String BREWEW_PREFIX = "Brewer";
	
	private final static String DELIM = "_";
	
	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(BREWEW_PREFIX))
			{
				product = readBrewer(line);
			}
			else    
			{   
				throw new DataFormatException(line);   
			}  
			
			catalog.addProduct(product);
			
			line = reader.readLine();
		}
		
		return catalog;
	}
	
	/**
	 * 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 product data in the specified line and returns 
	 * a {@link Product} object that encapsulates the book 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 CoffeeBrewer data in the specified line and returns 
	 * a {@link Book} object that encapsulates the CoffeeBrewer data.
	 * 
	 * @param line   a string that contains book data.
	 * @return a <code>CoffeeBrewer</code> object that encapsulates the  
	 *         CoffeeBrewer data in the specified line.
	 * @throws DataFormatException if the line contains errors.
	 */
	private CoffeeBrewer readBrewer(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 + -