Skip to content
Open

B10 #74

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>StuffDemo</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>
16 changes: 16 additions & 0 deletions projects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"projects": [
{
"name":"FirstProject",
"budget":5000000
},
{
"name":"SecondProject",
"budget":1000000
},
{
"name":"ThirdProject",
"budget":7000000
}
]
}
5 changes: 5 additions & 0 deletions src/main/java/Cleaner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Cleaner extends Personal{
public Cleaner(int id, String name, int rate){
super(id, name, rate);
}
}
20 changes: 20 additions & 0 deletions src/main/java/Driver.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/Employee.java
Original file line number Diff line number Diff line change
@@ -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();
}
25 changes: 25 additions & 0 deletions src/main/java/Engineer.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 4 additions & 0 deletions src/main/java/Heading.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public interface Heading {
final int PAYMENT= 2000;
int calcPaymentBySubs();
}
14 changes: 14 additions & 0 deletions src/main/java/Manager.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
16 changes: 16 additions & 0 deletions src/main/java/Personal.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
5 changes: 5 additions & 0 deletions src/main/java/Programmer.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 3 additions & 0 deletions src/main/java/Project.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface Project {
int calcPaymentByPart();
}
17 changes: 17 additions & 0 deletions src/main/java/ProjectManager.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
17 changes: 17 additions & 0 deletions src/main/java/Projects.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 5 additions & 0 deletions src/main/java/SeniorManager.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
156 changes: 156 additions & 0 deletions src/main/java/Staff.java
Original file line number Diff line number Diff line change
@@ -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> projects;
private ArrayList<Employee> 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<Employee>();
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<Projects>();
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());
}
}
}
Loading