0% found this document useful (0 votes)
4 views

Java q1

The document contains a Java program that implements an ArrayList to manage employee details including ID, Name, and Salary. It provides functionalities to add, update, remove, and search for employees, along with a display option for all employees. The program runs in a loop allowing users to choose actions until they decide to exit.

Uploaded by

vishalbhateria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java q1

The document contains a Java program that implements an ArrayList to manage employee details including ID, Name, and Salary. It provides functionalities to add, update, remove, and search for employees, along with a display option for all employees. The program runs in a loop allowing users to choose actions until they decide to exit.

Uploaded by

vishalbhateria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name – Sunidhi Goyal

UID – 22BCS16557
Section and Group – IoT_617 (B)

Q.1. Write a Java program to implement an ArrayList that stores employee


details (ID, Name, and Salary). Allow users to add, update, remove, and
search employees.

import java.util.*;

class Employee {
int id;
String name;
double salary;

Employee(int id, String name, double salary) {


this.id = id;
this.name = name;
this.salary = salary;
}

@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Salary: " + salary;
}
}

public class EmployeeManager {


static ArrayList<Employee> employees = new ArrayList<>();
static Scanner scanner = new Scanner(System.in);

public static void addEmployee() {


System.out.print("Enter Employee ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Salary: ");
double salary = scanner.nextDouble();
employees.add(new Employee(id, name, salary));
System.out.println("Employee Added Successfully!");
}

public static void updateEmployee() {


System.out.print("Enter Employee ID to Update: ");
int id = scanner.nextInt();
for (Employee emp : employees) {
if (emp.id == id) {
scanner.nextLine();
System.out.print("Enter New Name: ");
emp.name = scanner.nextLine();
System.out.print("Enter New Salary: ");
emp.salary = scanner.nextDouble();
System.out.println("Employee Updated Successfully!");
return;
}
}
System.out.println("Employee Not Found!");
}

public static void removeEmployee() {


System.out.print("Enter Employee ID to Remove: ");
int id = scanner.nextInt();
employees.removeIf(emp -> emp.id == id);
System.out.println("Employee Removed Successfully!");
}

public static void searchEmployee() {


System.out.print("Enter Employee ID to Search: ");
int id = scanner.nextInt();
for (Employee emp : employees) {
if (emp.id == id) {
System.out.println(emp);
return;
}
}
System.out.println("Employee Not Found!");
}

public static void displayEmployees() {


if (employees.isEmpty()) {
System.out.println("No Employees Found!");
} else {
for (Employee emp : employees) {
System.out.println(emp);
}
}
}

public static void main(String[] args) {


while (true) {
System.out.println("\n1. Add Employee\n2. Update Employee\n3.
Remove Employee\n4. Search Employee\n5. Display All Employees\n6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1 -> addEmployee();
case 2 -> updateEmployee();
case 3 -> removeEmployee();
case 4 -> searchEmployee();
case 5 -> displayEmployees();
case 6 -> {
System.out.println("Exiting...");
System.exit(0);
}
default -> System.out.println("Invalid Choice! Try Again.");
}
}
}
}
Output:

You might also like