Write a program to allow the user to 1 Create two classes Em
Write a program to allow the user to:
1. Create two classes. Employee and Departments.
The Department class will have: DepartmentID, Departmentname, DepartmentHeadName.
The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID.
Both of the above classes should have appropriate constructors, accessor methods.
2. Create two arrays . One for Employee with the size 7 and another one for Department with the size 3.
Your program should display a menu for the user to do the following:
1. Create Department. Collect all information about a department. Make sure the department ID does not already exist in the array containing Department objects. If it does not, then insert the Department object into the array. When the array is full, display the error message to the user \"The array is full, you can not add any more departments\"
2. Create Employee. Collect all information about an Employee. Make sure the Employee ID does not already exist in the array containing Employee objects. If it does not, then insert the Employee object into the array. Also make sure that the DepartmentID that the employee belongs also exists.
When the array is full, display the error message to the user \"The array is full, you can not add any more Employees\"
3. Write the data to the file. When the user selects this option, dump the information in each array into a separate file.
4. Retrieve data from file. When user selects this option, open each file, load the data from the file into the appropriate array.
5. Display Report: When user selects this option, go through arrays and display the total salary paid for each department.
Solution
Plese let me know If you need more information:-
---------------------------------------------------------------------------
TestCase.java
========================================
package employee;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class TestCase {
static int employee_count = 0;
static int department_count = 0;
static Employee emp[] = new Employee[7];
static Departments dep[] = new Departments[3];
public static void printMenu(){
System.out.println(\"1. Create Department.\");
System.out.println(\"2. Create Employee. \");
System.out.println(\"3. Write the data to the file.\");
System.out.println(\"4. Retrieve data from file.\");
System.out.println(\"5. Display Report: \");
}
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
char ch = \'\\0\';
while (true) {
printMenu();
ch = sc.next().charAt(0);
switch (ch) {
case \'2\':
if(employee_count < 7){
Employee tempEmp = createEmployee();
if(tempEmp != null){
emp[employee_count] = tempEmp;
employee_count++;
System.out.println(\"Employee added...\");
}else{
System.out.println(\"Enter Employee data properly ....\");
}
}
else
System.out.println(\"The array is full, you can not add any more Employees\");
break;
case \'1\':
if(department_count < 3){
Departments tempDep = createDepartment();
if(tempDep != null) {
dep[department_count] = tempDep;
department_count++;
System.out.println(\"Department added....\");
}else{
System.out.println(\"Department already exist\");
}
}
else
System.out.println(\"The array is full, you can not add any more departments\");
break;
case \'3\':
writeDataToFile();
System.out.println(\"Data written to file\");
break;
case \'4\':
readDataFromFile();
System.out.println(\"Data read to arrays\");
break;
case \'5\':
totalSalaryPaidForDept();
break;
default:
System.out.println(\"Entered non valid selection Going to exit.\");
System.exit(0);
}
}
} catch (Exception e) {
System.out.println(\"Something went wrong...Retry\"+e);
}
}
private static Departments createDepartment() {
Scanner scanner=new Scanner(System.in);
System.out.println(\"Enter DepartmentID\");
String DepartmentID=scanner.next();
if(department_count > 0 && checkDepartmentID(DepartmentID)) return null;
System.out.println(\"Enter Departmentname\");
String Departmentname=scanner.next();
System.out.println(\"Enter DepartmentHeadName\");
String DepartmentHeadName=scanner.next();
return new Departments(DepartmentID, Departmentname, DepartmentHeadName);
}
private static boolean checkDepartmentID( String deptID){
for(int i=0;i<3;i++){
if(dep[i] != null && dep[i].DepartmentID.equals(deptID)) return true;
}
return false;
}
private static boolean checkEmployeeID( String empID){
for(int i=0;i<7;i++){
if(emp[i] != null && emp[i].employeeID.equals(empID)) return true;
}
return false;
}
private static Employee createEmployee() {
Scanner scanner=new Scanner(System.in);
System.out.println(\"Enter Employee ID\");
String employeeID=scanner.next();
if(employee_count >0 && checkEmployeeID(employeeID)) return null;
System.out.println(\"Enter Employee name\");
String emploeename=scanner.next();
System.out.println(\"Enter Employee salary\");
String employeesalary=scanner.next();
System.out.println(\"Enter Employee age\");
String employeeage=scanner.next();
System.out.println(\"Enter EmployeeDepartmentID\");
String employeeDepartmentID=scanner.next();
if(department_count >0 && checkDepartmentID(employeeDepartmentID)){
System.out.println(\"Department not valid...\");
return null;
}
return new Employee(employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID);
}
private static void writeDataToFile(){
// ///////////writing data to file from Binary to Ascii
String outFile = \"employee.data\";
Writer output = null;
File file = new File(outFile);
try{
output = new BufferedWriter(new FileWriter(file));
for (int i=0;i<employee_count;i++) {
output.write(emp[i] + \"\ \ \");
}
output.close();
}
catch(Exception e){}
outFile = \"department.data\";
output = null;
file = new File(outFile);
try{
output = new BufferedWriter(new FileWriter(file));
for (int i=0;i<department_count;i++) {
output.write(dep[i] + \"\ \ \");
}
output.close();
}
catch(Exception e){System.out.println(\"Something went wrong...Retry\");}
}
private static void readDataFromFile(){
String inEmpFile = \"employee.data\";
String inDepFile = \"department.data\";
try{
FileInputStream fstream = new FileInputStream(inEmpFile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = \"\";
int i=0;
// Read File Line By Line
while ((strLine = br.readLine()) != null && i < 7) {// lines
String data[] = strLine.split(\",\");
emp[i] = new Employee(data[0], data[1], data[2], data[3], data[4]);
i++;
}
fstream = new FileInputStream(inEmpFile);
// Get the object of DataInputStream
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
strLine = \"\";
i=0;
// Read File Line By Line
while ((strLine = br.readLine()) != null && i < 3) {// lines
String data[] = strLine.split(\",\");
dep[i] = new Departments(data[0], data[1], data[2]);
i++;
}
// Close the input stream
in.close();
}catch(Exception e){System.out.println(\"Something went wrong...Retry\"+e);}
}
private static void totalSalaryPaidForDept(){
String dept1 = null;
if(dep[0] != null){
dept1 = dep[0].DepartmentID;
}
String dept2 = null;
if(dep[1] != null){
dept2 = dep[1].DepartmentID;
}
String dept3 = null;
if(dep[2] != null){
dept3 = dep[2].DepartmentID;
}
double dep1Salary=0;
double dep2Salary=0;
double dep3Salary=0;
for(int i=0;i<employee_count;i++){
if(dept1 != null && emp[i] != null && dept1.equals(emp[i].employeeID)){
dep1Salary += Double.parseDouble(emp[i].employeesalary);
}
else if(dept2 != null && emp[i] != null && dept2.equals(emp[i].employeeID)){
dep2Salary += Double.parseDouble(emp[i].employeesalary);
}else if(dept3 != null && emp[i] != null){
dep3Salary += Double.parseDouble(emp[i].employeesalary);
}
}
System.out.println(dep[0].DepartmentID +\" \"+dep1Salary);
System.out.println(dep[1].DepartmentID +\" \"+dep2Salary);
System.out.println(dep[2].DepartmentID +\" \"+dep3Salary);
System.out.println(\"====================================================\");
for(int i=0;i<employee_count;i++){
System.out.println(emp[i]);
}
System.out.println(\"====================================================\");
for(int i=0;i<department_count;i++){
System.out.println(dep[i]);
}
System.out.println(\"====================================================\");
}
}
============================================================
Departments .java
----------------------------------------------------------------------------------------------
public class Departments {
String DepartmentID;
String Departmentname;
String DepartmentHeadName;
public Departments(String DepartmentID,String Departmentname,String DepartmentHeadName) {
this.DepartmentID=DepartmentID;
this.Departmentname=Departmentname;
this.DepartmentHeadName=DepartmentHeadName;
}
@Override
public String toString() {
return DepartmentID + \",\" + Departmentname + \",\" + DepartmentHeadName;
}
public String getDepartmentID() {
return DepartmentID;
}
public void setDepartmentID(String departmentID) {
this.DepartmentID = departmentID;
}
public String getDepartmentname() {
return Departmentname;
}
public void setDepartmentname(String departmentname) {
this.Departmentname = departmentname;
}
public String getDepartmentHeadName() {
return DepartmentHeadName;
}
public void setDepartmentHeadName(String departmentHeadName) {
this.DepartmentHeadName = departmentHeadName;
}
}
-------------------------------------------------------------
Employee.java
========================================
public class Employee {
String employeeID;
String emploeename;
String employeesalary;
String employeeage;
String employeeDepartmentID;
@Override
public String toString() {
return employeeID + \",\" + emploeename + \",\" + employeesalary + \",\" + employeeage + \",\" + employeeDepartmentID;
}
public Employee(String employeeID, String emploeename,
String employeesalary, String employeeage,
String employeeDepartmentID) {
super();
this.employeeID = employeeID;
this.emploeename = emploeename;
this.employeesalary = employeesalary;
this.employeeage = employeeage;
this.employeeDepartmentID = employeeDepartmentID;
}
public String getEmployeeID() {
return employeeID;
}
public void setEmployeeID(String employeeID) {
this.employeeID = employeeID;
}
public String getEmploeename() {
return emploeename;
}
public void setEmploeename(String emploeename) {
this.emploeename = emploeename;
}
public String getEmployeesalary() {
return employeesalary;
}
public void setEmployeesalary(String employeesalary) {
this.employeesalary = employeesalary;
}
public String getEmployeeage() {
return employeeage;
}
public void setEmployeeage(String employeeage) {
this.employeeage = employeeage;
}
public String getEmployeeDepartmentID() {
return employeeDepartmentID;
}
public void setEmployeeDepartmentID(String employeeDepartmentID) {
this.employeeDepartmentID = employeeDepartmentID;
}
}
====================================================






