Advertisement
vladimirVenkov

Units of Work

Jul 28th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.66 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.*;
  6. import java.util.stream.Collectors;
  7.  
  8. public class UnitsOfWork {
  9.     private static final int NUMBER_OF_UNITS_TO_SELECT_BY_TYPE = 10;
  10.  
  11.     private static void fakeInput() {
  12.         String input = "add TheMightyThor God 100\n" +
  13.                 "add Artanis Protoss 250\n" +
  14.                 "add Fenix Protoss 200\n" +
  15.                 "add Spiderman MutatedHuman 180\n" +
  16.                 "add XelNaga God 500\n" +
  17.                 "add Wolverine MutatedHuman 200\n" +
  18.                 "add Zeratul Protoss 300\n" +
  19.                 "add Spiderman MutatedHuman 180\n" +
  20.                 "power 3\n" +
  21.                 "find Protoss\n" +
  22.                 "find God\n" +
  23.                 "remove Kerrigan\n" +
  24.                 "remove XelNaga\n" +
  25.                 "power 3\n" +
  26.                 "find Kerrigan\n" +
  27.                 "find God\n" +
  28.                 "end";
  29.         System.setIn(new ByteArrayInputStream(input.getBytes()));
  30.  
  31.     }
  32.  
  33.     public static void main(String[] args) throws IOException {
  34.         fakeInput();
  35.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  36.         init();
  37.         run(br);
  38.  
  39.     }
  40.  
  41.     private static Map<String, Unit> unitsByName;
  42.     private static Map<String, Set<Unit>> unitsByType;
  43.     private static Set<Unit> unitsByAttack;
  44.     private static void init() {
  45.         unitsByAttack = new TreeSet<>();
  46.         unitsByName = new HashMap<>();
  47.         unitsByType = new HashMap<>();
  48.     }
  49.     private static void run(BufferedReader br) throws IOException {
  50.     String[] inp = br.readLine().split(" ");
  51.         while (!inp[0].equals("end")) {
  52.             switch (inp[0]) {
  53.                 case "add":
  54.                     String name = inp[1];
  55.                     String type = inp[2];
  56.                     int attack = Integer.parseInt(inp[3]);
  57.                     addUnit(name, type, attack);
  58.                     break;
  59.                 case "remove":
  60.                     String nameOfUnitToDelete = inp[1];
  61.                     removeUnit(nameOfUnitToDelete);
  62.                     break;
  63.                 case "find":
  64.                     String unitTypeToFind = inp[1];
  65.                     findUnitByType(unitTypeToFind);
  66.                     break;
  67.                 case "power":
  68.                     int numberOfUnitsWithMostAttack = Integer.parseInt(inp[1]);
  69.                     printTopUnits(numberOfUnitsWithMostAttack);
  70.                     break;
  71.                 default:
  72.                     break;
  73.             }
  74.             inp = br.readLine().split(" ");
  75.         }
  76.     }
  77.  
  78.     private static void addUnit(String name, String type, int attack) {
  79.         if (unitsByName.containsKey(name)) {
  80.             //System.out.printf("FAIL: %s already exists!%n", name);
  81.             System.out.println("FAIL: " + name + " already exists!");
  82.  
  83.             return;
  84.         }
  85.  
  86.         Unit newUnit = new Unit(name, type, attack);
  87.         unitsByName.put(name, newUnit);
  88.         addToUnitsByType(type, newUnit);
  89.         addToUnitsByAttack(newUnit);
  90.         //System.out.printf("SUCCESS: %s added!%n", name);
  91.         System.out.println("SUCCESS: " + name + "added!");
  92.     }
  93.  
  94.     private static void addToUnitsByAttack(Unit unit) {
  95.         unitsByAttack.add(unit);
  96.     }
  97.  
  98.     private static void addToUnitsByType(String type, Unit unit) {
  99.         if (!unitsByType.containsKey(type)) {
  100.             unitsByType.put(type, new TreeSet<>());
  101.         }
  102.         unitsByType.get(type).add(unit);
  103.     }
  104.  
  105.     private static void removeUnit(String nameOfUnitToDelete) {
  106.         if (!unitsByName.containsKey(nameOfUnitToDelete)) {
  107.             System.out.printf("FAIL: %s could not be found!%n", nameOfUnitToDelete);
  108.             return;
  109.         }
  110.         unitsByAttack.remove(unitsByName.get(nameOfUnitToDelete));
  111.         unitsByType.get(unitsByName.get(nameOfUnitToDelete).getType())
  112.                 .remove(unitsByName.get(nameOfUnitToDelete));
  113.         unitsByName.remove(nameOfUnitToDelete);
  114.         System.out.printf("SUCCESS: %s removed!%n", nameOfUnitToDelete);
  115.     }
  116.  
  117.     private static void printTopUnits(int numberOfUnits) {
  118.         System.out.print("RESULT: ");
  119.         if (unitsByAttack.isEmpty()) {
  120.             System.out.println();
  121.             return;
  122.         }
  123.         List<Unit> selectedUnits = unitsByAttack
  124.                 .stream()
  125.                 .limit(numberOfUnits)
  126.                 .collect(Collectors.toList());
  127.         StringBuilder toPrint = new StringBuilder();
  128.         for (Unit unit : selectedUnits) {
  129.             toPrint.append(unit);
  130.             toPrint.append(", ");
  131.         }
  132.         toPrint.deleteCharAt(toPrint.length() - 1);
  133.         toPrint.deleteCharAt(toPrint.length() - 1);
  134.         System.out.println(toPrint);
  135.     }
  136.  
  137.     private static void findUnitByType(String type) {
  138.         System.out.print("RESULT: ");
  139.         if (!unitsByType.containsKey(type)) {
  140.             System.out.println();
  141.             return;
  142.         }
  143.         List<Unit> selectedUnits = unitsByType.get(type)
  144.                 .stream()
  145.                 .limit(NUMBER_OF_UNITS_TO_SELECT_BY_TYPE)
  146.                 .collect(Collectors.toList());
  147.         StringBuilder toPrint = new StringBuilder();
  148.         for (Unit unit : selectedUnits) {
  149.             toPrint.append(unit);
  150.             toPrint.append(", ");
  151.         }
  152.         toPrint.deleteCharAt(toPrint.length() - 1);
  153.         toPrint.deleteCharAt(toPrint.length() - 1);
  154.         System.out.println(toPrint);
  155.     }
  156.  
  157.  
  158.     private static class Unit implements Comparable<Unit> {
  159.         private String name;
  160.         private String type;
  161.         private int attack;
  162.  
  163.         private Unit(String name, String type, int attack) {
  164.             this.name = name;
  165.             this.type = type;
  166.             this.attack = attack;
  167.         }
  168.  
  169.         @Override
  170.         public int compareTo(Unit other) {
  171.             int resultFromComparision =
  172.                     Integer.compare(other.getAttack(), getAttack());
  173.             if (resultFromComparision == 0) {
  174.                 resultFromComparision =
  175.                         getName().compareTo(other.getName());
  176.             }
  177.             return resultFromComparision;
  178.         }
  179.  
  180.         @Override
  181.         public String toString() {
  182.  
  183.             return String.format("%s[%s](%d)",
  184.                     getName(), getType(), getAttack());
  185.         }
  186.  
  187.         public String getName() {
  188.             return name;
  189.         }
  190.  
  191.         public String getType() {
  192.             return type;
  193.         }
  194.  
  195.         public int getAttack() {
  196.             return attack;
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement