create program on eclips java
Lab8/.classpath
Lab8/.project
Lab8 org.eclipse.jdt.core.javabuilder org.eclipse.jdt.core.javanature
Lab8/Depts.txt
AM,Administration,11773 EN,Engineering,11787 EC,Executive,10856 SL,Sales,12165 FN,Finance,11841 MR,Marketing,11365 MF,Manufacturing,10961
Lab8/Emps.dat
Lab8/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.source=1.7
Lab8/edu/iup/cosc210/company/bo/Company.class
package edu.iup.cosc210.company.bo; public synchronized class Company { private java.util.List departments; private java.util.List employees; public void Company(); public void addDepartment(Department); public Department findDepartment(String); public int getNoDepts(); public Department getDeparment(int); public void addEmployee(Employee); public int getNoEmployees(); public Employee getEmployee(int); }
Lab8/edu/iup/cosc210/company/bo/Company.java
Lab8/edu/iup/cosc210/company/bo/Company.java
package
edu
.
iup
.
cosc210
.
company
.
bo
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
/**
* A Company. Maintains a list of departments and methods access
* the company's departments.
*
*
@author
David T. Smith
*/
public
class
Company
{
private
List
<
Department
>
departments
=
new
ArrayList
<
Department
>
();
private
List
<
Employee
>
employees
=
new
ArrayList
<
Employee
>
();
/**
* Add a Department to the list of departments for this company.
*
*
@param
department - the company to be added
*/
public
void
addDepartment
(
Department
department
)
{
departments
.
add
(
department
);
}
/**
* Find a department with a given department code
*
*
@param
deptCode - the department code used to find a department
*
@return
the department with the given code. Returns null if
* a department by the given department code is not found.
*/
public
Department
findDepartment
(
String
deptCode
)
{
for
(
Department
department
:
departments
)
{
if
(
deptCode
.
equals
(
department
.
getDeptCode
()))
{
return
department
;
}
}
return
null
;
}
/**
* Get the number of departments in this company.
*
@return
the number of departments in this company.
*/
public
int
getNoDepts
()
{
return
departments
.
size
();
}
/**
* Get the ith department in this company
*
@param
i - index identifying the department to be returned
*
@return
the ith department in this company
*/
public
Department
getDeparment
(
int
i
)
{
return
departments
.
get
(
i
);
}
/**
* Add an employee to the department's list of employees
*
*
@param
employee -
* employee to add to the department
*/
public
void
addEmployee
(
Employee
employee
)
{
Department
dept
=
findDepartment
(
employee
.
getDeptCode
());
if
(
dept
!=
null
)
{
dept
.
addEmployee
(
employee
);
}
employees
.
add
(
employee
);
}
/**
* Get the number of employees in the department
*
*
@return
- the number of employees in the department
*/
public
int
getNoEmployees
()
{
return
employees
.
size
();
}
/**
* Get an employee given an index position
*
*
@param
emp -
* the employee to return
*
@return
- the employee at the given position
*
@throws
IndexOutOfBoundsException
* if the index is less that 0 or greater than or equal to the
* number of employees.
*/
public
Employee
getEmployee
(
int
index
)
{
return
employees
.
get
(
index
);
}
}
Lab8/edu/iup/cosc210/company/bo/CompanyReport.class
package edu.iup.cosc210.company.bo; public synchronized class CompanyReport { private Company company; private static java.text.SimpleDateFormat dateFormatter; private static java.text.DecimalFormat decformatter; static void <clinit>(); public void CompanyReport(); public static void main(String[]); public void loadDepts(String) throws NumberFormatException, java.io.IOException; public void loadEmployees(String) throws Exception; public void printCompanyReport(); }
Lab8/edu/iup/cosc210/company/bo/CompanyReport.java
Lab8/edu/iup/cosc210/company/bo/CompanyReport.java
package
edu
.
iup
.
cosc210
.
company
.
bo
;
import
java
.
io
.
FileNotFoundException
;
import
java
.
io
.
IOException
;
import
java
.
text
.
DecimalFormat
;
import
java
.
text
.
SimpleDateFormat
;
import
edu
.
iup
.
cosc210
.
company
.
io
.
DepartmentReader
;
import
edu
.
iup
.
cosc210
.
company
.
io
.
EmployeeInputStream
;
/**
* Test printing of the company report.
*
*
@author
David T. Smith
*/
public
class
CompanyReport
{
private
Company
company
=
new
Company
();
private
static
SimpleDateFormat
dateFormatter
=
new
SimpleDateFormat
(
"MM/dd/yyyy"
);
private
static
DecimalFormat
decformatter
=
new
DecimalFormat
(
"#,###,###.00"
);
/**
* Main method to print the company report: Creates a company Loads
* Departments from the file name given in the first command line argument
* Loads Employees from the file name given in the last command line
* argument
*
*
@param
args - the command line arguments.
*/
public
static
void
main
(
String
[]
args
)
{
if
(
args
.
length
<
2
)
{
System
.
out
.
println
(
"Usage: java edu.iup.cosc210.company <department file> <employee file>"
);
System
.
exit
(
-
100
);
}
CompanyReport
companyReport
=
new
CompanyReport
();
try
{
companyReport
.
loadDepts
(
args
[
0
]);
companyReport
.
loadEmployees
(
args
[
1
]);
companyReport
.
printCompanyReport
();
}
catch
(
FileNotFoundException
e
)
{
System
.
out
.
println
(
"Unable to run: "
+
e
.
getMessage
());
System
.
exit
(
-
1
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
/**
* Load departments from a text file.
*
@param
fileName - the filename of the file that contains the departments.
*
@throws
IOException - in the event the file cannot be opened or read.
*/
public
void
loadDepts
(
String
fileName
)
throws
NumberFormatException
,
IOException
{
DepartmentReader
in
=
new
DepartmentReader
(
fileName
);
Department
department
;
while
((
department
=
in
.
readDepartment
())
!=
null
)
{
company
.
addDepartment
(
department
);
}
}
/**
* Load Employees from a binary file. The employees are added to the list of employees
* for their respective Department as indicated by deptCode.
*
@param
fileName - the name of the file that contains the employees.
*
@throws
Exception - catches an Exception.
*/
public
void
loadEmployees
(
String
fileName
)
throws
Exception
{
EmployeeInputStream
in
=
new
EmployeeInputStream
(
fileName
);
Employee
employee
;
while
((
employee
=
in
.
readEmployee
())
!=
null
)
{
company
.
addEmployee
(
employee
);
}
}
/**
* Prints a company report. Report include information on the department
* and a list of all employees.
*/
public
void
printCompanyReport
()
{
// loop over all departments
for
(
int
i
=
0
;
i
<
company
.
getNoDepts
();
i
++
)
{
Department
department
=
company
.
getDeparment
(
i
);
// print the department header
System
.
out
.
println
(
department
.
getDeptName
()
+
" Department"
);
System
.
out
.
println
();
System
.
out
.
printf
(
"%-20s%-20s\n"
,
"Manager: "
,
department
.
getManager
().
getFirstName
()
+
" "
+
department
.
getManager
().
getLastName
());
System
.
out
.
printf
(
"%-20s%-20s\n"
,
"Staff Size: "
,
department
.
getNoEmployees
());
System
.
out
.
printf
(
"%-20s%d\n"
,
"Vacation Days: "
,
department
.
getTotalVacationDays
());
System
.
out
.
println
();
// print the column labels for employees
System
.
out
.
printf
(
"%-5s %-26s %-10s %-3s %-8s %-6s %-3s\n"
,
"ID"
,
"Employee Name"
,
"Hire Date"
,
"Typ"
,
"Salary"
,
"Rate"
,
"Vac"
);
// loop over all employees in the department
for
(
int
j
=
0
;
j
<
department
.
getNoEmployees
();
j
++
)
{
Employee
emp
=
department
.
getEmployee
(
j
);
System
.
out
.
printf
(
"%5d %-26s %s %c %10s %6s %3d\n"
,
emp
.
getEmployeeNumber
(),
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
(),
dateFormatter
.
format
(
emp
.
getHireDate
()),
emp
.
getEmployeeType
(),
emp
.
getSalary
()
==
0
?
""
:
decformatter
.
format
(
emp
.
getSalary
()),
emp
.
getHourlyRate
()
==
0
?
""
:
String
.
format
(
"%6.2f"
,
emp
.
getHourlyRate
()),
emp
.
getVacationDays
());
}
System
.
out
.
println
();
System
.
out
.
println
();
}
}
}
Lab8/edu/iup/cosc210/company/bo/Department.class
package edu.iup.cosc210.company.bo; public synchronized class Department { private String deptCode; private String deptName; private int mgrEmpId; private java.util.List employees; private Employee manager; public void Department(String, String, int); public void addEmployee(Employee); public void removeEmployee(Employee); public String getDeptCode(); public String getDeptName(); public Employee getManager(); public int getNoEmployees(); public Employee getEmployee(int); public int getTotalVacationDays(); public boolean equals(Object); }
Lab8/edu/iup/cosc210/company/bo/Department.java
Lab8/edu/iup/cosc210/company/bo/Department.java
package
edu
.
iup
.
cosc210
.
company
.
bo
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
/**
* A Department of a Company.
*
*
@author
David T. Smith
*
*/
public
class
Department
{
private
String
deptCode
;
private
String
deptName
;
private
int
mgrEmpId
;
private
List
<
Employee
>
employees
=
new
ArrayList
<
Employee
>
();
private
Employee
manager
;
/**
* Constructor for Department.
*
*
@param
deptCode -
* the department code
*
@param
deptName -
* the department name
*
@param
mgrEmpId -
* the manager's employee id for the department
*/
public
Department
(
String
deptCode
,
String
deptName
,
int
mgrEmpId
)
{
this
.
deptCode
=
deptCode
;
this
.
deptName
=
deptName
;
this
.
mgrEmpId
=
mgrEmpId
;
}
/**
* Add an employee to the department's list of employees. In
* addition sets the employee's department and department code.
*
*
@param
employee -
* employee to add to the department
*/
public
void
addEmployee
(
Employee
employee
)
{
if
(
!
equals
(
employee
.
getDepartment
()))
{
employee
.
setDepartment
(
this
);
}
if
(
!
employees
.
contains
(
employee
))
{
employees
.
add
(
employee
);
}
}
/**
* Remove an employee from the department's list of employees
*
*
@param
employee - employee to be removed
*/
public
void
removeEmployee
(
Employee
employee
)
{
if
(
employees
.
contains
(
employee
))
{
employees
.
remove
(
employee
);
employee
.
setDepartment
(
null
);
}
}
/**
* Get the code for the department
*
*
@return
- the code for the department
*/
public
String
getDeptCode
()
{
return
deptCode
;
}
/**
* Get the name of the department
*
*
@return
- the name of the department
*/
public
String
getDeptName
()
{
return
deptName
;
}
/**
* Get the manager of the department. The manager is indicated by the
* mgrEmpId passed on the constructor. The manager must be an employee of
* the department, otherwise null is returned.
*
*
@return
- the department manager.
*/
public
Employee
getManager
()
{
if
(
manager
==
null
)
{
for
(
Employee
emp
:
employees
)
{
if
(
emp
.
getEmployeeNumber
()
==
mgrEmpId
)
{
manager
=
emp
;
break
;
}
}
}
return
manager
;
}
/**
* Get the number of employees in the department
*
*
@return
- the number of employees in the department
*/
public
int
getNoEmployees
()
{
return
employees
.
size
();
}
/**
* Get an employee given an index position
*
*
@param
emp -
* the employee to return
*
@return
- the employee at the given position
*
@throws
IndexOutOfBoundsException
* if the index is less that 0 or greater than or equal to the
* number of employees.
*/
public
Employee
getEmployee
(
int
index
)
{
return
employees
.
get
(
index
);
}
/**
* Get total number of vacation days of all employees in the department
*
*
@return
- the total number of vacation days
*/
public
int
getTotalVacationDays
()
{
int
totalVacationDays
=
0
;
for
(
Employee
emp
:
employees
)
{
totalVacationDays
+=
emp
.
getVacationDays
();
}
return
totalVacationDays
;
}
public
boolean
equals
(
Object
object
){
if
(
object
instanceof
Department
)
{
return
deptCode
.
equals
(((
Department
)
object
).
getDeptCode
());
}
return
false
;
}
}
Lab8/edu/iup/cosc210/company/bo/Employee.class
package edu.iup.cosc210.company.bo; public synchronized class Employee { private int employeeNumber; private String firstName; private String lastName; private String deptCode; private Department department; private java.util.Date hireDate; private char employeeType; private double salary; private double hourlyRate; private short vacationDays; private byte training; private static int nextAvailableEmployeeNumber; static void <clinit>(); public void Employee(int, String, String, String, java.util.Date, char, double, double, short, byte); public int getEmployeeNumber(); public static int getNextAvailableEmployeeNumber(); public String getFirstName(); public String getLastName(); public String getDeptCode(); public Department getDepartment(); public void setDepartment(Department); public java.util.Date getHireDate(); public char getEmployeeType(); public double getSalary(); public double getHourlyRate(); public short getVacationDays(); public byte getTraining(); public boolean equals(Object); }
Lab8/edu/iup/cosc210/company/bo/Employee.java
Lab8/edu/iup/cosc210/company/bo/Employee.java
package
edu
.
iup
.
cosc210
.
company
.
bo
;
import
java
.
util
.
Date
;
/**
* An Employee of a Company.
*
*
@author
David T. Smith
*/
public
class
Employee
{
private
int
employeeNumber
;
private
String
firstName
;
private
String
lastName
;
private
String
deptCode
;
private
Department
department
;
private
Date
hireDate
;
private
char
employeeType
;
private
double
salary
;
private
double
hourlyRate
;
private
short
vacationDays
;
private
byte
training
;
private
static
int
nextAvailableEmployeeNumber
=
0
;
/**
* Constructor for Employee
*
*
@param
employeeNumber
* - the employee's id number
*
@param
firstName
* - first name of the employee
*
@param
lastName
* - last name of the employee
*
@param
deptCode
* - department code of the employee's department
*
@param
hireDate
* - the date the employee was hired
*
@param
employeeType
* - indicates if an employee is Exempt ('E'), salaried ('S'),
* or hourly ('H')
*
@param
salary
* - the employee's salary
*
@param
hourlyRate
* - the employee's hourlyRate
*
@param
vacationDays
* - the number of vacation days
*
@param
training
* - a byte using bits to indicated the training the employee has
* received
*/
public
Employee
(
int
employeeNumber
,
String
firstName
,
String
lastName
,
String
deptCode
,
Date
hireDate
,
char
employeeType
,
double
salary
,
double
hourlyRate
,
short
vacationDays
,
byte
training
)
{
super
();
this
.
employeeNumber
=
employeeNumber
;
if
(
employeeNumber
>=
nextAvailableEmployeeNumber
)
{
nextAvailableEmployeeNumber
=
employeeNumber
+
1
;
}
this
.
firstName
=
firstName
;
this
.
lastName
=
lastName
;
this
.
deptCode
=
deptCode
;
this
.
hireDate
=
hireDate
;
this
.
employeeType
=
employeeType
;
this
.
salary
=
salary
;
this
.
hourlyRate
=
hourlyRate
;
this
.
vacationDays
=
vacationDays
;
this
.
training
=
training
;
}
/**
* Get the employee number
*
*
@return
- the employee number
*/
public
int
getEmployeeNumber
()
{
return
employeeNumber
;
}
/**
* Get the next available employee number. This will be one greater
* than the highest employee numbers of all Employee instances.
*
*
@return
next available employee number
*/
public
static
int
getNextAvailableEmployeeNumber
()
{
return
nextAvailableEmployeeNumber
;
}
/**
* Get the employee's first name
*
*
@return
- the employee's first name
*/
public
String
getFirstName
()
{
return
firstName
;
}
/**
* Get the employee's last name
*
*
@return
- the employee's last name
*/
public
String
getLastName
()
{
return
lastName
;
}
/**
* Get the employee's department code.
*
*
@return
- the employee's department code
*/
public
String
getDeptCode
()
{
return
deptCode
;
}
/**
* Get the employee's department.
*
*
@return
- the employee's department
*/
public
Department
getDepartment
()
{
return
department
;
}
/**
* Set the employee's department.
*
*
@param
department - the employee's department
*/
public
void
setDepartment
(
Department
department
)
{
if
(
this
.
department
!=
null
&&
!
this
.
department
.
equals
(
department
))
{
this
.
department
.
removeEmployee
(
this
);
}
if
(
department
==
null
)
{
department
=
null
;
deptCode
=
""
;
return
;
}
boolean
doAdd
=
false
;
if
(
this
.
department
==
null
||
!
this
.
department
.
equals
(
department
))
{
doAdd
=
true
;
}
this
.
deptCode
=
department
.
getDeptCode
();
this
.
department
=
department
;
if
(
doAdd
)
{
department
.
addEmployee
(
this
);
}
}
/**
* Get the employee's hire date as a string of the form mm/dd/yyyy.
*
*
@return
- returns the employee's hire date
*/
public
Date
getHireDate
()
{
return
hireDate
;
}
/**
* Get the employee's type E for exempt, S for salaried, H for hourly
*
*
@return
- the employee's type
*/
public
char
getEmployeeType
()
{
return
employeeType
;
}
/**
* Get the employee's salary
*
*
@return
- the employee's salary
*/
public
double
getSalary
()
{
return
salary
;
}
/**
* Get the employee's hourly rate
*
*
@return
- the employee's hourly rate
*/
public
double
getHourlyRate
()
{
return
hourlyRate
;
}
/**
* Get the number of vacation days for an employee
*
*
@return
- the number of vacation days an employee has
*/
public
short
getVacationDays
()
{
return
vacationDays
;
}
/**
* Get the encoded traning byte.
*
*
@return
- a byte whose bits indicate traingin the employee has recieved
*/
public
byte
getTraining
()
{
return
training
;
}
public
boolean
equals
(
Object
object
){
if
(
object
instanceof
Employee
)
{
return
employeeNumber
==
((
Employee
)
object
).
getEmployeeNumber
();
}
return
false
;
}
}
Lab8/edu/iup/cosc210/company/io/DepartmentReader.class
package edu.iup.cosc210.company.io; public synchronized class DepartmentReader { java.io.BufferedReader input; public void DepartmentReader(String) throws java.io.FileNotFoundException; public edu.iup.cosc210.company.bo.Department readDepartment() throws java.io.IOException; public void close() throws java.io.IOException; }
Lab8/edu/iup/cosc210/company/io/DepartmentReader.java
Lab8/edu/iup/cosc210/company/io/DepartmentReader.java
package
edu
.
iup
.
cosc210
.
company
.
io
;
import
java
.
io
.
BufferedReader
;
import
java
.
io
.
FileNotFoundException
;
import
java
.
io
.
FileReader
;
import
java
.
io
.
IOException
;
import
edu
.
iup
.
cosc210
.
company
.
bo
.
Department
;
/**
* Helper class to read departments from a comma separated
* department text file. The method readDepartment() returns
* the next department from the department file.
*
*
@author
David T. Smith
*/
public
class
DepartmentReader
{
BufferedReader
input
;
/**
* Constructor - opens the department file.
*
@param
fileName - name of the department file
*
@throws
FileNotFoundException - in the event the file could not be opened
*/
public
DepartmentReader
(
String
fileName
)
throws
FileNotFoundException
{
input
=
new
BufferedReader
(
new
FileReader
(
fileName
));
}
/**
* Read the next department from the department file.
*
@return
a department. Returns null in the event the end of
* the department file is reached.
*
@throws
IOException
*/
public
Department
readDepartment
()
throws
IOException
{
String
line
=
input
.
readLine
();
// Test for end of file
if
(
line
==
null
)
{
return
null
;
}
String
[]
parts
=
line
.
split
(
","
);
String
deptCode
=
parts
[
0
];
String
deptName
=
parts
[
1
];
int
mgrEmpId
=
Integer
.
parseInt
(
parts
[
2
]);
return
new
Department
(
deptCode
,
deptName
,
mgrEmpId
);
}
/**
* Close the department file
*
@throws
IOException
*/
public
void
close
()
throws
IOException
{
input
.
close
();
}
}
Lab8/edu/iup/cosc210/company/io/EmployeeInputStream.class
package edu.iup.cosc210.company.io; public synchronized class EmployeeInputStream { private java.io.DataInputStream input; private byte[] firstNameBytes; private byte[] lastNameBytes; private byte[] deptCodeBytes; public void EmployeeInputStream(String) throws java.io.FileNotFoundException; public edu.iup.cosc210.company.bo.Employee readEmployee() throws java.io.IOException; public void close() throws java.io.IOException; }
Lab8/edu/iup/cosc210/company/io/EmployeeInputStream.java
Lab8/edu/iup/cosc210/company/io/EmployeeInputStream.java
package
edu
.
iup
.
cosc210
.
company
.
io
;
import
java
.
io
.
DataInputStream
;
import
java
.
io
.
FileInputStream
;
import
java
.
io
.
FileNotFoundException
;
import
java
.
io
.
IOException
;
import
java
.
util
.
Date
;
import
edu
.
iup
.
cosc210
.
company
.
bo
.
Employee
;
/**
* Helper class to read employees from a binary employee file.
* The method readEmployee() returns the next employee from
* the employee file.
*
*
@author
David T. Smith
*/
public
class
EmployeeInputStream
{
private
DataInputStream
input
;
// declare byte buffers for the ascii fields.
private
byte
[]
firstNameBytes
=
new
byte
[
10
];
private
byte
[]
lastNameBytes
=
new
byte
[
15
];
private
byte
[]
deptCodeBytes
=
new
byte
[
2
];
/**
* Constructor - opens the employee file.
*
@param
fileName - name of the employee file
*
@throws
FileNotFoundException - in the event the file could not be opened
*/
public
EmployeeInputStream
(
String
fileName
)
throws
FileNotFoundException
{
input
=
new
DataInputStream
(
new
FileInputStream
(
fileName
));
}
/**
* Read the next employee from the employee file.
*
@return
an employee. Returns null in the event the end of
* the employee file is reached.
*
@throws
IOException
*/
@
SuppressWarnings
(
"deprecation"
)
public
Employee
readEmployee
()
throws
IOException
{
// Test for end of file
if
(
input
.
available
()
==
0
)
{
return
null
;
}
int
employeeNumber
=
input
.
readInt
();
input
.
read
(
firstNameBytes
);
String
firstName
=
new
String
(
firstNameBytes
).
trim
();
input
.
read
(
lastNameBytes
);
String
lastName
=
new
String
(
lastNameBytes
).
trim
();
input
.
read
(
deptCodeBytes
);
String
deptCode
=
new
String
(
deptCodeBytes
);
byte
month
=
input
.
readByte
();
byte
day
=
input
.
readByte
();
short
year
=
input
.
readShort
();
Date
hireDate
=
new
Date
(
year
-
1900
,
month
-
1
,
day
);
char
empType
=
(
char
)
input
.
readByte
();
double
salary
=
input
.
readDouble
();
double
hourlyRate
=
input
.
readDouble
();
short
vacationDays
=
input
.
readShort
();
byte
training
=
input
.
readByte
();
Employee
employee
=
new
Employee
(
employeeNumber
,
firstName
,
lastName
,
deptCode
,
hireDate
,
empType
,
salary
,
hourlyRate
,
vacationDays
,
training
);
return
employee
;
}
/**
* Close the employee file
*
@throws
IOException
*/
public
void
close
()
throws
IOException
{
input
.
close
();
}
}