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 

                contentOfUniqueNutrients.append("
");
                //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("
", -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;
    }

}
View Code

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;
    }

}
View Code

 最后一段改的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;}
View Code
原文地址:https://www.cnblogs.com/immiao0319/p/9868479.html