Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. import java.util.Stack;
  2. import java.util.Queue;
  3. import java.util.LinkedList;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. // This is to read files
  7. import java.util.Scanner;
  8.  
  9. public class ADT {
  10.  
  11. static Queue<String> line = new LinkedList<String>();
  12. public static void main(String[] args) throws FileNotFoundException {
  13.  
  14. // System.out.println(matcher("(45 + 36)-5"));
  15. // System.out.println(matcher("((a)"));
  16. // System.out.println(matcher("map{key[a(4)]}{b([v])}"));
  17. // System.out.println(matcher("({)}"));
  18. //
  19. // Queue<String[]> scoop = new LinkedList<String[]>();
  20. // String[] flavor1 = {"mint", "chocolate"};
  21. //
  22. // scoop.add(flavor1);
  23. //
  24. // String[] flavor2 = {"strawberry", "vanilla"};
  25. // scoop.add(flavor2);
  26. //
  27. // String[] flavor3 = {"choc. chip cookie dough"};
  28. // scoop.add(flavor3);
  29. //
  30. // String[] flavor4 = {"mint", "lime", "lime"};
  31. // scoop.add(flavor4);
  32. //
  33. // String[] flavor5 = {"orange", "green tea"};
  34. // scoop.add(flavor5);
  35. //
  36. // order(scoop);
  37.  
  38. System.out.print("\t\t\t\t\t\t"+"cookie\t\t"+"recipe\t\t"+"total");
  39. frequency("/Users/salikh/eclipse-workspace/adt/src/bettycrocker.txt", "cookie", "recipe");
  40. frequency("/Users/salikh/eclipse-workspace/adt/src/biggerbolderbaking.txt", "cookie", "recipe");
  41. frequency("/Users/salikh/eclipse-workspace/adt/src/cookiessf.txt", "cookie", "recipe");
  42. frequency("/Users/salikh/eclipse-workspace/adt/src/marthastewart.txt", "cookie", "recipe");
  43. frequency("/Users/salikh/eclipse-workspace/adt/src/mozilla.txt", "cookie", "recipe");
  44. frequency("/Users/salikh/eclipse-workspace/adt/src/tasteofhome.txt", "cookie", "recipe");
  45. frequency("/Users/salikh/eclipse-workspace/adt/src/thebakingchocolatess.txt", "cookie", "recipe");
  46.  
  47. }
  48.  
  49. /**
  50. * Uses stack to create regex like action to detect
  51. * syntax errors with brackets
  52. * @param brackets
  53. * @return boolean parenthesis are closed properly or not
  54. */
  55. public static boolean matcher(String brackets) {
  56.  
  57. Stack<Character> stack = new Stack<Character>();
  58.  
  59. for(int i = 0; i < brackets.length(); i++) {
  60. char bracketchar = brackets.charAt(i);
  61. if(bracketchar == '[' || bracketchar == '(' || bracketchar == '{' ) {
  62. stack.push(bracketchar);
  63. } else if(bracketchar == ']') {
  64. if(stack.isEmpty() || stack.pop() != '[') {
  65. return false;
  66. }
  67. } else if(bracketchar == ')') {
  68. if(stack.isEmpty() || stack.pop() != '(') {
  69. return false;
  70. }
  71. } else if(bracketchar == '}') {
  72. if(stack.isEmpty() || stack.pop() != '{') {
  73. return false;
  74. }
  75. }
  76.  
  77. }
  78. return stack.isEmpty();
  79. }
  80.  
  81.  
  82.  
  83. /**
  84. * Order method that adds new item to the queue
  85. * @param orders
  86. * @return
  87. */
  88. public static String order(Queue<String[]> orders) {
  89.  
  90. while(!orders.isEmpty()) {
  91. String[] current_order = orders.remove();
  92.  
  93. if (current_order[0].equals("12* choc. chip cookie dough")) {
  94. System.out.print("cone: 12* choc. chip cookie dough");
  95. continue;
  96. }
  97. if (line.isEmpty()) {
  98. System.out.println("cone: " + String.join(",",current_order));
  99. } else {
  100. String temp = "";
  101. for (String i: line) {
  102. temp += i+",";
  103. }
  104. System.out.println("cone: "+temp+String.join(",",current_order));
  105. }
  106. line.clear();
  107. for (int i=0; i<current_order.length; i++) {
  108. if (current_order[i].equals("mint") ||current_order[i].equals("green tea") || current_order[i].equals("lime")){
  109. line.add(current_order[i]);
  110. }
  111. else if (current_order[i].equals("choc. chip cookie dough")) {
  112. String[] temp = {"12* choc. chip cookie dough"};
  113. orders.add(temp);
  114. }
  115. }
  116. }
  117. return "" ;
  118. }
  119.  
  120. /**
  121. * Returns the number of times the word are found
  122. * @param file
  123. * @param wordA
  124. * @param wordB
  125. * @return
  126. * @throws FileNotFoundException
  127. */
  128. public static int frequency(String file, String wordA, String wordB) throws FileNotFoundException {
  129. int word1 = 0;
  130. int word2 = 0;
  131. File file1 = new File(file);
  132. Scanner sc = new Scanner(file1);
  133. while (sc.hasNext()) {
  134. String nextToken = sc.next();
  135. if (nextToken.equalsIgnoreCase(wordA))
  136. word1++;
  137. if (nextToken.equals(wordB))
  138. word2++;
  139. }
  140. System.out.println();
  141. int total = word2 + word1;
  142. String[] filename = file.split("/");
  143. String name = filename[filename.length-1];
  144. String format = "%1$-50s%2$-15s%3$-15s%4$-15s\n";
  145. System.out.format(format, name, word1, word2, total);
  146. return total;
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement