Java Employee ArrayList Menu Driven Application
Java Employee ArrayList Menu Driven Application
Here is a sample Java program that implements a menu-driven application that prompts the user to enter details for five employees and saves them in an ArrayList:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* https://mauricemuteti.info/java-employee-arraylist-menu-driven-application/(opens in a new tab)
* Java Employee ArrayList Menu Driven Application
*/
/**
*
* @author HP
*/
public class Employee {
private String name;
private double salary;
private String department;
public Employee(String name, double salary, String department) {
this.name = name;
this.salary = salary;
this.department = department;
}
@Override
public String toString() {
return "Employee{"
+ "name='" + name + '\''
+ ", salary=" + salary
+ ", department='" + department + '\''
+ '}';
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* https://mauricemuteti.info/java-employee-arraylist-menu-driven-application/(opens in a new tab)
* Java Employee ArrayList Menu Driven Application
*/
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author HP
*/
public class JavaEmployeeArrayListMenuDrivenApplication {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Employee> employees = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String input = "";
while (!input.equalsIgnoreCase("quit")) {
System.out.println("Enter the name of the employee:");
String name = scanner.nextLine();
if (name.equalsIgnoreCase("quit")) {
break;
}
System.out.println("Enter the salary of the employee:");
double salary = scanner.nextDouble();
scanner.nextLine(); // consume the remaining newline character
System.out.println("Enter the department of the employee:");
String department = scanner.nextLine();
employees.add(new Employee(name, salary, department));
if (employees.size() == 5) {
break;
}
}
System.out.println("Employee Details:");
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
This program uses a Scanner
to read input from the user and a while
loop to keep looping until the user enters “quit”. It also uses an ArrayList
to store the employee details entered by the user.
Inside the loop, the program prompts the user to enter the name, salary, and department of the employee. It then creates a new Employee
object using the input provided by the user and adds it to the ArrayList
.
Finally, the program prints out the details of all the employees in the ArrayList
.
Netbeans Output
run:
Enter the name of the employee:
jane
Enter the salary of the employee:
12
Enter the department of the employee:
IT
Enter the name of the employee:
quit
Employee Details:
Employee{name='jane', salary=12.0, department='IT'}
BUILD SUCCESSFUL (total time: 14 seconds)
You can Clone The Code From This Github Repository Link – https://github.com/mauricemuteti/Java-Employee-ArrayList-Menu-Driven-Application