Q Search or enter website name CIS 23448 - Homework 4 - Spring 2018 Due Date: Ap
ID: 3701583 • Letter: Q
Question
Q Search or enter website name CIS 23448 - Homework 4 - Spring 2018 Due Date: April 10h 2018 Exercise 1 of 1: Write a program that simulates a very simple HR application for a company. Your program should be able to 1. List all the current employees 2. Add a new employee 3. Remove a current employee 4. (Extra credit: Edit current employee) You will need to create a class to instantiate a Person object (or Employee object) In the beginning, the console output should ask the user what they want to do (list, add, remove) The program should keep all the employees in an appropriate storage object of your choice (List Collection, Array, etc.), but make sure it is not limited to a maximum amount of people. The Employee class should have at minimum the following fields First Name Last Name Id . Date of Birth . Position . SalaryExplanation / Answer
NOTE: I am not sure why you needed sql dependency in your project, but here is the pom.xml which is used for building the project. The pom.xml contains sql dependency.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hrapplication.example</groupId>
<artifactId>hrapplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hrapplication</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</project>
App.java
package com.hrapplication.example.hrapplication;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
EmployeesCollection empCol = new EmployeesCollection();
int choice = 0;
Scanner sc = new Scanner(System.in);
while (true) {
displayMenu();
choice = Integer.parseInt(sc.next());
switch (choice) {
case 1: {
if (empCol.getTotalEmployees() > 0)
empCol.listAllEmployees();
else
System.out.println("There are no employees to list");
break;
}
case 2: {
Employee emp = EmployeesCollection.createAnEmployee();
empCol.addEmployee(emp);
break;
}
case 3: {
System.out.println("Enter index of the employee to remove");
int idx = sc.nextInt();
Employee emp = empCol.removeEmployeeByIndex(idx);
if (emp == null) {
System.out.println("Removing operation failed. Given index is out of bound.");
} else {
System.out.println("Given employee has been removed");
}
break;
}
case 4: {
System.out.println("Enter id of the employee to remove");
String id = sc.nextLine().trim();
Employee emp = empCol.removeEmployeeByID(id);
if (emp == null) {
System.out.println("Removing operation failed. Given index is out of bound.");
} else {
System.out.println("Given employee has been removed");
}
break;
}
case 5: {
System.out.println("Enter index of the employee to update");
int idx = sc.nextInt();
if (empCol.updateCurrentEmployeeDetails(idx)) {
System.out.println("Your employee has been updated");
} else {
System.out.println("Update employee failed. Given index is out of bound");
}
break;
}
case 6: {
System.out.println("Exiting the application.");
sc.close();
System.exit(0);
break;
}
default : {
System.out.println("Chosen option is not correct");
break;
}
}
}
}
private static void displayMenu() {
System.out.println("***Options***");
System.out.println("1. List all the employees");
System.out.println("2. Add a new employee");
System.out.println("3. Remove a current employee by index");
System.out.println("4. Remove a current employee by id");
System.out.println("5. Edit a current employee info by index");
System.out.println("6. Exit the application");
}
}
EmployeesCollection.java
package com.hrapplication.example.hrapplication;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EmployeesCollection {
private List<Employee> employeeList = new ArrayList<Employee>();
public void listAllEmployees() {
System.out.println("FirstName LastName ID BirthDate Position Salary");
for (Employee e : employeeList) {
System.out.println(e.getFirstName() + " " + e.getLastName() + " " + e.getId() + " " + e.getBirthDate() + " "
+ e.getPosition() + " " + e.getSalary());
}
}
public void addEmployee(Employee e) {
this.employeeList.add(e);
}
public Employee removeEmployeeByIndex(int idx) {
if (idx >= this.getTotalEmployees())
return null;
else
return this.employeeList.remove(idx);
}
public Employee removeEmployeeByID(String id) {
int totalEmp = getTotalEmployees();
int idx = -1;
for (int i = 0; i < totalEmp; i++) {
Employee e = this.employeeList.get(i);
if (e.getId().equals(id)) {
idx = i;
break;
}
}
if (idx != -1)
return this.removeEmployeeByIndex(idx);
else
return null;
}
public boolean updateCurrentEmployeeDetails(int idx) {
if (idx >= this.employeeList.size())
return false;
Employee emp = createAnEmployee();
employeeList.set(idx, emp);
return true;
}
public static Employee createAnEmployee() {
Employee e = new Employee();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the credentials of the employee");
System.out.println("Enter the first name: ");
String firstName = sc.nextLine().trim();
e.setFirstName(firstName);
System.out.println("Enter the last name: ");
String lastName = sc.nextLine().trim();
e.setLastName(lastName);
System.out.println("Enter the unique id of the employee: ");
String id = sc.nextLine().trim();
e.setId(id);
System.out.println("Enter the birth date: ");
String birthDate = sc.nextLine().trim();
e.setBirthDate(birthDate);
System.out.println("Enter the position(designation): ");
String position = sc.nextLine().trim();
e.setPosition(position);
System.out.println("Enter the salary: ");
String salary = sc.nextLine().trim();
e.setSalary(salary);
return e;
}
public int getTotalEmployees() {
return this.employeeList.size();
}
}
Employee.java
package com.hrapplication.example.hrapplication;
public class Employee {
String firstName;
String lastName;
String id;
String birthDate;
String position;
String salary;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Employee [firstName=");
builder.append(firstName);
builder.append(", lastName=");
builder.append(lastName);
builder.append(", id=");
builder.append(id);
builder.append(", birthDate=");
builder.append(birthDate);
builder.append(", position=");
builder.append(position);
builder.append(", salary=");
builder.append(salary);
builder.append("]");
return builder.toString();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.