question archive Java program Task 1: Create a class Employee with following attributes empID, empName, status as enumeration wich has two possible values PermanantEmployee and VisitingEmployee
Subject:Computer SciencePrice:2.87 Bought7
Java program
Task 1: Create a class Employee with following attributes empID, empName, status as enumeration wich has two possible values PermanantEmployee and VisitingEmployee. Create a class Department which depName and depEmployees(of type Employee). All attributes are privates and exposed via getter and setter. Both classes have a fully parameterized constructor.
Task 2: In main class, create an Arraylist of Department. Create a static method to insertData() to add data in ArrayList( Ask user to input data in the list). Create another method deleteData() which will delete specific employee from given index. Create another method named updateData() which will update status of an employee. Create another method displayData() which will print data of departments.
Answer:
Java Program
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
enum status {
PermanantEmployee , VisitingEmployee
}
class Main {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
ArrayList<Department> arrLstDpt = new ArrayList<Department>();//ArrayList creation
while(true) //Menu
{
System.out.println("1.Insert\n2.Delete\n3.Upadate status\n4.Print\n5.Exit");
System.out.println("Enter your chpice:?");
int ch= Integer.parseInt(s.nextLine());
switch(ch)
{
case 1:
insert(arrLstDpt);
break;
case 2:
deleteData(arrLstDpt);
break;
case 3:
updateData(arrLstDpt);
break;
case 4:
displayData(arrLstDpt);
break;
case 5:
System.exit(0) ;
default:
System.out.println("Wrong choice");
}
}
}
//function for insertion
public static void insert( ArrayList<Department> arrLstDpt)
{
Department dpt=new Department();
status st;
Scanner s = new Scanner(System.in);
System.out.println("Enter department name:?");
String dptName=s. nextLine();
System.out.println("Enter employee Id?");
String id= s.nextLine();
System.out.println("Enter employee name:?");
String empName=s. nextLine();
System.out.println("Enter 1 for PermanantEmployee 2 for VisitingEmployee?");
int type=s. nextInt();
if(type==1)
st=status.PermanantEmployee;
else
st=status.VisitingEmployee;
dpt.set(dptName,id,empName,st);
arrLstDpt.add(dpt);
}
//static function for printing
public static void displayData( ArrayList<Department> arrLstDpt)
{
for (int i = 0; i < arrLstDpt.size(); i++) {
System.out.println("----------------------------");
System.out.println("Index : "+i);
arrLstDpt.get(i).get();
System.out.println("----------------------------");
}
}
//static function for deleteData()
public static void deleteData( ArrayList<Department> arrLstDpt){
if(arrLstDpt.size()!=0)
{
System.out.println("----------------------------");
Scanner s = new Scanner(System.in);
System.out.println("Enter index of employee to remove:?");
int id= Integer.parseInt(s.nextLine());
arrLstDpt.remove(id);
System.out.println("Removed employee at index"+id);
}
else
{
System.out.println("Empty employee list");
}
}
//static function for updateData()
public static void updateData( ArrayList<Department> arrLstDpt){
status st;
if(arrLstDpt.size()!=0)
{
System.out.println("----------------------------");
Department dpt=new Department();
Scanner s = new Scanner(System.in);
System.out.println("Enter index of employee to update his status?");
int id= Integer.parseInt(s.nextLine());
dpt=arrLstDpt.get(id);
System.out.println("Enter 1 for PermanantEmployee 2 for VisitingEmployee?");
int type=s. nextInt();
if(type==1)
st=status.PermanantEmployee;
else
st=status.VisitingEmployee;
dpt.updateStatus(st);
arrLstDpt.set(id,dpt);
System.out.println("updated employee at index"+id);
}
else
{
System.out.println("Empty employee list");
}
}
}
//employee class with specified details
class employee
{
private String empID;
private String employeeName;
private status stat;
employee() //constructor
{
}
employee(String id,String name,status st)//parameterised constructor
{
empID=id;
employeeName=name;
stat=st;
}
void get() //for retrieving values
{
System.out.println("Employee Id : "+empID);
System.out.println("Employee Name : "+employeeName);
System.out.println("Employee Status : "+stat);
}
void set(String id,String name,status st)
{
empID=id;
employeeName=name;
stat=st;
}
void updateStatus(status st)
{
stat=st;
}
}
//Department class with specified details
class Department
{
private String depName;
private employee depEmployees;
Department() //constructor
{
depEmployees=new employee();
}
Department(String dptName,String id,String name,status st)//parameterised constructor
{
depName=dptName;
depEmployees=new employee(id,name,st);
}
void get() //for retrieving values
{
System.out.println("Department :"+depName);
depEmployees.get();
}
void set(String dptName,String id,String name,status st)
{
depName=dptName;
depEmployees.set(id,name,st);
}
void updateStatus(status st)
{
depEmployees.updateStatus(st);
}
}
Sample Output
please use this google drive link to download the answer file.
https://drive.google.com/file/d/1nL8w4vDtVr24Kjmff0XgyqDPZwHuAWpR/view?usp=sharing
note: if you have any trouble in viewing/downloading the answer from the given link, please use this below guide to understand the whole process.
https://helpinhomework.org/blog/how-to-obtain-answer-through-google-drive-link