Advertisement
Guest User

Untitled

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