Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- import java.util.stream.Collectors;
- public class UnitsOfWork {
- private static final int NUMBER_OF_UNITS_TO_SELECT_BY_TYPE = 10;
- private static void fakeInput() {
- String input = "add TheMightyThor God 100\n" +
- "add Artanis Protoss 250\n" +
- "add Fenix Protoss 200\n" +
- "add Spiderman MutatedHuman 180\n" +
- "add XelNaga God 500\n" +
- "add Wolverine MutatedHuman 200\n" +
- "add Zeratul Protoss 300\n" +
- "add Spiderman MutatedHuman 180\n" +
- "power 3\n" +
- "find Protoss\n" +
- "find God\n" +
- "remove Kerrigan\n" +
- "remove XelNaga\n" +
- "power 3\n" +
- "find Kerrigan\n" +
- "find God\n" +
- "end";
- System.setIn(new ByteArrayInputStream(input.getBytes()));
- }
- public static void main(String[] args) throws IOException {
- fakeInput();
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- init();
- run(br);
- }
- private static Map<String, Unit> unitsByName;
- private static Map<String, Set<Unit>> unitsByType;
- private static Set<Unit> unitsByAttack;
- private static void init() {
- unitsByAttack = new TreeSet<>();
- unitsByName = new HashMap<>();
- unitsByType = new HashMap<>();
- }
- private static void run(BufferedReader br) throws IOException {
- String[] inp = br.readLine().split(" ");
- while (!inp[0].equals("end")) {
- switch (inp[0]) {
- case "add":
- String name = inp[1];
- String type = inp[2];
- int attack = Integer.parseInt(inp[3]);
- addUnit(name, type, attack);
- break;
- case "remove":
- String nameOfUnitToDelete = inp[1];
- removeUnit(nameOfUnitToDelete);
- break;
- case "find":
- String unitTypeToFind = inp[1];
- findUnitByType(unitTypeToFind);
- break;
- case "power":
- int numberOfUnitsWithMostAttack = Integer.parseInt(inp[1]);
- printTopUnits(numberOfUnitsWithMostAttack);
- break;
- default:
- break;
- }
- inp = br.readLine().split(" ");
- }
- }
- private static void addUnit(String name, String type, int attack) {
- if (unitsByName.containsKey(name)) {
- //System.out.printf("FAIL: %s already exists!%n", name);
- System.out.println("FAIL: " + name + " already exists!");
- return;
- }
- Unit newUnit = new Unit(name, type, attack);
- unitsByName.put(name, newUnit);
- addToUnitsByType(type, newUnit);
- addToUnitsByAttack(newUnit);
- //System.out.printf("SUCCESS: %s added!%n", name);
- System.out.println("SUCCESS: " + name + "added!");
- }
- private static void addToUnitsByAttack(Unit unit) {
- unitsByAttack.add(unit);
- }
- private static void addToUnitsByType(String type, Unit unit) {
- if (!unitsByType.containsKey(type)) {
- unitsByType.put(type, new TreeSet<>());
- }
- unitsByType.get(type).add(unit);
- }
- private static void removeUnit(String nameOfUnitToDelete) {
- if (!unitsByName.containsKey(nameOfUnitToDelete)) {
- System.out.printf("FAIL: %s could not be found!%n", nameOfUnitToDelete);
- return;
- }
- unitsByAttack.remove(unitsByName.get(nameOfUnitToDelete));
- unitsByType.get(unitsByName.get(nameOfUnitToDelete).getType())
- .remove(unitsByName.get(nameOfUnitToDelete));
- unitsByName.remove(nameOfUnitToDelete);
- System.out.printf("SUCCESS: %s removed!%n", nameOfUnitToDelete);
- }
- private static void printTopUnits(int numberOfUnits) {
- System.out.print("RESULT: ");
- if (unitsByAttack.isEmpty()) {
- System.out.println();
- return;
- }
- List<Unit> selectedUnits = unitsByAttack
- .stream()
- .limit(numberOfUnits)
- .collect(Collectors.toList());
- StringBuilder toPrint = new StringBuilder();
- for (Unit unit : selectedUnits) {
- toPrint.append(unit);
- toPrint.append(", ");
- }
- toPrint.deleteCharAt(toPrint.length() - 1);
- toPrint.deleteCharAt(toPrint.length() - 1);
- System.out.println(toPrint);
- }
- private static void findUnitByType(String type) {
- System.out.print("RESULT: ");
- if (!unitsByType.containsKey(type)) {
- System.out.println();
- return;
- }
- List<Unit> selectedUnits = unitsByType.get(type)
- .stream()
- .limit(NUMBER_OF_UNITS_TO_SELECT_BY_TYPE)
- .collect(Collectors.toList());
- StringBuilder toPrint = new StringBuilder();
- for (Unit unit : selectedUnits) {
- toPrint.append(unit);
- toPrint.append(", ");
- }
- toPrint.deleteCharAt(toPrint.length() - 1);
- toPrint.deleteCharAt(toPrint.length() - 1);
- System.out.println(toPrint);
- }
- private static class Unit implements Comparable<Unit> {
- private String name;
- private String type;
- private int attack;
- private Unit(String name, String type, int attack) {
- this.name = name;
- this.type = type;
- this.attack = attack;
- }
- @Override
- public int compareTo(Unit other) {
- int resultFromComparision =
- Integer.compare(other.getAttack(), getAttack());
- if (resultFromComparision == 0) {
- resultFromComparision =
- getName().compareTo(other.getName());
- }
- return resultFromComparision;
- }
- @Override
- public String toString() {
- return String.format("%s[%s](%d)",
- getName(), getType(), getAttack());
- }
- public String getName() {
- return name;
- }
- public String getType() {
- return type;
- }
- public int getAttack() {
- return attack;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement