a) Write a simple program to store some Employee objects in an ArrayList. Then display all the details of the objects in the ArrayList. Use the JOptionPane class for output.
b) Use the JOptionPane class to display only the name and surname of employees whose surname starts with the letter ‘M’. (Ensure that you have at least 2 such employees in the list).
Advanced: modify this functionality so that the start letter is not hard-coded, but the user can specify any letter, using the JOptionPane class for input.
c) Remove all employees whose salary is less than or equal to R5000. Then, using the JOptionPane class, display all the details of the remaining objects in the list.
d) Try to sort the contents of the list in alphabetical order of surname. Then, using the JOptionPane class, display all the details of the objects in the list.
e) Try to sort the contents of the list in ascending order of salary. Then, using the JOptionPane class, display all the details of the objects in the list.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.swing.JOptionPane;
class Employee {
private String name;
private String surname;
private double salary;
public Employee(String name, String surname, double salary) {
this.name = name;
this.surname = surname;
this.salary = salary;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the surname
*/
public String getSurname() {
return surname;
}
/**
* @param surname the surname to set
*/
public void setSurname(String surname) {
this.surname = surname;
}
/**
* @return the salary
*/
public double getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(double salary) {
this.salary = salary;
}
}
class App {
public static void main(String[] args) {
// a) Write a simple program to store some Employee objects in an ArrayList.
// Then display all the details of the objects in the ArrayList. Use the
// JOptionPane class for output.
ArrayList<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("Peter", "Smith", 25000));
employees.add(new Employee("Mike", "Marshall", 2500));
employees.add(new Employee("Mary", "Morris", 3000));
employees.add(new Employee("Peter", "Peterson", 5000));
employees.add(new Employee("Julia", "Johnson", 15000));
String allEmployees = "";
for (int i = 0; i < employees.size(); i++) {
allEmployees += "Name: " + employees.get(i).getName() + "\n";
allEmployees += "Surname: " + employees.get(i).getSurname() + "\n";
allEmployees += "Salary: R" + employees.get(i).getSalary() + "\n\n";
}
JOptionPane.showMessageDialog(null, allEmployees);
// b) Use the JOptionPane class to display only the name and surname of
// employees whose surname starts with the letter ‘M’. (Ensure that you have at
// least 2 such employees in the list).
String startLetter = "M";
allEmployees = "";
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getSurname().toLowerCase().startsWith(startLetter.toLowerCase())) {
allEmployees += "Name: " + employees.get(i).getName() + "\n";
allEmployees += "Surname: " + employees.get(i).getSurname() + "\n\n";
}
}
JOptionPane.showMessageDialog(null, allEmployees);
// Advanced: modify this functionality so that the start letter is not
// hard-coded, but the user can specify any letter, using the JOptionPane class
// for input.
startLetter = JOptionPane.showInputDialog("Enter the start letter of surname: ");
allEmployees = "";
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getSurname().toLowerCase().startsWith(startLetter.toLowerCase())) {
allEmployees += "Name: " + employees.get(i).getName() + "\n";
allEmployees += "Surname: " + employees.get(i).getSurname() + "\n\n";
}
}
JOptionPane.showMessageDialog(null, allEmployees);
// c) Remove all employees whose salary is less than or equal to R5000. Then,
// using the JOptionPane class, display all the details of the remaining objects
// in the list.
for (int j = 0; j < employees.size(); j++) {
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getSalary() <= 5000) {
employees.remove(i);
}
}
}
allEmployees = "";
for (int i = 0; i < employees.size(); i++) {
allEmployees += "Name: " + employees.get(i).getName() + "\n";
allEmployees += "Surname: " + employees.get(i).getSurname() + "\n";
allEmployees += "Salary: R" + employees.get(i).getSalary() + "\n\n";
}
JOptionPane.showMessageDialog(null, allEmployees);
// d) Try to sort the contents of the list in alphabetical order of surname.
// Then, using the JOptionPane class, display all the details of the objects in
// the list.
Comparator<Employee> listInAlphabeticalOrderSurname = (o1, o2) -> o1.getSurname().compareTo(o2.getSurname());
Collections.sort(employees, listInAlphabeticalOrderSurname);
allEmployees = "The list in alphabetical order of surname:\n";
for (int i = 0; i < employees.size(); i++) {
allEmployees += "Name: " + employees.get(i).getName() + "\n";
allEmployees += "Surname: " + employees.get(i).getSurname() + "\n";
allEmployees += "Salary: R" + employees.get(i).getSalary() + "\n\n";
}
JOptionPane.showMessageDialog(null, allEmployees);
// e) Try to sort the contents of the list in ascending order of salary. Then,
// using the JOptionPane class, display all the details of the objects in the
// list.
employees.sort((o1, o2) -> Double.compare(o1.getSalary(), o2.getSalary()));
allEmployees = "The list in ascending order of salary:\n";
for (int i = 0; i < employees.size(); i++) {
allEmployees += "Name: " + employees.get(i).getName() + "\n";
allEmployees += "Surname: " + employees.get(i).getSurname() + "\n";
allEmployees += "Salary: R" + employees.get(i).getSalary() + "\n\n";
}
JOptionPane.showMessageDialog(null, allEmployees);
}
}
Comments
Leave a comment