Create an employee form that accepts an employee number, employee name, employee surname, employee department, gender, and salary. The form should insert the employee details in the employee database. Include a table that fetches the employee’s details from the database, deletes and updates these details on a table.
import java.util.ArrayList;
import java.util.Scanner;
class Employee {
private String number;
private String name;
private String surname;
private String department;
private String gender;
private float salary;
public Employee(String number, String name, String surname, String department, String gender, float salary) {
this.number = number;
this.name = name;
this.surname = surname;
this.department = department;
this.gender = gender;
this.salary = salary;
}
public String toString() {
return "Number: " + number + "\nName: " + name + "\nSurname: " + surname + "\nDepartment: " + department
+ "\nGender: " + gender + "\nSalary: " + salary;
}
/**
* @return the number
*/
public String getNumber() {
return number;
}
/**
* @param number the number to set
*/
public void setNumber(String number) {
this.number = number;
}
/**
* @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 department
*/
public String getDepartment() {
return department;
}
/**
* @param department the department to set
*/
public void setDepartment(String department) {
this.department = department;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}
/**
* @return the salary
*/
public float getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(float salary) {
this.salary = salary;
}
}
public class Main {
public static int getEmployeeIndex(ArrayList<Employee> employees, String number) {
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getNumber().compareTo(number) == 0) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
ArrayList<Employee> employees = new ArrayList<Employee>();
Scanner input = new Scanner(System.in);
String number;
String name;
String surname;
String department;
String gender;
float salary;
int ch = -1;
while (ch != 5) {
System.out.println("1. Add a new employee");
System.out.println("2. Display all employees");
System.out.println("3. Edit employee");
System.out.println("4. Delete a new employee");
System.out.println("5. Exit");
System.out.print("Your choice: ");
ch = input.nextInt();
input.nextLine();
switch (ch) {
case 1: {
System.out.print("Enter number: ");
number = input.nextLine();
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter surname: ");
surname = input.nextLine();
System.out.print("Enter department: ");
department = input.nextLine();
System.out.print("Enter gender: ");
gender = input.nextLine();
System.out.print("Enter salary: ");
salary = input.nextFloat();
employees.add(new Employee(number, name, surname, department, gender, salary));
}
break;
case 2: {
for (int i = 0; i < employees.size(); i++) {
System.out.println(employees.get(i).toString() + "\n");
}
}
break;
case 3: {
System.out.print("Enter number to edit: ");
number = input.nextLine();
int index = getEmployeeIndex(employees, number);
if (index != -1) {
System.out.print("Enter a new name: ");
name = input.nextLine();
System.out.print("Enter a new surname: ");
surname = input.nextLine();
System.out.print("Enter a new department: ");
department = input.nextLine();
System.out.print("Enter a new gender: ");
gender = input.nextLine();
System.out.print("Enter a new salary: ");
salary = input.nextFloat();
employees.remove(index);
employees.add(index, new Employee(number, name, surname, department, gender, salary));
System.out.println("\nEmployee has been updated\n");
} else {
System.out.println("\nEmployee does not exist\n");
}
}
break;
case 4: {
System.out.print("Enter number to delete: ");
number = input.nextLine();
int index = getEmployeeIndex(employees, number);
if (index != -1) {
employees.remove(index);
System.out.println("\nEmployee has been deleted\n");
} else {
System.out.println("\nEmployee does not exist\n");
}
}
break;
case 5: {
// exit
}
break;
default: {
}
break;
}
}
input.close();
}
}
Comments
Leave a comment