Advertisement
Guest User

Untitled

a guest
Oct 5th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. package com.exam.fourth.sept;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.text.DecimalFormat;
  6. import java.util.LinkedList;
  7. import java.util.Scanner;
  8.  
  9. public class DragonAccounting {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. Scanner scanner = new Scanner(System.in);
  14. LinkedList<EmployeeSet> employeesList = new LinkedList<>();
  15. int day = 1;
  16.  
  17. BigDecimal capital = new BigDecimal(scanner.nextLine());
  18. //System.out.println(capital/ 30);
  19. String line = scanner.nextLine();
  20.  
  21. while (!line.equals("END")) {
  22. String[] input = line.split(";");
  23.  
  24. long hired = Long.parseLong(input[0]);
  25. long fired = Long.parseLong(input[1]);
  26. BigDecimal salary = new BigDecimal(input[2]);
  27. // hire employees
  28. employeesList.add(new EmployeeSet(hired, salary, day));
  29. //check for raise
  30. for (EmployeeSet employeesSet : employeesList) {
  31. int days = day - employeesSet.dayHired;
  32. if ((days) % 365 == 0 && days != 0 && employeesSet.number != 0) {
  33. employeesSet.updateSalary();
  34. }
  35. }
  36. //give salaries
  37. if (day % 30 == 0) {
  38. for (EmployeeSet employeeSet : employeesList) {
  39. int todalDays = day - employeeSet.dayHired;
  40. if (todalDays > 30) {
  41. todalDays = 30;
  42. }
  43. if (employeeSet.number != 0) {
  44. BigDecimal salaryToDeduct = employeeSet.salary
  45. .divide(new BigDecimal(30), 9, RoundingMode.UP)
  46. .setScale(7, BigDecimal.ROUND_DOWN)
  47. .multiply(BigDecimal.valueOf(todalDays))
  48. .multiply(BigDecimal.valueOf(employeeSet.number));
  49. capital = capital.subtract(salaryToDeduct);
  50. }
  51. }
  52. }
  53. //fire employees
  54. for (EmployeeSet employeeSet : employeesList) {
  55. long employeesLeft = employeeSet.number;
  56. employeeSet.decreaseNumber(fired);
  57. fired -= (employeesLeft - employeeSet.number);
  58. if (fired == 0) {
  59. break;
  60. }
  61. }
  62. //check for additional income/expense ->
  63. for (int i = 3; i < input.length; i++) {
  64. capital = capital.add(extractMoneyFromEvent(input[i]));
  65. }
  66. //check for bankruptcy.
  67. if (capital.compareTo(BigDecimal.ZERO) < 0) {
  68. System.out.printf("BANKRUPTCY: %.4f", capital.negate());
  69. return;
  70. }
  71. day++;
  72. line = scanner.nextLine();
  73. }
  74. int totalEmployees = 0;
  75. for (EmployeeSet employeeSet : employeesList) {
  76. totalEmployees += employeeSet.number;
  77. }
  78. System.out.printf("%d %s", totalEmployees, capital.setScale(4, RoundingMode.DOWN));
  79. }
  80.  
  81. public static class EmployeeSet {
  82. private long number;
  83. private BigDecimal salary;
  84. private int dayHired;
  85.  
  86. public EmployeeSet(long number, BigDecimal salary, int dayHired) {
  87. this.number = number;
  88. this.salary = salary;
  89. this.dayHired = dayHired;
  90. }
  91.  
  92. public void decreaseNumber(long numberToFire) {
  93. if (this.number - numberToFire <= 0) {
  94. this.number = 0;
  95. } else {
  96. this.number -= numberToFire;
  97. }
  98. }
  99.  
  100. public void updateSalary() {
  101. this.salary = this.salary.multiply(new BigDecimal(1.006));
  102. }
  103. }
  104.  
  105. private static BigDecimal extractMoneyFromEvent(String input) {
  106. BigDecimal money = BigDecimal.ZERO;
  107. String[] splitted = input.split(":");
  108. switch (splitted[0]) {
  109. case "Previous years deficit":
  110. case "Machines":
  111. case "Taxes":
  112. money = new BigDecimal(splitted[1]).negate();
  113. break;
  114. case "Product development":
  115. case "Unconditional funding":
  116. money = new BigDecimal(splitted[1]);
  117. break;
  118. default:
  119. break;
  120. }
  121. return money;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement