Filip_Markoski

[NP] FileSystem

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