栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

东北大学java项目coffeesystem实验二代码哦

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

东北大学java项目coffeesystem实验二代码哦

 1. 读取文件字符流的时候,我们用FileReader,而FileInputStream类用于读字节流。

2.注意,若是函数头部已经声明可能抛出异常,那么一个已经声明可能会抛出异常的函数,调用时得用try-catch处理

3.如果你的函数可能抛出异常,就必须在函数头部加以声明,当然,你也可以声明一种并不会真的抛出的异常(就是说其实这种异常根本不会在这个方法里出现,但仍然可以声明),但注意要在引用的地方catch该异常,并处理

4.BufferedReader与BufferedWriter是什么:

你并不一定要使用这两个类,不用程序也是ok的,但

BufferedReader与BufferedWriter顾名思义就是缓冲读入与缓冲写入,它们的存在是为了提高读入和写入的效率的,当我们想要从文件读入或写入某些数据,如:BufferedReader在读取文件时,会先从文件中读入字符数据并放入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取,如果缓冲区数据不足,才会再从文件中读取,这样读取的效率会提高,BufferedWriter同理

5.StringTokenizer介绍如下:

StringTokenizer这个类可以实现让我们根据自己需要分割字符串

比如:StringTokenizer D=new StringTokenizer(line,"_");

意思就是对于line这个字符串,我们用_下划线分割,假如line=hello_world_love_you,

经过分割后就变成了4个小字符串hello,world,love,you,对于D,那是一个容器,存放所有分割好的小字符串,用D.nextToken()读取

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

public class FileCatalogLoader implements CatalogLoader {
	
	
	private Coffee readCoffee(String line)throws DataFormatException
	{
		try {
			
			StringTokenizer L=new StringTokenizer(line,"_");
			String Coffee=L.nextToken();
			String code=L.nextToken();
			String description=L.nextToken();
			double price=Double.valueOf(L.nextToken());//字符串转成double型数据
			String origin=L.nextToken();
			String roast=L.nextToken();
			String flavor=L.nextToken();
			String aroma=L.nextToken();
			String acidity=L.nextToken();
			String body=L.nextToken();

			return new Coffee(code, description, price, origin, roast, flavor, aroma, acidity, body);
			
			
		} catch (NoSuchElementException e) {
			
			throw e;
		}
		
		
		
		
		
		
		
		
	}
	
	private Product readProduct(String line)throws DataFormatException
	{
		try {
		StringTokenizer B=new StringTokenizer(line,"_");
		
		String product=B.nextToken();
		String code=B.nextToken();
		String description=B.nextToken();
		double price=Double.parseDouble(B.nextToken());
		
		return  new Product(code, description, price);
		}catch (NoSuchElementException e) {
			
			throw e;
		}
	}
	
	private CoffeeBrewer readCoffeeBrewer(String line)throws DataFormatException
	{
		try {
			StringTokenizer D=new StringTokenizer(line,"_");
			String Brewer=D.nextToken();
			String description=D.nextToken();
			String code=D.nextToken();
			double price=Double.parseDouble(D.nextToken());
			String model=D.nextToken();
			String waterSupply=D.nextToken();
			int numberOfCups=Integer.parseInt(D.nextToken());
			
			return new CoffeeBrewer(description, code, price, model, waterSupply, numberOfCups);
					
			
					
			
			
			
		} catch (NoSuchElementException e) {
			
			throw e;
		}
		
		
		
		
	}
	
	
	@Override
	public Catalog loadCatalog(String fileName) throws FileNotFoundException, IOException, DataFormatException {
		
		Catalog catalog=new Catalog();
		String line;
		
		BufferedReader lbd=new BufferedReader(
				new FileReader(fileName)//FileReader用于读取字符流
				);
	
		try {
        while ((line = lbd.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));
            }
        }
		}catch (FileNotFoundException e) {
			throw e;
		}catch (IOException e) {
			throw e;
		}catch (DataFormatException e) {
			throw e;
		}
		
		
        lbd.close();
        return catalog;

		

	}

	
}

 还有一个是GourmetCoffee里的writeFile补全代码。

private void writeFile(String filename, String content)
		throws IOException {

		
		try {
			BufferedWriter lbd=new BufferedWriter(new FileWriter(filename));
			lbd.write(content);
			lbd.close();
			
		} catch (IOException e) {
			throw e;
		}
		
		
	}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830167.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号