Advertisement
RonWeber

Untitled

Apr 15th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Scanner;
  6. import java.util.regex.Pattern;
  7. import java.util.stream.Stream;
  8.  
  9. /**
  10.  * The main class for OfficeParty.
  11.  * @author ronnie
  12.  *
  13.  */
  14. public class Main
  15. {
  16.     static final Pattern officeGoer = Pattern.compile("^(\\w+) (\\d+(?:.\\d+)?)(( (\\w+))*)$");
  17.  
  18.     public static void main(String[] args) throws FileNotFoundException
  19.     {
  20.         Scanner inputScanner = new Scanner(new File("office.dat"));
  21.         int numOfficesRemaining = Integer.parseInt(inputScanner.nextLine()); //The first line is how many offices there are.
  22.         while (numOfficesRemaining > 0) { //For each office
  23.             int peopleInOffice = Integer.parseInt(inputScanner.nextLine()); //The next line is how many people are in this office.
  24.             ArrayList<String> workerLines = new ArrayList<>(peopleInOffice);
  25.            
  26.             for (int i = 0; i < peopleInOffice; i++) { //Make a list of the lines in this office.
  27.                 workerLines.add(inputScanner.nextLine());
  28.             }
  29.            
  30.             workerLines.stream().map(s -> officeGoer.matcher(s)).map(m -> {m.find(); return m;}) //Process the lines.
  31.             .map(m -> new OfficeWorker(m.group(1), Double.parseDouble(m.group(2)), m.group(3).split(" ")))
  32.             .collect(
  33.                     HashMap<String, Double>::new,
  34.                     (m, w) -> {
  35.                         m.put(w.us, m.getOrDefault(w.us, 0.) - w.money);
  36.                         double count = w.others.length - 1; //The results of splitting on spaces is an array one larger than the number of people we're giving money to.
  37.                         Stream.of(w.others).filter(s -> !s.isEmpty()).map(String::trim).
  38.                         forEach(s -> m.put(s, m.getOrDefault(s, 0.) + w.money/count));
  39.                         },
  40.                     (m1, m2) -> {throw new UnsupportedOperationException();})
  41.             .entrySet().stream().map(e -> String.format("%s %.2f", e.getKey(), e.getValue())).sorted().
  42.             forEachOrdered(s -> System.out.println(s));
  43.            
  44.             System.out.println(); //Blank line after an office.
  45.             numOfficesRemaining--; //We handled an office.
  46.         }
  47.         inputScanner.close();
  48.     }
  49.    
  50.     private static class OfficeWorker {
  51.         public String us;
  52.         public double money;
  53.         public String[] others;
  54.        
  55.         private OfficeWorker(String us, double money, String[] others)
  56.         {
  57.             super();
  58.             this.us = us;
  59.             this.money = money;
  60.             this.others = others;
  61.         }
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement