-
Notifications
You must be signed in to change notification settings - Fork 0
/
Company.java
93 lines (65 loc) · 2.5 KB
/
Company.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package lab4;
import java.util.*;
/**
* @author Fei Alm
* @version 1.0
* @since 2019
*/
public class Company extends Object{
// has one dynamic array that stores all employees (workers and directors)once
private ArrayList<Employee> theCompanyList;
/** The constructor takes no argument */
public Company() {
// skapar nytt objekt (arraylista) som ska innehålla Employee-objekt
theCompanyList = new ArrayList<Employee>();
}
/**
* This instance method adds a director to the dynamic array of employees.
* @param directorWillBeAdded - the argument that is received to this method is of type Director
* */
public void addEmployee(Director directorWillBeAdded) {
// This instance method adds a director to the dynamic array of employees.
theCompanyList.add(directorWillBeAdded);
}
/**
* Adds a worker to theCompanyList and add worker to its directors list, theDirector
* @param aWorker - the worker that will be put into the list, type Worker
* @param theDirector - the director that the worker till be connected to
*/
public void addEmployee(Worker aWorker, Director theDirector) {
// en chef och hans arbetare till listan
theCompanyList.add(aWorker);
// kopllar ihop worker och chef
// skickar in wotker till metoden
// lägger in i theDirector lista, alla hans/hennes workers
theDirector.addEmployee(aWorker);
}
/**
* Takes no argument.
* Displays the list of employees (workers and directors). Different titles for different sorting options
*/
public String toString() {
String title = "List of Employees" + "\n------------------------";
String result = "";
String Employees = "";
if(Employee.sortCriterion == Employee.BYNAME) {
result = "\nFirst name " + "Surname " + "Number";
}
if(Employee.sortCriterion == Employee.BYSALARY){
result = "\nFirst name " + "Surname " + "Number " + " Salary";
}
if(Employee.sortCriterion == Employee.BYTAXES) {
result = "\nFirst name " + "Surname " + "Number " + " Taxes";
}
// hämtar Collections.sort med import java.util.*;
// sorterar
// sort metoden anropar compareTo och får svar på vad den ska sortera och hur
Collections.sort(theCompanyList);
// hämtar och skriver ut employee-listan
for(int i = 0; i < theCompanyList.size(); i++ ) {
Employees += "\n" + theCompanyList.get(i).toString();
}
title += result + Employees;
return title;
}
}