JAVA HW2
MODEL
//yuec2 Yue Cheng
package hw2; import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner; import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap; public class Model extends DataFiler{
ObservableMap<StringProperty, Product> productsMap = FXCollections.observableHashMap();
static ObservableMap<StringProperty, Nutrient> nutrientsMap = FXCollections.observableHashMap();
Product[] products;
Nutrient[] nutrients; public void readProducts(String productFile) {
// TODO Auto-generated method stub
//initialize two Scanner
//Scanner fileScanner is used to count the number of lines in file
Scanner fileScanner = null;
//Scanner sc is used to read information into the products array
Scanner sc = null;
//fileContent is used to store all the information for this array
StringBuilder fileContent = new StringBuilder(); try {
File file = new File(productFile);
fileScanner = new Scanner(file);
sc = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int countOfLines = 0; //skip the title line
fileScanner.nextLine();
while (fileScanner.hasNextLine()) {
//add all the information into the fileContent
fileContent.append(fileScanner.nextLine());
//get countOfLines by using fileScanner
countOfLines++; }
//initialize a Product array using the countOfLines as size
products = new Product[countOfLines]; //skip the title line
sc.nextLine();
for (int i = 0; i < countOfLines; i++) {
//put every line's product information into a String array temp
String[] temp = sc.nextLine().toString().split("\",\"");
//get information from the temp array and assign as each product's information
products[i] = new Product();
//StringProperty is not a string?
products[i].ndbNumber.set(temp[0]+'"');
products[i].productName.set(temp[1].trim());
products[i].ingredients.set(temp[7].trim());
products[i].manufacturer.set(temp[4].trim());
productsMap.put(products[i].ndbNumber, products[i]);
}
} public void readNutrients(String nutrientFile) {
//initialize 2 Scanner
//Scanner fileScanner is used to count the number of lines in file
Scanner fileScanner = null;
//Scanner sc is used to read information into the nutrients array
Scanner sc = null;
//StringBuilder contentOfUniqueNutrients is used to store information of the unique nutrients
StringBuilder contentOfUniqueNutrients = new StringBuilder();
//fileContent is used to store all the information for this array
StringBuilder fileContent = new StringBuilder(); try {
File file = new File(nutrientFile);
fileScanner = new Scanner(file);
sc = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//skip the title line
fileScanner.nextLine();
//initialize the count numbers
int countOfLines = 0;
int numOfUniqueNutirents = 0; while (fileScanner.hasNextLine()) {
//get one line's information into the String cur
String cur = fileScanner.nextLine();
//extract the nutrients's id to identify each unique nutrient
String id = cur.split("\",\"")[1];
//add all the information into the fileContent
fileContent.append(cur);
//get countOfLines by using fileScanner
countOfLines++; if (!contentOfUniqueNutrients.toString().contains(id)) {
//if the id is unique, append this nutrient's information
contentOfUniqueNutrients.append(cur);
//split each line by \n
contentOfUniqueNutrients.append("\n");
//count the number for unique nutrients
numOfUniqueNutirents++;
}
} //skip the title line
sc.nextLine();
//initialize an array using the numOfUniqueNutirents as size
nutrients = new Nutrient[numOfUniqueNutirents];
//store each line's information into a string array
String[] lines = contentOfUniqueNutrients.toString().split("\n", -1);
for (int i = 0; i < numOfUniqueNutirents; i++) {
//split each array's elements
String[] temp1 = lines[i].toString().split("\",", -1);
//assign each element's information
nutrients[i] = new Nutrient();
nutrients[i].nutrientCode.set(temp1[1].replaceAll("\"", "").trim());
nutrients[i].nutrientName.set(temp1[2].replaceAll("\"", "").trim());
nutrients[i].nutrientUom.set(temp1[5].replaceAll("\"", "").trim());
nutrientsMap.put(nutrients[i].nutrientCode, nutrients[i]);
}
} public void readServingSizes(String servingSizeFile) {
//initialize two Scanner
//Scanner fileScanner is used to count the number of lines in file
Scanner fileScanner = null;
//Scanner sc is used to read information into the products array
Scanner sc = null;
//add all the information into the fileContent
StringBuilder fileContent = new StringBuilder(); try {
File file = new File(servingSizeFile);
fileScanner = new Scanner(file);
sc = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} int countOfLines = 0;
//get countOfLines
fileScanner.nextLine();
while (fileScanner.hasNextLine()) {
fileContent.append(fileScanner.nextLine());
countOfLines++;
} //count the number of words of each line of serving size file
int lengthOfServingSizeFile = sc.nextLine().toString().split("\",\"").length;
//initialize an array using the lengthOfServingSizeFile as size
String[] temp = new String[lengthOfServingSizeFile]; for (int i = 0; i < countOfLines; i++) {
//split each array's elements
temp = sc.nextLine().toString().split("\",\"");
//assign each element's information
products[i] = new Product();
products[i].servingSize.set(Float.parseFloat(temp[1].trim()));
products[i].servingUom.set(temp[2].trim());
products[i].householdSize.set(Float.parseFloat(temp[3].trim()));
products[i].householdUom.set(temp[4].trim());
productsMap.put(products[i].ndbNumber, products[i]);
}
} @Override
public void writeFile(String filename) {
// TODO Auto-generated method stub } @Override
public boolean readFile(String filename) {
// TODO Auto-generated method stub
//how to judge the file?
CSVFiler cf = new CSVFiler();
XMLFiler xf = new XMLFiler();
if (filename.contains("csv")) cf.readFile(filename);
if (filename.contains("xml")) xf.readFile(filename);
return false;
} }
CSVFiler
package hw2; import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner; public class CSVFiler extends DataFiler{ @Override
public void writeFile(String filename) {
// TODO Auto-generated method stub
//no code in this version
} @Override
public boolean readFile(String filename) {
// TODO Auto-generated method stub
Scanner sc = null;
//add all the information into the fileContent
StringBuilder fileContent = new StringBuilder(); try {
File file = new File("Profile1.csv");
sc = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
fileContent.append(sc.nextLine()); String[] temp = fileContent.toString().split("\",\"");
//Female(float age, float weight, float height, float physicalActivityLevel, String ingredientsToAvoid) {
Female female = new Female(Float.valueOf(temp[1]), Float.valueOf(temp[2]),
Float.valueOf(temp[3]), Float.valueOf(temp[4]),temp[5]);
Person p = female;
//how to judge correct or not? return false;
} }
最后一段改的CSVFiler
public
boolean readFile(String filename) {
CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader(); try {
CSVParser csvParser = CSVParser.parse(new FileReader(filename), csvFormat); for (CSVRecord csvRecord : csvParser) {
Person person = new Person(csvRecord.get(1), csvRecord.get(2), csvRecord.get(3), csvRecord.get(4), csvRecord.get(5));
} }
catch (FileNotFoundException e1) { e1.printStackTrace(); return false;}
catch (IOException e1) { e1.printStackTrace(); return false;} return true;}
JAVA HW2的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- java返回一个简单的日历
import java.text.*; //首先得导包 import java.util.*; public class hw2 { /** * 计算日期差 返回的天数 * @param dstr1 ...
- Java Gradle入门指南之简介、安装与任务管理
这是一篇Java Gradle入门级的随笔,主要介绍Gradle的安装与基本语法,这些内容是理解和创建build.gradle的基础,关于Gradle各种插件的使用将会在其他随笔中介绍. ...
- JAVA基础知识之网络编程——-TCP/IP协议,socket通信,服务器客户端通信demo
OSI模型分层 OSI模型是指国际标准化组织(ISO)提出的开放系统互连参考模型(Open System Interconnection Reference Model,OSI/RM),它将网络分为七 ...
- java的nio之:java的nio的服务器实现模型
[nio服务端序列图]
- Java相关错误
http://blog.csdn.net/pipisorry/article/details/51291063 使用hadoop jar ./Hw2Part1.jar /hw2/example-inp ...
- java 中什么是aop
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- 【转】Java Spring AOP详解
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- JAVA总结--Spring框架全解
一.Spring简介 Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring 框架目标是简化Java企业 ...
随机推荐
- YUV420格式解析<转>
在YUV420中,一个像素点对应一个Y,一个2X2的小方块对应一个U和V.对于所有YUV420图像,它们的Y值排列是完全相同的,因为只有Y的图像就是灰度图像. YUV420sp与YUV420p的数据格 ...
- Lazarus 中文汉字解决方案
使用Lazarus不得不面对编码问题,尤其中文.Lazarus使用的是UTF8编码,而很多windows程序使用的是ANSI编码,编码问题在此不多说大家可以google去. ANSI数据库与Lazar ...
- cin.tie与sync_with_stdio加速输入输出
在LeetCode上练习习题的时候每次AC之后都会去看别人的代码,几乎每次都能遇到cin.tie与sync_with_stdio.类似这样: static auto x = [](){ std::io ...
- Visual Studio配置C/C++-PostgreSQL(9.6.3)开发环境(ZT)
https://www.2cto.com/database/201707/658910.html 开发环境 Visual Studio 2017[15.2(26430.16)] PostgreSQL ...
- 自行编译mwan加入openwrt里
参考源文:http://www.right.com.cn/forum/thread-124449-1-1.html 本例以 opoenwrt 12.09正式版为例,原软件来自openwrt 英文论坛: ...
- 【原】Win7 host 与 virtualbox ubuntu guest 相同ping通
环境:Win7 host 与 virtualbox ubuntu guest 在 “设置”-->"网络" 中选择网卡1,修改连接方式为 “桥接网卡” 后, 主机和虚拟机可以相 ...
- IE 11中 onpropertychange失效
https://msdn.microsoft.com/zh-cn/library/ie/dn265032(v=vs.85).aspx 将突变事件和属性更改事件迁移到突变观察者 Internet Exp ...
- 吴裕雄 02-mysql PHP语法
mysqli_connect($connect);mysqli_query($connect,"SQL 语句");mysqli_fetch_array()mysqli_close( ...
- Java中字节流如何转字符流,OutputStreamWriter用法
OutputStreamWriter 将字节流转换为字符流.是字节流通向字符流的桥梁.如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:UTF-8: 步骤: 1.创建流 子类对象 绑定数 ...
- Aptana在Eclipse的安装
1.下载 com.aptana.rcp.product-3.4.2.zip文件 https://pan.baidu.com/s/1sl81Vit 2.安装 接着Next.直到成功 3.怎么判定安装成功 ...