Professional Documents
Culture Documents
Experiment - 4
Experiment - 4
Experiment -4
Create a menu based Java application with the following options.1. Add an
Employee2.Display All3.Exit If option 1 is selected, the application should
gather details of the employee like employee name, employee id, designation,
and salary and store it in a file. If option 2 is selected, the application should
display all the employee details. If option 3 is selected the application should
exit.
Tasks:-
1. Create menu absed java application
2. Add Employee
3. Display
4. Switch options
1. Aim/Overview of the practical: Create a menu based Java application with the
following options.1. Add an Employee2.Display All3.Exit If option 1 is selected, the
application should gather details of the employee like employee name, employee id,
designation, and salary and store it in a file. If option 2 is selected, the application should
display all the employee details. If option 3 is selected the application should exit..
2. Task to be done:
Create a menu based Java application with the following options.1. Add an
Employee2.Display All3.Exit If option 1 is selected, the application should gather
details of the employee like employee name, employee id, designation, and salary
and store it in a file. If option 2 is selected, the application should display all the
employee details. If option 3 is selected the application should exit.
package com.EM.Model;
import java.io.Serializable;
return id;
}
this.id = id;
}
return name;
}
@Override
}
super();
//this.id = id;
setId(id);
//this.name = name;
setName(name);
//this.salary = salary;
setSalary(salary);
//this.age = age;
setAge(age);
}
this.name = name;
}
return salary;
}
this.salary = salary;
}
return age;
}
this.age = age;
}
import java.util.*;
import java.io.*;
public class EmployeeManager
int menuChoice=0;
{
objectManager.displayMenu();
while(objectManager.menuChoice!=5)
{
objectManager.displayMenu();
}
}
void displayMenu()
{
System.out.println("Main Menu");
System.out.println("1.Add an employee");
System.out.println("2.Display All");
System.out.println("5.Exit");
menuChoice=objectScanner.nextInt();
try
{
switch(menuChoice)
{
case 1:
getEmployeeInformationFromConsole();
break;
case 2:
displayFromList(objectEmployeeList);
break;
case 3:
writeEmployeesToFile(objectScanner.next());
break;
case 4:
readEmployeesFromFile(objectScanner.next());
break;
case 5:
System.exit(0);
}
}
catch(InputMismatchException e)
{
System.out.println("Exception is----->"+e);
}
}
{
FileOutputStream fileStreamObject;
try {
objectWriterToFile.writeObject(objectEmployeeList);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
{
String status=null;
if(!fileobject.exists())
{
return status;
}
FileInputStream fileStreamObject;
try {
objectEmployeeList.clear();
objectEmployeeList=(List<Employee>)objectReaderFromFile.readObject();
status=displayFromList(objectEmployeeList);
if(status.equalsIgnoreCase("Displayed Successfully"))
{
}
else
{
status="";
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
return status;
}
void getEmployeeInformationFromConsole()
{
try{
int id=objectScanner.nextInt();
String name=objectScanner.next();
int age=objectScanner.nextInt();
}
catch(InputMismatchException e)
{
System.out.println(""+e);
}
}
{
String status="";
try{
while(objectIterator.hasNext())
{
System.out.println(""+objectIterator.next());
}
status="Displayed Successfully";
return status;
}
catch(Exception e)
{
status="Failure";
return status;
}
}
}
{
FileOutputStream fileStreamObject;
try {
objectWriterToFile.writeObject(objectEmployeeList);
e.printStackTrace();
e.printStackTrace();
}
}
{
String status;
if(!fileobject.exists())
{
return status;
}
FileInputStream fileStreamObject;
try {
objectEmployeeList.clear();
objectEmployeeList=(List<Employee>) objectReaderFromFile.readObject();
status=displayFromList(objectEmployeeList);
if(status.equalsIgnoreCase("Displayed Successfully"))
{
}
else
{
status=""
}
e.printStackTrace();
e.printStackTrace();
}
finally
{
}
}
(Getting employee information from console and display information for specific name):-
void getEmployeeInformationFromConsole()
{
try{
int id=objectScanner.nextInt();
int age=objectScanner.nextInt();
}
catch(InputMismatchException e)
{
System.out.println(""+e);
}
}
{
String status="";
try{
while(objectIterator.hasNext())
{
System.out.println (""+objectIterator.next());
}
status="Displayed Successfully";
return status;
}
catch(Exception e)
{
status="Failure";
return status;
}
}
{
String status="";
while(objectIterator.hasNext())
{
if(objectLocalEmployee.getName().equalsIgnoreCase("1"))
{
System.out.println (""+objectLocalEmployee);
}
}
}
4. Result/Output/Writing Summary:
5. Learning outcomes (What I have learnt):
1. To solve using methods, to create a separate method for all input data.