Untitled
user_6122548
plain_text
a year ago
3.0 kB
4
Indexable
Never
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; public class Exercise_09_ja { public static void main(String[] args) { Scanner sc = new Scanner(System.in); LinkedHashMap<String, String> imeStrana = new LinkedHashMap<>(); LinkedHashMap<String, ArrayList<String>> stranaImena = new LinkedHashMap<>(); while (true) { String unos = sc.nextLine(); if (unos.equals("Lumpawaroo")) break; if(unos.contains(" | ")) { String [] nizOdUnosa = unos.split(" \\| "); String forceSide = nizOdUnosa[0]; String forceUser = nizOdUnosa[1]; if (!imeStrana.containsKey(forceUser) && !stranaImena.containsKey(forceSide)){ imeStrana.put(forceUser, forceSide); stranaImena.put(forceSide, new ArrayList<>()); stranaImena.get(forceSide).add(forceUser); } if (stranaImena.containsKey(forceSide) && !imeStrana.containsKey(forceUser)) { stranaImena.get(forceSide).add(forceUser); imeStrana.put(forceUser, forceSide); } //if (imeStrana.containsKey(forceUser)) continue; } else if (unos.contains(" -> ")) { String [] nizOdUnosa = unos.split(" -> "); String forceSide = nizOdUnosa[1]; String forceUser = nizOdUnosa[0]; System.out.printf("%s joins the %s side!\n", forceUser, forceSide); if (imeStrana.containsKey(forceUser)) { String prethodniSide = imeStrana.get(forceUser); imeStrana.put(forceUser, forceSide); stranaImena.get(prethodniSide).remove(forceUser); stranaImena.get(forceSide).add(forceUser); } if (!imeStrana.containsKey(forceUser) && stranaImena.containsKey(forceSide)){ imeStrana.put(forceUser, forceSide); stranaImena.get(forceSide).add(forceUser); } if (!imeStrana.containsKey(forceUser) && !stranaImena.containsKey(forceSide)) { imeStrana.put(forceUser, forceSide); stranaImena.put(forceSide, new ArrayList<>()); stranaImena.get(forceSide).add(forceUser); } } } for(Map.Entry<String, ArrayList<String>> e : stranaImena.entrySet()) { String strana = e.getKey(); ArrayList<String> imena = e.getValue(); if (!imena.isEmpty()) { System.out.printf("Side: %s, Members: %d\n",strana, imena.size()); for(String s: imena){ System.out.printf("! %s\n", s); } } } } }