0% found this document useful (0 votes)
7 views12 pages

Experiment

The document outlines a practical experiment to create a menu-based Java application for managing employee records. The application allows users to add employee details, display all employees, and exit the program, with functionalities for file input/output. It includes code snippets for the implementation and emphasizes learning outcomes related to method creation and user input handling.

Uploaded by

Prajanya Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Experiment

The document outlines a practical experiment to create a menu-based Java application for managing employee records. The application allows users to add employee details, display all employees, and exit the program, with functionalities for file input/output. It includes code snippets for the implementation and emphasizes learning outcomes related to method creation and user input handling.

Uploaded by

Prajanya Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Experiment -1.

4
Student Name: UID:
Branch: Section/Group
Semester: Date of Performance:
Subject Name Subject Code:

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:

1. Create menu absed java application

2. Add employee

3. Display

4. switch option

3. Apparatus(For applied/experimental sciences/materials based labs):


4. Algorithm/Flowchart (For programming based labs):

5. Theme/Interests definition( For creative domains):

6. Steps for experiment/practical:


package com.EM.Model;
import java.io.serializable;
public class employee implements Serializable {
private int id;
private String name;
private double salary;
private int getid() {
return id; }
public void setld(int id) {
this.id = id;
}
public string getName() {
return name;

}
@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;
}
}
}

7. Observations/Discussions(For applied/experimental sciences/materials based labs):

8. Percentage error (if any or applicable):


9. Calculations/ Chemical Reactions / Theorems /Formulas used etc :

10. Result/Output/Writing Summary:

11. Graphs (If Any): Image /Soft copy of graph paper to be attached here

Learning outcomes (What I have learnt):


1. To solve using methods, to create a separate method for all input
data.

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):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like