Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class RoyalAccounting {
- public static void main(String[] args) {
- try (Scanner scanner = new Scanner(System.in);) {
- Map<Team, List<Employee>> data = new LinkedHashMap<>();
- String inputLine = scanner.nextLine();
- Pattern pat = Pattern
- .compile("^([A-Za-z]+);(-?[\\d]+);(-?[\\d.?]+);([A-Za-z]+)$");
- while (true) {
- if (inputLine.equals("Pishi kuf i da si hodim")) {
- break;
- }
- Matcher match = pat.matcher(inputLine);
- if (match.find()) {
- String employeeName = match.group(1);
- long workingHours = Long.parseLong(match.group(2));
- BigDecimal dailyPayment = new BigDecimal(match.group(3));
- String teamName = match.group(4);
- boolean checkForAvailability = isNewTeam(teamName, data);
- Team team = new Team(teamName);
- if (!checkForAvailability) {
- data.put(team, new ArrayList<>());
- }
- boolean checkForEmployee = checkForAllreadyListedEmployees(data,team,employeeName);
- if (checkForEmployee) {
- inputLine = scanner.nextLine();
- continue;
- }
- for (Team t : data.keySet()) {
- if (t.getName().equals(teamName)) {
- BigDecimal dailyToDecimal = BigDecimal
- .valueOf(workingHours);
- BigDecimal dayHours = new BigDecimal("24");
- BigDecimal totalMoney = (dailyPayment
- .multiply(dailyToDecimal));
- totalMoney = totalMoney.divide(dayHours, 6,
- BigDecimal.ROUND_HALF_UP);
- data.get(t).add(
- new Employee(employeeName, workingHours,
- totalMoney));
- t.setIncome(t.getIncome().add(
- totalMoney.multiply(new BigDecimal("30"))));
- }
- }
- }
- inputLine = scanner.nextLine();
- }
- data.entrySet()
- .stream()
- .sorted((team1, team2) -> team2.getKey().getIncome()
- .compareTo(team1.getKey().getIncome()))
- .forEach(
- e -> {
- System.out.println("Team - "
- + e.getKey().getName());
- List<Employee> temp = e.getValue();
- temp.sort((s1,s2) -> s1.getName().compareTo(s2.getName()));
- temp.sort((s1, s2) -> s2.getDailyIncome().compareTo(s1.getDailyIncome()));
- temp.sort((s1, s2) -> Long.compare(
- s2.getWorkngHours(),
- s1.getWorkngHours()));
- for (Employee employee : temp) {
- System.out.printf("$$$%s - %d - %s\n",
- employee.getName(),
- employee.getWorkngHours(),
- employee.getDailyIncome());
- ;
- }
- });
- } catch (Exception e) {
- System.out.println(e.toString());
- }
- }
- private static boolean checkForAllreadyListedEmployees(
- Map<Team, List<Employee>> data, Team team, String employeeName) {
- boolean isEmployeeInside = false;
- for (Entry<Team, List<Employee>> t : data.entrySet()) {
- List<Employee> temp = t.getValue();
- for (Employee employee : temp) {
- if (employee.getName().equals(employeeName)) {
- isEmployeeInside = true;
- }
- }
- }
- return isEmployeeInside;
- }
- private static boolean isNewTeam(String teamName,
- Map<Team, List<Employee>> data) {
- boolean isNew = false;
- for (Team team : data.keySet()) {
- if (team.getName().equals(teamName)) {
- isNew = true;
- }
- }
- return isNew;
- }
- }
- class Employee {
- private BigDecimal dailyIncome;
- private String name;
- private long workngHours;
- public Employee(String name, long workngHours, BigDecimal dailyIncome) {
- this.name = name;
- this.workngHours = workngHours;
- this.dailyIncome = dailyIncome;
- }
- public String getName() {
- return name;
- }
- public long getWorkngHours() {
- return workngHours;
- }
- public BigDecimal getDailyIncome() {
- return dailyIncome;
- }
- @Override
- public String toString() {
- return "Employe = " + name;
- }
- }
- class Team implements Comparable<Team> {
- private BigDecimal income = BigDecimal.ZERO;
- private String name;
- public Team(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public BigDecimal getIncome() {
- return income;
- }
- public void setIncome(BigDecimal income) {
- this.income = income;
- }
- @Override
- public String toString() {
- return "Team = " + name + " " + income;
- }
- @Override
- public int compareTo(Team a) {
- return this.income.compareTo(a.income);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement