Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.exam.fourth.sept;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.DecimalFormat;
- import java.util.LinkedList;
- import java.util.Scanner;
- public class DragonAccounting {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- LinkedList<EmployeeSet> employeesList = new LinkedList<>();
- int day = 1;
- BigDecimal capital = new BigDecimal(scanner.nextLine());
- //System.out.println(capital/ 30);
- String line = scanner.nextLine();
- while (!line.equals("END")) {
- String[] input = line.split(";");
- long hired = Long.parseLong(input[0]);
- long fired = Long.parseLong(input[1]);
- BigDecimal salary = new BigDecimal(input[2]);
- // hire employees
- employeesList.add(new EmployeeSet(hired, salary, day));
- //check for raise
- for (EmployeeSet employeesSet : employeesList) {
- int days = day - employeesSet.dayHired;
- if ((days) % 365 == 0 && days != 0 && employeesSet.number != 0) {
- employeesSet.updateSalary();
- }
- }
- //give salaries
- if (day % 30 == 0) {
- for (EmployeeSet employeeSet : employeesList) {
- int todalDays = day - employeeSet.dayHired;
- if (todalDays > 30) {
- todalDays = 30;
- }
- if (employeeSet.number != 0) {
- BigDecimal salaryToDeduct = employeeSet.salary
- .divide(new BigDecimal(30), 9, RoundingMode.UP)
- .setScale(7, BigDecimal.ROUND_DOWN)
- .multiply(BigDecimal.valueOf(todalDays))
- .multiply(BigDecimal.valueOf(employeeSet.number));
- capital = capital.subtract(salaryToDeduct);
- }
- }
- }
- //fire employees
- for (EmployeeSet employeeSet : employeesList) {
- long employeesLeft = employeeSet.number;
- employeeSet.decreaseNumber(fired);
- fired -= (employeesLeft - employeeSet.number);
- if (fired == 0) {
- break;
- }
- }
- //check for additional income/expense ->
- for (int i = 3; i < input.length; i++) {
- capital = capital.add(extractMoneyFromEvent(input[i]));
- }
- //check for bankruptcy.
- if (capital.compareTo(BigDecimal.ZERO) < 0) {
- System.out.printf("BANKRUPTCY: %.4f", capital.negate());
- return;
- }
- day++;
- line = scanner.nextLine();
- }
- int totalEmployees = 0;
- for (EmployeeSet employeeSet : employeesList) {
- totalEmployees += employeeSet.number;
- }
- System.out.printf("%d %s", totalEmployees, capital.setScale(4, RoundingMode.DOWN));
- }
- public static class EmployeeSet {
- private long number;
- private BigDecimal salary;
- private int dayHired;
- public EmployeeSet(long number, BigDecimal salary, int dayHired) {
- this.number = number;
- this.salary = salary;
- this.dayHired = dayHired;
- }
- public void decreaseNumber(long numberToFire) {
- if (this.number - numberToFire <= 0) {
- this.number = 0;
- } else {
- this.number -= numberToFire;
- }
- }
- public void updateSalary() {
- this.salary = this.salary.multiply(new BigDecimal(1.006));
- }
- }
- private static BigDecimal extractMoneyFromEvent(String input) {
- BigDecimal money = BigDecimal.ZERO;
- String[] splitted = input.split(":");
- switch (splitted[0]) {
- case "Previous years deficit":
- case "Machines":
- case "Taxes":
- money = new BigDecimal(splitted[1]).negate();
- break;
- case "Product development":
- case "Unconditional funding":
- money = new BigDecimal(splitted[1]);
- break;
- default:
- break;
- }
- return money;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement