diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..24a65fe --- /dev/null +++ b/pom.xml @@ -0,0 +1,22 @@ + + + 4.0.0 + + org.example + StuffDemo + 1.0-SNAPSHOT + + + + com.googlecode.json-simple + json-simple + 1.1.1 + + + + 1.7 + 1.7 + + diff --git a/projects.json b/projects.json new file mode 100644 index 0000000..9b0a047 --- /dev/null +++ b/projects.json @@ -0,0 +1,16 @@ +{ + "projects": [ + { + "name":"FirstProject", + "budget":5000000 + }, + { + "name":"SecondProject", + "budget":1000000 + }, + { + "name":"ThirdProject", + "budget":7000000 + } + ] +} diff --git a/src/main/java/Cleaner.java b/src/main/java/Cleaner.java new file mode 100644 index 0000000..5c48f77 --- /dev/null +++ b/src/main/java/Cleaner.java @@ -0,0 +1,5 @@ +public class Cleaner extends Personal{ + public Cleaner(int id, String name, int rate){ + super(id, name, rate); + } +} diff --git a/src/main/java/Driver.java b/src/main/java/Driver.java new file mode 100644 index 0000000..fd554b6 --- /dev/null +++ b/src/main/java/Driver.java @@ -0,0 +1,20 @@ +public class Driver extends Personal{ + protected boolean getBonus = false; + final protected int bonus = 3000; + public Driver(int id, String name, int rate){ + super(id, name, rate); + if (rate > 180){ + getBonus = true; + } + } + + @Override + public int calcPayment() { + if (getBonus) { + return super.calcPayment() + bonus; + } + else{ + return super.calcPayment(); + } + } +} diff --git a/src/main/java/Employee.java b/src/main/java/Employee.java new file mode 100644 index 0000000..ffe1552 --- /dev/null +++ b/src/main/java/Employee.java @@ -0,0 +1,43 @@ +public abstract class Employee { + protected int id; + protected String name; + protected int worktime; + protected int payment; + protected String position; + + public Employee (int id, String name){ + this.id = id; + this.name = name; + this.position = this.getClass().toString().split(" ")[1]; + } + + public int getId() { + return id; + } + + public int getPayment() { + return payment; + } + + public int getWorktime() { + return worktime; + } + + public String getName() { + return name; + } + + public String getPosition() { + return position; + } + + public void setPayment(){ + this.payment = calcPayment(); + } + + public void setWorktime(int worktime) { + this.worktime = worktime; + } + + abstract protected int calcPayment(); +} diff --git a/src/main/java/Engineer.java b/src/main/java/Engineer.java new file mode 100644 index 0000000..f997379 --- /dev/null +++ b/src/main/java/Engineer.java @@ -0,0 +1,25 @@ +public abstract class Engineer extends Employee implements WorkTime, Project{ + protected double part; + protected int projectBudget; + protected int rate; + + public Engineer(int id, String name, int rate, int projectBudget, double part){ + super(id, name); + this.rate = rate; + this.projectBudget = projectBudget; + this.part = part; + } + + public int calcPaymentByPart(){ + return ((int)(projectBudget * part)); + } + + public int calcPaymentByRate(){ + return worktime * rate; + } + + @Override + public int calcPayment() { + return calcPaymentByRate() + calcPaymentByPart(); + } +} diff --git a/src/main/java/Heading.java b/src/main/java/Heading.java new file mode 100644 index 0000000..cbe3cb7 --- /dev/null +++ b/src/main/java/Heading.java @@ -0,0 +1,4 @@ +public interface Heading { + final int PAYMENT= 2000; + int calcPaymentBySubs(); +} diff --git a/src/main/java/Manager.java b/src/main/java/Manager.java new file mode 100644 index 0000000..ee2984a --- /dev/null +++ b/src/main/java/Manager.java @@ -0,0 +1,14 @@ +public abstract class Manager extends Employee implements Project{ + protected double part; + protected int projectBudget; + + public Manager(int id, String name, int projectBudget, double part){ + super(id, name); + this.projectBudget = projectBudget; + this.part = part; + } + + public int calcPaymentByPart(){ + return ((int)(projectBudget * part)); + } +} diff --git a/src/main/java/Personal.java b/src/main/java/Personal.java new file mode 100644 index 0000000..0066e16 --- /dev/null +++ b/src/main/java/Personal.java @@ -0,0 +1,16 @@ +public abstract class Personal extends Employee implements WorkTime{ + protected int rate; + + public Personal(int id, String name, int rate){ + super(id, name); + this.rate = rate; + } + public int calcPaymentByRate(){ + return worktime * rate; + } + + @Override + public int calcPayment(){ + return calcPaymentByRate(); + } +} diff --git a/src/main/java/Programmer.java b/src/main/java/Programmer.java new file mode 100644 index 0000000..8f73dd3 --- /dev/null +++ b/src/main/java/Programmer.java @@ -0,0 +1,5 @@ +public class Programmer extends Engineer{ + public Programmer(int id, String name, int rate, int projectBudget, double part){ + super(id, name, rate, projectBudget, part); + } +} diff --git a/src/main/java/Project.java b/src/main/java/Project.java new file mode 100644 index 0000000..3be5718 --- /dev/null +++ b/src/main/java/Project.java @@ -0,0 +1,3 @@ +public interface Project { + int calcPaymentByPart(); +} diff --git a/src/main/java/ProjectManager.java b/src/main/java/ProjectManager.java new file mode 100644 index 0000000..83f1c75 --- /dev/null +++ b/src/main/java/ProjectManager.java @@ -0,0 +1,17 @@ +public class ProjectManager extends Manager implements Heading{ + protected int amountOfSubs; + + public ProjectManager (int id, String name, int projectBudget, double part, int amountOfSubs){ + super(id, name, projectBudget, part); + this.amountOfSubs = amountOfSubs; + } + + public int calcPaymentBySubs(){ + return amountOfSubs * PAYMENT; + } + + @Override + public int calcPayment() { + return calcPaymentBySubs() + calcPaymentByPart(); + } +} diff --git a/src/main/java/Projects.java b/src/main/java/Projects.java new file mode 100644 index 0000000..028c3c6 --- /dev/null +++ b/src/main/java/Projects.java @@ -0,0 +1,17 @@ +public class Projects { + private int budget; + private String name; + + public Projects (String name, int budget){ + this.name = name; + this.budget = budget; + } + + public int getBudget() { + return budget; + } + + public String getName(){ + return name; + } +} diff --git a/src/main/java/SeniorManager.java b/src/main/java/SeniorManager.java new file mode 100644 index 0000000..20f16bb --- /dev/null +++ b/src/main/java/SeniorManager.java @@ -0,0 +1,5 @@ +public class SeniorManager extends ProjectManager{ + public SeniorManager (int id, String name, int projectBudget, double part, int amountOfSubs){ + super(id, name, projectBudget, part, amountOfSubs); + } +} diff --git a/src/main/java/Staff.java b/src/main/java/Staff.java new file mode 100644 index 0000000..4e44bf0 --- /dev/null +++ b/src/main/java/Staff.java @@ -0,0 +1,156 @@ +import java.util.ArrayList; +import java.io.FileReader; +import java.util.Iterator; +import java.io.IOException; +import org.json.simple.parser.JSONParser; +import org.json.simple.JSONObject; +import org.json.simple.parser.ParseException; +import org.json.simple.JSONArray; + +public class Staff { + private ArrayList projects; + private ArrayList employees; + + public Staff (String fileStaff, String fileProjects){ + createProjects(fileProjects); + createEmployees(fileStaff); + } + + public Projects findProjectByName(String projectName){ + for (Projects project: projects){ + if (project.getName().equals(projectName)){ + return project; + } + } + System.out.println("Wrong project name. Check your data!"); + return null; + } + + public int calcAllProjectsBudget(){ + int result = 0; + for (Projects project: projects){ + result += project.getBudget(); + } + if (0 >= result){ + System.out.println("Error! Project budgets are uncorrect."); + } + return result; + } + public void createEmployees(String fileStaff){ + employees = new ArrayList(); + JSONParser parser = new JSONParser(); + + try (FileReader reader = new FileReader(fileStaff)){ + JSONObject object = (JSONObject)parser.parse(reader); + JSONArray employeesArr = (JSONArray) object.get("employees"); + Iterator employeesIterator = employeesArr.iterator(); + + while(employeesIterator.hasNext()) { + JSONObject emp = (JSONObject) employeesIterator.next(); + int id = ((Long) emp.get("id")).intValue(); + String fio = (String) emp.get("fio"); + String position = (String) emp.get("position"); + int rate = 0; + String projectName = null; + Double part = 0.0; + int projectBudget = 0; + int subs = 0; + Employee employee = null; + switch (position){ + case "Programmer": + rate = ((Long) emp.get("rate")).intValue(); + projectName = (String) emp.get("project"); + part = (Double) emp.get("part"); + projectBudget = findProjectByName(projectName).getBudget(); + employee = new Programmer(id, fio, rate, projectBudget, part); + break; + + case "Tester": + rate = ((Long) emp.get("rate")).intValue(); + projectName = (String) emp.get("project"); + part = (Double) emp.get("part"); + projectBudget = findProjectByName(projectName).getBudget(); + employee = new Tester(id, fio, rate, projectBudget, part); + break; + + case "Cleaner": + rate = ((Long) emp.get("rate")).intValue(); + employee = new Cleaner(id, fio, rate); + break; + + case "Driver": + rate = ((Long) emp.get("rate")).intValue(); + employee = new Driver(id, fio, rate); + break; + + case "TeamLeader": + rate = ((Long) emp.get("rate")).intValue(); + projectName = (String) emp.get("project"); + part = (Double) emp.get("part"); + projectBudget = findProjectByName(projectName).getBudget(); + subs = ((Long) emp.get("subs")).intValue(); + employee = new TeamLeader(id, fio, rate, projectBudget, part, subs); + break; + + case "ProjectManager": + projectName = (String) emp.get("project"); + part = (Double) emp.get("part"); + projectBudget = findProjectByName(projectName).getBudget(); + subs = ((Long) emp.get("subs")).intValue(); + employee = new ProjectManager(id, fio, projectBudget, part, subs); + break; + + case "SeniorManager": + projectName = (String) emp.get("project"); + part = (Double) emp.get("part"); + projectBudget = calcAllProjectsBudget(); + subs = ((Long) emp.get("subs")).intValue(); + employee = new SeniorManager(id, fio, projectBudget, part, subs); + break; + + default: + System.out.println("Error! Wrong position in file with employees."); + return; + } + employees.add(employee); + } + } + catch (ParseException | IOException e){ + e.printStackTrace(); + } + } + + public void createProjects(String fileProjects){ + projects = new ArrayList(); + JSONParser parser = new JSONParser(); + try (FileReader reader = new FileReader(fileProjects)){ + JSONObject object = (JSONObject)parser.parse(reader); + JSONArray projectsArr = (JSONArray) object.get("projects"); + Iterator projectsIterator = projectsArr.iterator(); + String name = null; + int budget = 0; + while(projectsIterator.hasNext()) { + JSONObject pr = (JSONObject) projectsIterator.next(); + name = (String) pr.get("name"); + budget = ((Long) pr.get("budget")).intValue(); + Projects project = new Projects (name, budget); + projects.add(project); + } + } + catch (ParseException | IOException e){ + e.printStackTrace(); + } + } + public void calcPaymentForAllStaff(int worktime){ + for (Employee emp: employees){ + emp.setWorktime(worktime); + emp.setPayment(); + } + } + public void printData(){ + for (Employee emp: employees){ + System.out.println(emp.getId() + " " + emp.getName() + " " + emp.getPosition() + " отработал " + + emp.getWorktime() + " часов. Зарплата " + emp.getPayment()); + } + } +} diff --git a/src/main/java/TeamLeader.java b/src/main/java/TeamLeader.java new file mode 100644 index 0000000..9566022 --- /dev/null +++ b/src/main/java/TeamLeader.java @@ -0,0 +1,17 @@ +public class TeamLeader extends Programmer implements Heading{ + protected int amountOfSubs; + + public TeamLeader(int id, String name, int rate, int projectBudget, double part, int amountOfSubs){ + super(id, name, rate, projectBudget, part); + this.amountOfSubs = amountOfSubs; + } + + public int calcPaymentBySubs(){ + return amountOfSubs * PAYMENT; + } + + @Override + public int calcPayment(){ + return calcPaymentBySubs() + super.calcPayment(); + } +} diff --git a/src/main/java/Tester.java b/src/main/java/Tester.java new file mode 100644 index 0000000..ac6445d --- /dev/null +++ b/src/main/java/Tester.java @@ -0,0 +1,5 @@ +public class Tester extends Engineer{ + public Tester(int id, String name, int rate, int projectBudget, double part){ + super(id, name, rate, projectBudget, part); + } +} diff --git a/src/main/java/WorkTime.java b/src/main/java/WorkTime.java new file mode 100644 index 0000000..b68e3d9 --- /dev/null +++ b/src/main/java/WorkTime.java @@ -0,0 +1,3 @@ +public interface WorkTime { + int calcPaymentByRate(); +} diff --git a/src/main/java/main.java b/src/main/java/main.java new file mode 100644 index 0000000..244a818 --- /dev/null +++ b/src/main/java/main.java @@ -0,0 +1,7 @@ +public class main { + public static void main(String[] args) { + Staff myStaff = new Staff("staff.json", "projects.json"); + myStaff.calcPaymentForAllStaff(168); + myStaff.printData(); + } +} diff --git a/src/test/java/DriverTest.java b/src/test/java/DriverTest.java new file mode 100644 index 0000000..ce2a13d --- /dev/null +++ b/src/test/java/DriverTest.java @@ -0,0 +1,22 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class DriverTest { + + @Test + public void calcPayment() { + Driver myDriver = new Driver(234, "Мария", 15); + myDriver.setWorktime(100); + myDriver.calcPayment(); + assertEquals(1500, myDriver.calcPayment()); + } + + @Test + public void calcPaymentWithBonus() { + Driver myDriver = new Driver(234, "Мария", 200); + myDriver.setWorktime(100); + myDriver.calcPayment(); + assertEquals(23000, myDriver.calcPayment()); + } +} diff --git a/src/test/java/EngineerTest.java b/src/test/java/EngineerTest.java new file mode 100644 index 0000000..8b90652 --- /dev/null +++ b/src/test/java/EngineerTest.java @@ -0,0 +1,26 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class EngineerTest { + + @Test + public void calcPaymentByPart() { + Engineer myEngineer = new Programmer(234, "Юрий", 15, 1000, 0.5); + assertEquals(500, myEngineer.calcPaymentByPart()); + } + + @Test + public void calcPaymentByRate() { + Engineer myEngineer = new Programmer(234, "Юрий", 15, 1000, 0.5); + myEngineer.setWorktime(100); + assertEquals(1500, myEngineer.calcPaymentByRate()); + } + + @Test + public void calcPayment() { + Engineer myEngineer = new Programmer(234, "Юрий", 15, 1000, 0.5); + myEngineer.setWorktime(100); + assertEquals(2000, myEngineer.calcPayment()); + } +} diff --git a/src/test/java/ManagerTest.java b/src/test/java/ManagerTest.java new file mode 100644 index 0000000..0be25fd --- /dev/null +++ b/src/test/java/ManagerTest.java @@ -0,0 +1,12 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ManagerTest { + + @Test + public void calcPaymentByPart() { + Manager myManager = new ProjectManager(234, "Юрий", 1000, 0.5, 2); + assertEquals(500, myManager.calcPaymentByPart()); + } +} diff --git a/src/test/java/PersonalTest.java b/src/test/java/PersonalTest.java new file mode 100644 index 0000000..1b25842 --- /dev/null +++ b/src/test/java/PersonalTest.java @@ -0,0 +1,20 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PersonalTest { + + @Test + public void calcPaymentByRate() { + Personal myPersonal = new Cleaner(234, "Юрий", 15); + myPersonal.setWorktime(100); + assertEquals(1500, myPersonal.calcPaymentByRate()); + } + + @Test + public void calcPayment() { + Personal myPersonal = new Cleaner(234, "Юрий", 15); + myPersonal.setWorktime(100); + assertEquals(1500, myPersonal.calcPayment()); + } +} diff --git a/src/test/java/ProjectManagerTest.java b/src/test/java/ProjectManagerTest.java new file mode 100644 index 0000000..8a10ea1 --- /dev/null +++ b/src/test/java/ProjectManagerTest.java @@ -0,0 +1,20 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ProjectManagerTest { + + @Test + public void calcPaymentBySubs() { + ProjectManager myProjectManager = new ProjectManager(234, "Юрий", 1000, + 0.5, 2); + assertEquals(4000, myProjectManager.calcPaymentBySubs()); + } + + @Test + public void calcPayment() { + ProjectManager myProjectManager = new ProjectManager(234, "Юрий", 1000, + 0.5, 2); + assertEquals(4500, myProjectManager.calcPayment()); + } +} diff --git a/src/test/java/TeamLeaderTest.java b/src/test/java/TeamLeaderTest.java new file mode 100644 index 0000000..a7d54d3 --- /dev/null +++ b/src/test/java/TeamLeaderTest.java @@ -0,0 +1,21 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TeamLeaderTest { + + @Test + public void calcPaymentBySubs() { + TeamLeader myTeamLeader = new TeamLeader(234, "Вася", 100, + 1000, 0.5, 2); + assertEquals(4000, myTeamLeader.calcPaymentBySubs()); + } + + @Test + public void calcPayment() { + TeamLeader myTeamLeader = new TeamLeader(234, "Вася", 50, + 1000, 0.5, 2); + myTeamLeader.setWorktime(100); + assertEquals(9500, myTeamLeader.calcPayment()); + } +} diff --git a/staff.json b/staff.json new file mode 100644 index 0000000..c7abc63 --- /dev/null +++ b/staff.json @@ -0,0 +1,274 @@ +{ + "employees": [ + { + "id":101, + "fio":"Пупкин А.А.", + "position":"Cleaner", + "rate":100, + "project":"", + "part":null, + "subs":null + }, + { + "id":102, + "fio":"Иванов Б.Б.", + "position":"Driver", + "rate":150, + "project":"", + "part":null, + "subs":null + }, + { + "id":103, + "fio":"Петров В.В.", + "position":"Driver", + "rate":180, + "project":"", + "part":null, + "subs":null + }, + { + "id":104, + "fio":"Васечкин Г.Г.", + "position":"Programmer", + "rate":50, + "project":"FirstProject", + "part":0.15, + "subs":null + }, + { + "id":105, + "fio":"Шатунов Д.Д.", + "position":"Programmer", + "rate":50, + "project":"FirstProject", + "part":0.1, + "subs":null + }, + { + "id":106, + "fio":"Михалин Е.Е.", + "position":"Programmer", + "rate":50, + "project":"FirstProject", + "part":0.1, + "subs":null + }, + { + "id":107, + "fio":"Маврычев Ё.Ё.", + "position":"Programmer", + "rate":50, + "project":"SecondProject", + "part":0.1, + "subs":null + }, + { + "id":108, + "fio":"Веселов Ж.Ж.", + "position":"Programmer", + "rate":50, + "project":"SecondProject", + "part":0.1, + "subs":null + }, + { + "id":109, + "fio":"Кривоногов З.З.", + "position":"Programmer", + "rate":50, + "project":"SecondProject", + "part":0.1, + "subs":null + }, + { + "id":110, + "fio":"Васин Й.Й.", + "position":"Programmer", + "rate":50, + "project":"SecondProject", + "part":0.1, + "subs":null + }, + { + "id":111, + "fio":"Постников И.И.", + "position":"Programmer", + "rate":50, + "project":"ThirdProject", + "part":0.1, + "subs":null + }, + { + "id":112, + "fio":"Кошкина К.К.", + "position":"Programmer", + "rate":50, + "project":"ThirdProject", + "part":0.1, + "subs":null + }, + { + "id":113, + "fio":"Лапин Л.Л.", + "position":"Programmer", + "rate":50, + "project":"ThirdProject", + "part":0.1, + "subs":null + }, + { + "id":114, + "fio":"Колесникова М.М.", + "position":"Programmer", + "rate":50, + "project":"ThirdProject", + "part":0.1, + "subs":null + }, + { + "id":115, + "fio":"Спиридонова Н.Н.", + "position":"Tester", + "rate":120, + "project":"FirstProject", + "part":0.05, + "subs":null + }, + { + "id":116, + "fio":"Мартынова О.О.", + "position":"Tester", + "rate":120, + "project":"FirstProject", + "part":0.05, + "subs":null + }, + { + "id":117, + "fio":"Лысов П.П.", + "position":"Tester", + "rate":120, + "project":"SecondProject", + "part":0.05, + "subs":null + }, + { + "id":118, + "fio":"Макарычев Р.Р.", + "position":"Tester", + "rate":120, + "project":"SecondProject", + "part":0.05, + "subs":null + }, + { + "id":119, + "fio":"Иванова С.С.", + "position":"Tester", + "rate":120, + "project":"ThirdProject", + "part":0.05, + "subs":null + }, + { + "id":120, + "fio":"Сучкина Т.Т.", + "position":"Tester", + "rate":120, + "project":"ThirdProject", + "part":0.05, + "subs":null + }, + { + "id":121, + "fio":"Гаврилов У.У.", + "position":"TeamLeader", + "rate":180, + "project":"FirstProject", + "part":0.15, + "subs":1 + }, + { + "id":122, + "fio":"Лесов Ф.Ф.", + "position":"TeamLeader", + "rate":180, + "project":"FirstProject", + "part":0.1, + "subs":2 + }, + { + "id":123, + "fio":"Коптева Х.Х.", + "position":"TeamLeader", + "rate":180, + "project":"SecondProject", + "part":0.1, + "subs":2 + }, + { + "id":124, + "fio":"Новоторова Ц.Ц.", + "position":"TeamLeader", + "rate":180, + "project":"SecondProject", + "part":0.1, + "subs":2 + }, + { + "id":125, + "fio":"Чалин Ч.Ч.", + "position":"TeamLeader", + "rate":180, + "project":"ThirdProject", + "part":0.1, + "subs":2 + }, + { + "id":126, + "fio":"Шарова Ш.Ш.", + "position":"TeamLeader", + "rate":180, + "project":"ThirdProject", + "part":0.1, + "subs":2 + }, + { + "id":127, + "fio":"Шнуров Щ.Щ.", + "position":"ProjectManager", + "rate":null, + "project":"FirstProject", + "part":0.2, + "subs":7 + }, + { + "id":128, + "fio":"Багратионов Э.Э.", + "position":"ProjectManager", + "rate":null, + "project":"SecondProject", + "part":0.2, + "subs":8 + }, + { + "id":129, + "fio":"Галкин Ю.Ю.", + "position":"ProjectManager", + "rate":null, + "project":"ThirdProject", + "part":0.2, + "subs":8 + }, + { + "id":130, + "fio":"Лысова Я.Я.", + "position":"SeniorManager", + "rate":null, + "project":"", + "part":0.1, + "subs":29 + } + ] +}