Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. package hw7;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8.  
  9. /**
  10.  * @author Khalil Fleming
  11.  *
  12.  * ParseCampusModel
  13.  * CampusPathModelParse will handle the parsing in of the csv files given to the MVC
  14.  * by the controller and will store the information inside multiple data structures to be
  15.  * used later to store the information in the graph.
  16.  *
  17.  */
  18. public class CampusPathModelParse {
  19.    
  20.    
  21.     /**
  22.      * readEdge will store the edge csv file inside a multitude of data structures for
  23.      * future use to create a graph.
  24.      * @param filename - the file which is to be read in
  25.      * @param idToId - an ArrayList of ArrayLists that stores id1 as the first element, and id2 as the second element
  26.      * @effects adds to hash map idToId in the form of <id, id>
  27.      * @throws IOException - if the file is the incorrect format or cannot be read
  28.      */
  29.     public static void readEdge(String filename, ArrayList<ArrayList<String>> idToId)
  30.                                 throws IOException{
  31.         @SuppressWarnings("resource")
  32.         BufferedReader reader = new BufferedReader(new FileReader(filename));
  33.         String line = null;
  34.  
  35.         while ((line = reader.readLine()) != null) {
  36.             String[] temp = line.split(",");
  37.             if(temp.length != 2) {
  38.                 throw new IOException("Incorrect Edge file format");
  39.             }
  40.             String id1 = temp[0];
  41.             String id2 = temp[1];
  42.             ArrayList<String> tempArrayList = new ArrayList<>();
  43.             tempArrayList.add(id1);
  44.             tempArrayList.add(id2);
  45.             idToId.add(tempArrayList);
  46.            
  47.         }
  48.        
  49.         reader.close();
  50.        
  51.     }
  52.    
  53.     /**
  54.      * readNode will store the node csv file inside a multitude of data structures for
  55.      * future use to create a graph.
  56.      *
  57.      * @param filename - the file to be read in
  58.      * @param nameToId - a hash map which will store names as keys and id's as values
  59.      * @param idToName - a hash map which will store id's as keys and names as values
  60.      * @param idToX - a hash map which will store id's as keys and the x position as values
  61.      * @param idToY - a hash map which will store id's as keys and the y position as values
  62.      * @effects adds to hash map nameToId with the structure <name, id>
  63.      *          adds to hash map idToName with the structure <id, name>
  64.      *          adds to hash map idToX with the structure <id, x>
  65.      *          adds to hash map idToY with the structure <id, y>
  66.      * @throws IOException if the file is not in the correct format, or can not be read in.
  67.      */
  68.     public static void readNode(String filename, HashMap<String, String> nameToId,
  69.                                 HashMap<String, String> idToName, HashMap<String, Integer> idToX,
  70.                                 HashMap<String, Integer> idToY) throws IOException {
  71.        
  72.         @SuppressWarnings("resource")
  73.         BufferedReader reader = new BufferedReader(new FileReader(filename));
  74.         String line = null;
  75.  
  76.         while ((line = reader.readLine()) != null) {
  77.              String[] temp = line.split(",");
  78.              if(temp.length != 4) {
  79.                  throw new IOException("Incorrect Node file Format");
  80.              }
  81.              String buildingName = temp[0];
  82.              String buildingID = temp[1];
  83.              if(buildingName.equals("")) {
  84.                  buildingName = "Intersection " + buildingID;
  85.              }
  86.              Integer xCoordinate = Integer.parseInt(temp[2]);
  87.              Integer yCoordinate = Integer.parseInt(temp[3]);
  88.              nameToId.put(buildingName, buildingID);
  89.              idToName.put(buildingID, buildingName);
  90.              idToX.put(buildingID, xCoordinate);
  91.              idToY.put(buildingID, yCoordinate);
  92.         }
  93.         reader.close();
  94.        
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement