Experiment
Experiment
4
Student Name: UID:
Branch: Section/Group
Semester: Date of Performance:
Subject Name Subject Code:
2. Task to be done:
2. Add employee
3. Display
4. switch option
}
@Override
public String toString {
return "Employee [id= " =id +", name=" + name +", salary=" + salary
+",age="+age+"]";
}
public Employee(int id, String name, double salary, int age){
super();
//this.id = id;
setid(id);
//this.name= name;
setName(name);
//this.salary= salary;
setsalary(salary);
//this.age= age;
setAge(age);
}
public void setName(string name0 {
this.name = name;
}
public double getSalary(){
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public intgetAge(){
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.util.*;
importjava.io.*;
public class EmoloyeeManager
{
int menuChoice=0;
Scanner objectScanner= new Scanner(System.in);
List<Employee>objectEmployeeList= new LinkedList<employee>();
Public static void main(String[] args)
{
EmployeeManager objectManager= new EmployeeManager();
objectManager.displayMenu();
while(objectManager.menuChoice1=5)
{
Objectmanager.displayMeny();
}
}
Void displayMenu()
{
System.out.println(“Main Menu’);
System.out.println(“1.Add an employee”);
System.out.println(“2.display all”);
System.out.println(“3.write all the entered employees to file”);
System.out.println(“4.Read all the employees from filr”);
System.out.println(“5.exit”);
Menuchoice=objectScanner.nextlnt();
Try
{
Switch(menuChoice)
{
Case 1:
getEmployeeImformationformconsole();
break;
Case 2:
displayfromList(obhectEmployeeList);
breake;
Case 3:
System.out.println(“Write the file name you want to write to “);
WriteEmpolyeesToFile(objectScanner.next());
Break;
Case 4:
System.out.println(“Write the file name you want to read from “);
readEmployeesFromFile(objectScanner.next());
break;
case 5:
system.out.println(“Exiting the progrem”);
System.exit(0);
}
}
Catch(InputMismatchException e)
{
System.out.println(“Exception is --”+e);
}
}
Void writeEmployeesToFile(String fileName)
{
File fileobject= new file(filename);
FileoutputStream fileStreamObject;
Try{
fileStreamObject = new fileOutputStream(FileObject);
System.out.println(“ inside file”);
ObjectOutputStream ObjectWriterTofile= new
ObjectOutputstream(fileStreamObject);
objectwritertoFile.writeObjecy(ObjectemployeeList;
}
Catch(FileNotfoundException e)
{
e.printStackTrace();
}
Catch(IOException e)
{
e.printStackTrace();
}
}
string readEmployeesFromFile(String fileNAme)
{
String status=null;
File fileobject= new File(filename);
If(!fileobject.exists())
{
Status=”unable to create the file”;
Return status;
}
FileInputStream file StreamObject;
Try{
fileStreamObject = newFileInputStream(fileobject);
ObjectInputstream objectReaderFromfile = new
ObjectInputstream(fileStreamObject);
objectEmployeeList.clear();
ObjectEmployeeList=(List<Employee>)objectReaderFromfile.readObject();
Status=displayFromList(objectEmployeeList);
If(status.equalsIngoreCase(“Displayed successfully”))
{
Status=”Files read from file successfully”;
}
Else
{
Status=”” ;
}
}
Catch(FileNotfoundException e)
{
e.printstackTrace();
}
Catch (IOException e)
{
e.printStrackTrace();
}
Catch ( ClassNotFountException e)
{
e.printstackTrace();
}
Return status;
}
Void getEmployeeInformationfromConsole()
{
System.out.println(“Enter employee ID:”);
Try{
Int id=objectScanner.nextInt();
System.out.println(“Enter employee Name:”);
String name=objectScanner.next();
System.out.println(“Enter employee Age:”);
Int age= objectScanner.nextlnt();
System.outprintln(‘enter employee salary:”);
Double salary= objectScanner.nextDouble();
objectEmployeeList.ass(new Employee, name, salary, age));
}
Catch(inputMismatchexception e)
{
System.out.prinylm(“”+e);
}
}
String displayFromList(List<Employee>objectList)
{
String status=””;
Try{
Lterator objectlterator= objectList.iterator();
While(objectlterator.hasNext())
{
System.out.println(“”+objectlterator.next());
}
Status=”displayed Successfully”;
Return status;
}
Catch(Exception e)
{
Stais= “Failure”;
Return status;
}
}
}
11. Graphs (If Any): Image /Soft copy of graph paper to be attached here
2.To take input by user in run time and solve the problem.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the
faculty):