Advertisement
lameski

Untitled

Jun 9th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.time.LocalDateTime;
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. /**
  7. * Partial exam II 2016/2017
  8. */
  9.  
  10. class File implements Comparable<File>
  11. {
  12. String name;
  13. Integer size;
  14. LocalDateTime createdAt;
  15.  
  16. File(String name, int size, LocalDateTime createdAt)
  17. {
  18. this.name = name;
  19. this.size = size;
  20. this.createdAt = createdAt;
  21. }
  22.  
  23. @Override
  24. public int compareTo(File o) {
  25. if(createdAt.compareTo(o.createdAt)==0)
  26. {
  27. if(name.compareTo(o.name)==0)
  28. {
  29. return size.compareTo(o.size);
  30. }
  31. else
  32. return name.compareTo(o.name);
  33. }
  34. else return createdAt.compareTo(o.createdAt);
  35. }
  36.  
  37. @Override
  38. public String toString() {
  39.  
  40. return String.format("%-10s %5dB %s", name,size,createdAt);
  41.  
  42. }
  43.  
  44. }
  45. class FileSystem
  46. {
  47. Map<Character,File> files;
  48. FileSystem()
  49. {
  50. files = new HashMap<>();
  51. }
  52.  
  53. public void addFile(char folder, String name, int size, LocalDateTime createdAt)
  54. {
  55. files.put(folder, new File(name, size, createdAt));
  56. }
  57. public List<File> findAllHiddenFilesWithSizeLessThen(int size)
  58. {
  59. return files.values().stream()
  60. .filter(val -> val.name.startsWith("."))
  61. .filter(val -> val.size<size)
  62. .collect(Collectors.toList());
  63. }
  64. public int totalSizeOfFilesFromFolders(List<Character> folders)
  65. {
  66. int size = 0;
  67. for(char f : folders)
  68. {
  69. if(files.containsKey(f))
  70. {
  71. size+=files.get(f).size;
  72. }
  73. }
  74. return size;
  75. }
  76. public Map<Integer, Set<File>> byYear()
  77. {
  78. Map<Integer,Set<File>> map = new TreeMap<>();
  79. Set<File>set = new TreeSet<>();
  80. files.values().stream()
  81. .forEach(f -> {
  82. set.add(f);
  83. map.put(f.createdAt.getYear(),set);
  84. });
  85. return map;
  86. }
  87.  
  88. /*public Map<String, Long> sizeByMonthAndDay()
  89. {
  90.  
  91. }*/
  92. }
  93.  
  94. public class FileSystemTest {
  95. public static void main(String[] args) {
  96. FileSystem fileSystem = new FileSystem();
  97. Scanner scanner = new Scanner(System.in);
  98. int n = scanner.nextInt();
  99. scanner.nextLine();
  100. for (int i = 0; i < n; i++) {
  101. String line = scanner.nextLine();
  102. String[] parts = line.split(":");
  103. fileSystem.addFile(parts[0].charAt(0), parts[1],
  104. Integer.parseInt(parts[2]),
  105. LocalDateTime.of(2016, 12, 29, 0, 0, 0).minusDays(Integer.parseInt(parts[3]))
  106. );
  107. }
  108. int action = scanner.nextInt();
  109. if (action == 0) {
  110. scanner.nextLine();
  111. int size = scanner.nextInt();
  112. System.out.println("== Find all hidden files with size less then " + size);
  113. List<File> files = fileSystem.findAllHiddenFilesWithSizeLessThen(size);
  114. files.forEach(System.out::println);
  115. } else if (action == 1) {
  116. scanner.nextLine();
  117. String[] parts = scanner.nextLine().split(":");
  118. System.out.println("== Total size of files from folders: " + Arrays.toString(parts));
  119. int totalSize = fileSystem.totalSizeOfFilesFromFolders(Arrays.stream(parts)
  120. .map(s -> s.charAt(0))
  121. .collect(Collectors.toList()));
  122. System.out.println(totalSize);
  123. } else if (action == 2) {
  124. System.out.println("== Files by year");
  125. Map<Integer, Set<File>> byYear = fileSystem.byYear();
  126. byYear.keySet().stream().sorted()
  127. .forEach(key -> {
  128. System.out.printf("Year: %d\n", key);
  129. Set<File> files = byYear.get(key);
  130. files.stream()
  131. .sorted()
  132. .forEach(System.out::println);
  133. });
  134. } else if (action == 3) {
  135. /* System.out.println("== Size by month and day");
  136. Map<String, Long> byMonthAndDay = fileSystem.sizeByMonthAndDay();
  137. byMonthAndDay.keySet().stream().sorted()
  138. .forEach(key -> System.out.printf("%s -> %d\n", key, byMonthAndDay.get(key)));*/
  139. }
  140. scanner.close();
  141. }
  142. }
  143.  
  144. // Your code here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement