Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hw7;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- /**
- * @author Khalil Fleming
- *
- * ParseCampusModel
- * CampusPathModelParse will handle the parsing in of the csv files given to the MVC
- * by the controller and will store the information inside multiple data structures to be
- * used later to store the information in the graph.
- *
- */
- public class CampusPathModelParse {
- /**
- * readEdge will store the edge csv file inside a multitude of data structures for
- * future use to create a graph.
- * @param filename - the file which is to be read in
- * @param idToId - an ArrayList of ArrayLists that stores id1 as the first element, and id2 as the second element
- * @effects adds to hash map idToId in the form of <id, id>
- * @throws IOException - if the file is the incorrect format or cannot be read
- */
- public static void readEdge(String filename, ArrayList<ArrayList<String>> idToId)
- throws IOException{
- @SuppressWarnings("resource")
- BufferedReader reader = new BufferedReader(new FileReader(filename));
- String line = null;
- while ((line = reader.readLine()) != null) {
- String[] temp = line.split(",");
- if(temp.length != 2) {
- throw new IOException("Incorrect Edge file format");
- }
- String id1 = temp[0];
- String id2 = temp[1];
- ArrayList<String> tempArrayList = new ArrayList<>();
- tempArrayList.add(id1);
- tempArrayList.add(id2);
- idToId.add(tempArrayList);
- }
- reader.close();
- }
- /**
- * readNode will store the node csv file inside a multitude of data structures for
- * future use to create a graph.
- *
- * @param filename - the file to be read in
- * @param nameToId - a hash map which will store names as keys and id's as values
- * @param idToName - a hash map which will store id's as keys and names as values
- * @param idToX - a hash map which will store id's as keys and the x position as values
- * @param idToY - a hash map which will store id's as keys and the y position as values
- * @effects adds to hash map nameToId with the structure <name, id>
- * adds to hash map idToName with the structure <id, name>
- * adds to hash map idToX with the structure <id, x>
- * adds to hash map idToY with the structure <id, y>
- * @throws IOException if the file is not in the correct format, or can not be read in.
- */
- public static void readNode(String filename, HashMap<String, String> nameToId,
- HashMap<String, String> idToName, HashMap<String, Integer> idToX,
- HashMap<String, Integer> idToY) throws IOException {
- @SuppressWarnings("resource")
- BufferedReader reader = new BufferedReader(new FileReader(filename));
- String line = null;
- while ((line = reader.readLine()) != null) {
- String[] temp = line.split(",");
- if(temp.length != 4) {
- throw new IOException("Incorrect Node file Format");
- }
- String buildingName = temp[0];
- String buildingID = temp[1];
- if(buildingName.equals("")) {
- buildingName = "Intersection " + buildingID;
- }
- Integer xCoordinate = Integer.parseInt(temp[2]);
- Integer yCoordinate = Integer.parseInt(temp[3]);
- nameToId.put(buildingName, buildingID);
- idToName.put(buildingID, buildingName);
- idToX.put(buildingID, xCoordinate);
- idToY.put(buildingID, yCoordinate);
- }
- reader.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement