Program Overview In this program, you will create a database of Employees. Each
ID: 3878235 • Letter: P
Question
Program Overview In this program, you will create a database of Employees. Each Employee has a name & and ID. Managers must also be part of this database. A manager is an employee as well. However, in addition to managers having all the characteristics of employees, managers also manage employees as subordinates. Therefore, a Manager has a list of Employees he/she manages as well as a count of how many subordinates he/she has. Your application should ask the user for details to maintain the list of employees. For each employee, the program should ask the user for the name and whether or not the employee is a manager. If the user specifies that the employee is a Manager, the program should ask the user how many subordinates they manage as well as their names. It should create a Manager object, and within the Manager object, create the subordinate Employees. The program should ask the user for the name of the subordinate employees and store their names in the subordinate Employee objects. If the user specifies the employee is not a Manager, the program should create an Employee object. All employees (including Managers), should be added to the database of Employees. Finally, the program should print out a list of all employees. However, if an Employee is a Manager, it should also print out a list his/her subordinates Employees.Explanation / Answer
Here is your program : ------------------------->>>>>>>>>>>>>.
import java.util.Scanner;
import java.util.*;
public class EmployeeSystem{
private static ArrayList<Employee> employeeList;
private static int noOfEmployee;
private static void RunProgram(){
System.out.println(" Employee management System ");
char choice = 'y';
Scanner sc = new Scanner(System.in);
while(choice == 'y'){
choice = 'y';
AddEmployee();
System.out.println("Do you Want to add more Employee (y/n)? ");
choice = sc.next().charAt(0);
}
PrintEmployeeList();
}
private static void PrintEmployeeList(){
Employee temp;
System.out.println(" List Of All Employee");
for(int i = 0;i<noOfEmployee;i++){
temp = employeeList.get(i);
System.out.println(temp.NAME());
if(temp instanceof Manager){
Manager t = (Manager)temp;
t.PrintSubordinateList();
}
}
}
private static void AddEmployee(){
char mCh = 'n';
Scanner sc = new Scanner(System.in);
String name = "";
System.out.println(" Adding Employee ");
System.out.println("Enter Employee name : ");
name = sc.next();
System.out.println("Is this Employee a Manager ? : ");
mCh = sc.next().charAt(0);
if(mCh == 'y'){
employeeList.add(new Manager(name));
Manager m = (Manager)employeeList.get(noOfEmployee);
System.out.println("How many subordinate Employee ? ");
int no = sc.nextInt();
m.setNoOfSubordinate(no);
noOfEmployee++;
for(int i = 0;i<no;i++){
employeeList.add(m.AddSubordinate());
noOfEmployee++;
}
}else{
employeeList.add(new Employee(name));
noOfEmployee++;
}
}
public static void main(String[] args) {
employeeList = new ArrayList<>();
RunProgram();
}
}
Employee.java : ------------->>>>>>>>>>>
public class Employee{
protected String name;
protected String id;
private int code = 100;
public Employee(){
name = "";
id = "";
}
public Employee(String n){
name = n;
id = n+code;
code++;
}
public void setName(String n){
name = n;
}
public void setID(String i){
id = i;
}
public String NAME(){
return name;
}
public String ID(){
return id;
}
}
Manager.java : --------------->>>>>>>>>>>>>..
import java.util.Scanner;
public class Manager extends Employee{
private Employee[] subordinate;
private int nuOfSubordinate;
public Manager(){
super();
nuOfSubordinate = 0;
}
public Manager(String n){
super(n);
}
public void setNoOfSubordinate(int n){
subordinate = new Employee[n];
}
public Employee AddSubordinate(){
Scanner sc = new Scanner(System.in);
System.out.println("Subordinate Name: ");
String name = sc.next();
subordinate[nuOfSubordinate] = new Employee(name);
nuOfSubordinate++;
return subordinate[nuOfSubordinate-1];
}
public void PrintSubordinateList(){
for(int i = 0;i<nuOfSubordinate;i++){
System.out.println(" "+subordinate[i].NAME());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.