Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Scanner;
- import java.util.Map.Entry;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Arrays;
- import java.util.Collection;
- public class Relation {
- String name;
- Attribute attribute1;
- Attribute attribute2;
- String tablename1;
- String tablename2;
- private String tableName;
- private String[] attributeNames;
- private String[] attributes;
- private String keyName;
- HashMap<Attribute, ArrayList<Attribute>> storage;
- //could use iterable
- /**
- * Add every course into the database
- * Add the students into the array,
- * if a course is being added for the second time, we need to add the student to the end of the arraylist
- */
- //must load two tables
- //relation just reads the different tables
- //
- public Relation(File inputFile) {
- // Set up scanner
- Scanner scanner = null;
- try {
- scanner = new Scanner(inputFile);
- } catch (FileNotFoundException fnf) {
- System.out.println("Could not find file!");
- System.exit(1);
- }
- // Read in header info
- scanner.nextLine(); // Skip the first line
- // Get table name
- this.tableName = scanner.nextLine();
- this.tableName = tableName.substring(8, tableName.length());
- // Get attribute names
- String attributeNamesStr = scanner.nextLine();
- attributeNamesStr = attributeNamesStr.substring(19, attributeNamesStr.length());
- this.attributeNames = attributeNamesStr.split(" ");
- // Get attribute types
- String attributesStr = scanner.nextLine();
- attributesStr = attributesStr.substring(19, attributesStr.length());
- this.attributes = attributesStr.split(" ");
- // Get key
- this.keyName = scanner.nextLine();
- this.keyName = keyName.substring(7, keyName.length());
- // Skip the line of hashes at end of header
- scanner.nextLine();
- // Read in data
- // Get the index of the key in the row
- int keyIndex = Arrays.asList(this.attributeNames).indexOf(this.keyName);
- // Store the data according to the format in the header
- storage = new HashMap<>();
- while (scanner.hasNext()) {
- Attribute key = null;
- ArrayList<Attribute> values = new ArrayList<>(); // might need to put somewhere else
- for (int i = 0; i < this.attributes.length; i++) {
- Attribute value = null;
- switch (this.attributes[i]) {
- case ("INTEGER"):
- value = new IntAttribute(scanner.nextInt());
- break;
- case ("STRING"):
- value = new StringAttribute(scanner.next());
- break;
- case ("LINE_STRING"):
- value = new StringAttribute(scanner.nextLine());
- break;
- }
- values.add(value);
- if (i == keyIndex)
- key = value;
- if (storage.get(key) != null) {
- storage.get(key).add(value);
- }
- }
- if (storage.get(key) == null)
- storage.put(key, values);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement