Guest User

Untitled

a guest
Apr 26th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.TreeMap;
  3. import java.util.TreeSet;
  4.  
  5. public class GUnit {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         String input;
  9.         TreeMap<String, Class> classTests = new TreeMap<>();
  10.         while (true) {
  11.             input = sc.nextLine();
  12.             if (input.equals("It's testing time!")) {
  13.                 sc.close();
  14.                 break;
  15.             }
  16.             String[] data = input.split("\\s*\\|\\s*");
  17.             String className = data[0];
  18.             String methodName = data[1];
  19.             String testName = data[2];
  20.             boolean isValidInput = allValid(className, methodName, testName);
  21.  
  22.             if (data.length > 3 || !isValidInput) {
  23.                 continue;
  24.             }
  25.             if (!classTests.containsKey(className)) {
  26.                 Class clas = new Class(className);
  27.                 classTests.put(className, clas);
  28.                 clas.tests++;
  29.             }
  30.             Class clas = classTests.get(className);
  31.             if (!clas.methods.containsKey(methodName)) {
  32.                 clas.methods.put(methodName, new TreeSet<String>());
  33.             }
  34.             if (!clas.methods.get(methodName).contains(testName)) {
  35.                 clas.methods.get(methodName).add(testName);
  36.                 clas.tests++;
  37.             }
  38.         }
  39.         classTests.values().stream().sorted().forEach(System.out::println);
  40.     }
  41.  
  42.     public static boolean allValid(String word1, String word2, String word3) {
  43.         boolean firstWord = validWord(word1);
  44.         boolean secondWord = validWord(word2);
  45.         boolean thirdWord = validWord(word3);
  46.         if (firstWord && secondWord && thirdWord) {
  47.             return true;
  48.         } else {
  49.             return false;
  50.         }
  51.     }
  52.  
  53.     public static boolean validWord(String word) {
  54.         if (word.length() < 2) {
  55.             return false;
  56.         }
  57.         for (int i = 0; i < word.length(); i++) {
  58.             if (!Character.isUpperCase(word.charAt(0))) {
  59.                 return false;
  60.             }
  61.             if (!Character.isLetterOrDigit(word.charAt(i))) {
  62.                 return false;
  63.             }
  64.         }
  65.         return true;
  66.     }
  67. }
  68.  
  69. class Class implements Comparable<Class> {
  70.     public String name;
  71.     public int tests;
  72.     public TreeMap<String, TreeSet<String>> methods;
  73.  
  74.     public Class(String name) {
  75.         this.name = name;
  76.         this.tests = 0;
  77.         this.methods = new TreeMap<String, TreeSet<String>>();
  78.     }
  79.  
  80.     public int compareTo(Class o) {
  81.         int totalresult = Integer.compare(o.tests, this.tests);
  82.         if (totalresult == 0) {
  83.             totalresult = Integer.compare(this.methods.size(), o.methods.size());
  84.         }
  85.         return totalresult;
  86.     }
  87.  
  88.     @Override
  89.     public String toString() {
  90.         StringBuilder sb = new StringBuilder();
  91.         sb.append(this.name + ": ");
  92.  
  93.         methods.entrySet().stream().sorted((a, b) -> Integer.compare(b.getValue().size(), a.getValue().size()))
  94.                 .forEach(method -> {
  95.                     sb.append("\n");
  96.                     sb.append("##" + method.getKey());
  97.                     method.getValue().stream().sorted()
  98.                             .sorted((test1, test2) -> Integer.compare(test1.length(), test2.length())).forEach(test -> {
  99.                                 sb.append("\n");
  100.                                 sb.append("####" + test);
  101.                             });
  102.                     ;
  103.                 });
  104.         ;
  105.         return sb.toString();
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment