2.4java 20BCS5253

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Experiment – 2.

Student Name : Ketan Raj UID : 20BCS5253


Section/Group : 20BCS_KRG_WM-1 (A) Branch : CSE
Date of Performance : 11th October 2022 _ Semester : 5th
Subject Name : Project Based Learning in Java Lab Subject Code : 20CSP – 321

1. Aim:

Employee Management System: Create a menu based Java application with the following options.

1. Add an Employee

2. Display All

3. 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. Code for the program to be written:

import java.util.*;

class Employee {
int employeeId;
String employeeName;
int employeeAge;
int employeeSalary;
Employee(int id, String name, int age, int salary) {
employeeId = id;
employeeName = name;
employeeAge = age;
employeeSalary = salary;
}
}

public class Main {


public static void main(String[] args) {
ArrayList < Employee > data = new ArrayList < > ();
Scanner sc = new Scanner(System.in);
System.out.println("Select one of the following options!!");
Boolean quit = false;
while (true) {
System.out.println("1. Add an Employee");
System.out.println("2. Display All");
System.out.println("3. Exit");
System.out.print("Please enter your choice: ");
int option_selected = sc.nextInt();
switch (option_selected) {
case 1: {
System.out.print("Please enter employee id: ");
boolean flag = true;
int id = sc.nextInt();
for (Employee e: data) {
if (e.employeeId == id) {
flag = false;
System.out.println("Employee id already exist!");
}
}
if (flag) {
System.out.print("Please enter employee name: ");
sc.nextLine();
String name = sc.nextLine();
System.out.print("Please enter employee age: ");
int age = sc.nextInt();
System.out.print("Please enter employee salary: ");
int salary = sc.nextInt();
data.add(new Employee(id, name, age, salary));
}
break;
}
case 2: {
System.out.println("---------------Report---------------");
for (Employee e: data) {
System.out.println(e.employeeId + " " + e.employeeName + " " +
e.employeeAge + " " + e.employeeSalary);
}
System.out.println("-------------End of Report----------");
break;
}
case 3: {
quit = true;
break;
}
default: {
System.out.println("Invalid number! Please enter valid number.");
}
}
if (quit)
break;
}
}
}
3. Output Screenshots:

You might also like