Advertisement
mellowdeep

pizza time

Jun 26th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. package methodsExercises.pr09PizzaTime;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. package methodsExercises.pr09PizzaTime;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.InputStreamReader;
  11. import java.lang.reflect.Method;
  12. import java.util.Arrays;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.stream.Collectors;
  16.  
  17. public class Pr09PizaaTime {
  18.  
  19.     public static void main(String[] args) {
  20.        
  21.         try (BufferedReader reader  = new BufferedReader(new InputStreamReader(System.in));) {
  22.             String[] inputLine = reader.readLine().split("\\s+");
  23.             Pizza pizza = new Pizza();
  24.             pizza.addPizza(inputLine);
  25.             Map<Integer,StringBuilder> printList = pizza.getPizzaList();
  26.             printList
  27.             .entrySet()
  28.             .stream()
  29.             .sorted((g1,g2) -> Integer
  30.                     .compare(g1.getKey(), g2.getKey()))
  31.                     .forEach(group -> {
  32.                 System.out.printf("%d - %s\n",group
  33.                         .getKey(),group
  34.                         .getValue()
  35.                         .substring(0,group.getValue()
  36.                                 .length()-2));
  37.             });
  38.            
  39.             Class<?> pizzaClass = Pizza.class;
  40.             Method[] methods = pizzaClass.getDeclaredMethods();
  41.             List<Method> checkedMethods = Arrays.stream(methods)
  42.                     .filter(m -> m.getReturnType().getName().contains("Map"))
  43.                     .collect(Collectors.toList());
  44.  
  45.             if (checkedMethods.size() < 1) {
  46.                 throw new ClassFormatError();
  47.             }
  48.  
  49.         } catch (Exception e) {
  50.             e.printStackTrace();
  51.         }
  52.     }
  53.  
  54. }
  55.  
  56. class Pizza {
  57.    
  58.     private Map<Integer,StringBuilder> pizzaList;
  59.    
  60.     public Pizza(){
  61.         this.pizzaList = new LinkedHashMap<>();
  62.     }
  63.  
  64.     public void addPizza(String ... pizza){
  65.         String regex = "([0-9]+)(.*)";
  66.         Pattern pattern = Pattern.compile(regex);
  67.        
  68.         for (String currPizza : pizza) {
  69.             Matcher match = pattern.matcher(currPizza);
  70.             while (match.find()) {
  71.                 int group = Integer.parseInt(match.group(1));
  72.                 String pizzaName = match.group(2);
  73.                 if (! pizzaList.containsKey(group)) {
  74.                     pizzaList.put(group,new StringBuilder());
  75.                 }
  76.                     pizzaList.get(group).append(pizzaName + ", ");
  77.             }
  78.         }
  79.     }
  80.  
  81.     public Map<Integer,StringBuilder> getPizzaList() {
  82.         return this.pizzaList;
  83.     }
  84.  
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement