Advertisement
mellowdeep

Royal accounting

May 14th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.util.ArrayList;
  3. import java.util.LinkedHashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import java.util.Scanner;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. public class RoyalAccounting {
  12.  
  13.     public static void main(String[] args) {
  14.         try (Scanner scanner = new Scanner(System.in);) {
  15.             Map<Team, List<Employee>> data = new LinkedHashMap<>();
  16.             String inputLine = scanner.nextLine();
  17.             Pattern pat = Pattern
  18.                     .compile("^([A-Za-z]+);(-?[\\d]+);(-?[\\d.?]+);([A-Za-z]+)$");
  19.             while (true) {
  20.                 if (inputLine.equals("Pishi kuf i da si hodim")) {
  21.                     break;
  22.                 }
  23.                 Matcher match = pat.matcher(inputLine);
  24.                 if (match.find()) {
  25.                     String employeeName = match.group(1);
  26.                     long workingHours = Long.parseLong(match.group(2));
  27.                     BigDecimal dailyPayment = new BigDecimal(match.group(3));
  28.                     String teamName = match.group(4);
  29.  
  30.                     boolean checkForAvailability = isNewTeam(teamName, data);
  31.                     Team team = new Team(teamName);
  32.                     if (!checkForAvailability) {
  33.                         data.put(team, new ArrayList<>());
  34.                     }
  35.                     boolean checkForEmployee = checkForAllreadyListedEmployees(data,team,employeeName);
  36.                     if (checkForEmployee) {
  37.                         inputLine = scanner.nextLine();
  38.                         continue;
  39.                     }
  40.                     for (Team t : data.keySet()) {
  41.                         if (t.getName().equals(teamName)) {
  42.                             BigDecimal dailyToDecimal = BigDecimal
  43.                                     .valueOf(workingHours);
  44.                             BigDecimal dayHours = new BigDecimal("24");
  45.                             BigDecimal totalMoney = (dailyPayment
  46.                                     .multiply(dailyToDecimal));
  47.                             totalMoney = totalMoney.divide(dayHours, 6,
  48.                                     BigDecimal.ROUND_HALF_UP);
  49.                             data.get(t).add(
  50.                                     new Employee(employeeName, workingHours,
  51.                                             totalMoney));
  52.                             t.setIncome(t.getIncome().add(
  53.                                     totalMoney.multiply(new BigDecimal("30"))));
  54.                         }
  55.                     }
  56.                 }
  57.                 inputLine = scanner.nextLine();
  58.             }
  59.  
  60.             data.entrySet()
  61.                     .stream()
  62.                     .sorted((team1, team2) -> team2.getKey().getIncome()
  63.                             .compareTo(team1.getKey().getIncome()))
  64.                     .forEach(
  65.                             e -> {
  66.                                 System.out.println("Team - "
  67.                                         + e.getKey().getName());
  68.                                 List<Employee> temp = e.getValue();
  69.                                 temp.sort((s1,s2) -> s1.getName().compareTo(s2.getName()));
  70.                                 temp.sort((s1, s2) -> s2.getDailyIncome().compareTo(s1.getDailyIncome()));
  71.                                 temp.sort((s1, s2) -> Long.compare(
  72.                                         s2.getWorkngHours(),
  73.                                         s1.getWorkngHours()));
  74.                                
  75.                                 for (Employee employee : temp) {
  76.                                     System.out.printf("$$$%s - %d - %s\n",
  77.                                             employee.getName(),
  78.                                             employee.getWorkngHours(),
  79.                                             employee.getDailyIncome());
  80.                                     ;
  81.                                 }
  82.                             });
  83.         } catch (Exception e) {
  84.             System.out.println(e.toString());
  85.         }
  86.     }
  87.  
  88.     private static boolean checkForAllreadyListedEmployees(
  89.             Map<Team, List<Employee>> data, Team team, String employeeName) {
  90.        
  91.         boolean isEmployeeInside = false;
  92.        
  93.         for (Entry<Team, List<Employee>> t : data.entrySet()) {
  94.             List<Employee> temp = t.getValue();
  95.             for (Employee employee : temp) {
  96.                 if (employee.getName().equals(employeeName)) {
  97.                     isEmployeeInside = true;
  98.                 }
  99.             }
  100.         }
  101.         return isEmployeeInside;
  102.     }
  103.  
  104.     private static boolean isNewTeam(String teamName,
  105.             Map<Team, List<Employee>> data) {
  106.         boolean isNew = false;
  107.         for (Team team : data.keySet()) {
  108.             if (team.getName().equals(teamName)) {
  109.                 isNew = true;
  110.             }
  111.         }
  112.         return isNew;
  113.     }
  114.  
  115. }
  116.  
  117. class Employee {
  118.  
  119.     private BigDecimal dailyIncome;
  120.     private String name;
  121.     private long workngHours;
  122.  
  123.     public Employee(String name, long workngHours, BigDecimal dailyIncome) {
  124.         this.name = name;
  125.         this.workngHours = workngHours;
  126.         this.dailyIncome = dailyIncome;
  127.  
  128.     }
  129.  
  130.     public String getName() {
  131.         return name;
  132.     }
  133.  
  134.     public long getWorkngHours() {
  135.         return workngHours;
  136.     }
  137.  
  138.     public BigDecimal getDailyIncome() {
  139.         return dailyIncome;
  140.     }
  141.  
  142.     @Override
  143.     public String toString() {
  144.         return "Employe = " + name;
  145.     }
  146. }
  147.  
  148. class Team implements Comparable<Team> {
  149.  
  150.     private BigDecimal income = BigDecimal.ZERO;
  151.     private String name;
  152.  
  153.     public Team(String name) {
  154.         this.name = name;
  155.     }
  156.  
  157.     public String getName() {
  158.         return name;
  159.     }
  160.  
  161.     public BigDecimal getIncome() {
  162.         return income;
  163.     }
  164.  
  165.     public void setIncome(BigDecimal income) {
  166.         this.income = income;
  167.     }
  168.  
  169.     @Override
  170.     public String toString() {
  171.         return "Team = " + name + " " + income;
  172.     }
  173.  
  174.     @Override
  175.     public int compareTo(Team a) {
  176.  
  177.         return this.income.compareTo(a.income);
  178.     }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement