Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Scanner;
  4. import java.util.Map.Entry;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.util.Arrays;
  8. import java.util.Collection;
  9.  
  10. public class Relation {
  11.  
  12.  
  13. String name;
  14. Attribute attribute1;
  15. Attribute attribute2;
  16. String tablename1;
  17. String tablename2;
  18.  
  19. private String tableName;
  20. private String[] attributeNames;
  21. private String[] attributes;
  22. private String keyName;
  23.  
  24. HashMap<Attribute, ArrayList<Attribute>> storage;
  25.  
  26.  
  27. //could use iterable
  28.  
  29. /**
  30. * Add every course into the database
  31. * Add the students into the array,
  32. * if a course is being added for the second time, we need to add the student to the end of the arraylist
  33. */
  34. //must load two tables
  35. //relation just reads the different tables
  36. //
  37. public Relation(File inputFile) {
  38.  
  39. // Set up scanner
  40. Scanner scanner = null;
  41. try {
  42. scanner = new Scanner(inputFile);
  43. } catch (FileNotFoundException fnf) {
  44. System.out.println("Could not find file!");
  45. System.exit(1);
  46. }
  47.  
  48. // Read in header info
  49. scanner.nextLine(); // Skip the first line
  50.  
  51. // Get table name
  52. this.tableName = scanner.nextLine();
  53. this.tableName = tableName.substring(8, tableName.length());
  54.  
  55. // Get attribute names
  56. String attributeNamesStr = scanner.nextLine();
  57. attributeNamesStr = attributeNamesStr.substring(19, attributeNamesStr.length());
  58. this.attributeNames = attributeNamesStr.split(" ");
  59.  
  60. // Get attribute types
  61. String attributesStr = scanner.nextLine();
  62. attributesStr = attributesStr.substring(19, attributesStr.length());
  63. this.attributes = attributesStr.split(" ");
  64.  
  65. // Get key
  66. this.keyName = scanner.nextLine();
  67. this.keyName = keyName.substring(7, keyName.length());
  68.  
  69. // Skip the line of hashes at end of header
  70. scanner.nextLine();
  71.  
  72. // Read in data
  73.  
  74. // Get the index of the key in the row
  75. int keyIndex = Arrays.asList(this.attributeNames).indexOf(this.keyName);
  76.  
  77. // Store the data according to the format in the header
  78. storage = new HashMap<>();
  79. while (scanner.hasNext()) {
  80. Attribute key = null;
  81. ArrayList<Attribute> values = new ArrayList<>(); // might need to put somewhere else
  82. for (int i = 0; i < this.attributes.length; i++) {
  83. Attribute value = null;
  84. switch (this.attributes[i]) {
  85. case ("INTEGER"):
  86. value = new IntAttribute(scanner.nextInt());
  87. break;
  88. case ("STRING"):
  89. value = new StringAttribute(scanner.next());
  90. break;
  91. case ("LINE_STRING"):
  92. value = new StringAttribute(scanner.nextLine());
  93. break;
  94. }
  95.  
  96. values.add(value);
  97. if (i == keyIndex)
  98. key = value;
  99.  
  100. if (storage.get(key) != null) {
  101. storage.get(key).add(value);
  102.  
  103. }
  104.  
  105.  
  106. }
  107. if (storage.get(key) == null)
  108. storage.put(key, values);
  109. }
  110.  
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement