Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.TreeMap;
- import java.util.TreeSet;
- public class GUnit {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String input;
- TreeMap<String, Class> classTests = new TreeMap<>();
- while (true) {
- input = sc.nextLine();
- if (input.equals("It's testing time!")) {
- sc.close();
- break;
- }
- String[] data = input.split("\\s*\\|\\s*");
- String className = data[0];
- String methodName = data[1];
- String testName = data[2];
- boolean isValidInput = allValid(className, methodName, testName);
- if (data.length > 3 || !isValidInput) {
- continue;
- }
- if (!classTests.containsKey(className)) {
- Class clas = new Class(className);
- classTests.put(className, clas);
- clas.tests++;
- }
- Class clas = classTests.get(className);
- if (!clas.methods.containsKey(methodName)) {
- clas.methods.put(methodName, new TreeSet<String>());
- }
- if (!clas.methods.get(methodName).contains(testName)) {
- clas.methods.get(methodName).add(testName);
- clas.tests++;
- }
- }
- classTests.values().stream().sorted().forEach(System.out::println);
- }
- public static boolean allValid(String word1, String word2, String word3) {
- boolean firstWord = validWord(word1);
- boolean secondWord = validWord(word2);
- boolean thirdWord = validWord(word3);
- if (firstWord && secondWord && thirdWord) {
- return true;
- } else {
- return false;
- }
- }
- public static boolean validWord(String word) {
- if (word.length() < 2) {
- return false;
- }
- for (int i = 0; i < word.length(); i++) {
- if (!Character.isUpperCase(word.charAt(0))) {
- return false;
- }
- if (!Character.isLetterOrDigit(word.charAt(i))) {
- return false;
- }
- }
- return true;
- }
- }
- class Class implements Comparable<Class> {
- public String name;
- public int tests;
- public TreeMap<String, TreeSet<String>> methods;
- public Class(String name) {
- this.name = name;
- this.tests = 0;
- this.methods = new TreeMap<String, TreeSet<String>>();
- }
- public int compareTo(Class o) {
- int totalresult = Integer.compare(o.tests, this.tests);
- if (totalresult == 0) {
- totalresult = Integer.compare(this.methods.size(), o.methods.size());
- }
- return totalresult;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(this.name + ": ");
- methods.entrySet().stream().sorted((a, b) -> Integer.compare(b.getValue().size(), a.getValue().size()))
- .forEach(method -> {
- sb.append("\n");
- sb.append("##" + method.getKey());
- method.getValue().stream().sorted()
- .sorted((test1, test2) -> Integer.compare(test1.length(), test2.length())).forEach(test -> {
- sb.append("\n");
- sb.append("####" + test);
- });
- ;
- });
- ;
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment