You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the createEmployee(), the variable String fname's value is not being carried when i call it to print in the later statements and possibly it is not being passed to the employee service's createEmployee() method - why?
the fname variable works fine when when asked to confirm details and is also visible when called in the seeAllEmployees method.
In the updateDetailsOfCurrentEmployee() and the deleteEmployee() methods, it is not asking for the Employee-id. Already takes in the null value - why? I have not put in a not null condition. Is it because I have not made it final?
When I try to make the employee-id variable final, shows me the variable might not have been initialized error. However, I am initializing it in the constructor. - what is the solution?
Including all the classes below:
//Creating the base model class defining the fields and methods for the employee object.
//since i have set the fields final we cannot use the setter methods but i am running short of ideas since there are certain fields that need to be updated.
//Any other option other than setters?
public class Employee {
private final String fName;
private final String lName;
private final int age;
private final String phoneNumber;
private String workEmail;
private String personalEmail;
private String employee_ID;
//first creating a singleton instance so that when the application is run multiple times, there aren't several instances created but instead only one entry
// point for the program in the system
private static final EmployeeService employeeServiceInstance = new EmployeeService();
private EmployeeService(){}
public static EmployeeService getEmployeeServiceInstance() {
return employeeServiceInstance;
}
Map<String, Employee> employees = new HashMap<>();
//Since we are creating a newly creating a new employee he cannot have an employee_id or a work email address. We created the below method to assign to the
// newly created employee a unique id and work-email address
private String[] createEmployeeIDAndEmail(String fname, String lname){
Random random = new Random();
int count = random.nextInt(111,99999);
String[] empData = new String[2];
if(fname != null && lname != null){
empData[0] = Integer.toString(random.nextInt(0,999999999));
empData[1] = fname+"."+lname+count+"@learnermedia.com";
}
return empData;
}
public void createAnEmployee(String fname, String lname, int age, String phoneNumber, String personalEmail){
String[] localEmpInstance = createEmployeeIDAndEmail(fname,lname);
employees.put(localEmpInstance[0], new Employee(fname, lname, age,phoneNumber,personalEmail,localEmpInstance[0],localEmpInstance[1]));
}
public Employee getEmployeeDetails(String employee_ID){
return employees.get(employee_ID);
}
//facing issues in this method as I cannot work out the logic. What I want the method to do is that when update details method from the Employee Menu class is
//called, the new details for an existing employee are updated. One way i thought of is the setter methods but for that i will have to make the fields of the
//employee class non-final. Is that a good practice?
//What I was trying to do int the belo method is, I was matching the present name with the updated name and if they were different,
//then for the same employee id, I would have passed the new value for the particular field while keeping the rest of the fields unchanged.
//I thought of a way I can work this out, but it will be a long way
public void updateDetails(String newFirstName, String newLastName, int newAge, String newPhoneNumber, String newPersonalEmail, String emp_id, String newWorkEmail){
Employee updatedEmployee = employees.get(emp_id);
if(!updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
updatedEmployee.getAge()== newAge && updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail)
&& updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail) && updatedEmployee.getEmployee_ID().equalsIgnoreCase(emp_id)) {
//is there a way that only the new value for a particular field is updated while the rest remain the same.
// One is the setters but then i will have to make the fields non-final. is that a good practice?
//new First Name
employees.put(emp_id,new Employee(newFirstName,updatedEmployee.getlName(),updatedEmployee.getAge(),updatedEmployee.getPhoneNumber(),
updatedEmployee.getPersonalEmail(),updatedEmployee.getWorkEmail(),updatedEmployee.getEmployee_ID()));
//repeat checking for all the fields. :D
//new Last Name
}else if(updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && !updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
updatedEmployee.getAge()== newAge && updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail)
&& updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail) && updatedEmployee.getEmployee_ID().equalsIgnoreCase(emp_id)) {
employees.put(emp_id, new Employee(updatedEmployee.getfName(), newLastName, updatedEmployee.getAge(), updatedEmployee.getPhoneNumber(),
updatedEmployee.getPersonalEmail(), updatedEmployee.getWorkEmail(), updatedEmployee.getEmployee_ID()));
}
//New Age. Knowing it can be derived from the DOB as well but since I do not have DOB field we will go this way.
else if(updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
updatedEmployee.getAge() != newAge && updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail)
&& updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail) && updatedEmployee.getEmployee_ID().equalsIgnoreCase(emp_id)){
employees.put(emp_id,new Employee(updatedEmployee.getfName(), updatedEmployee.getlName(),newAge,updatedEmployee.getPhoneNumber(),
updatedEmployee.getPersonalEmail(),updatedEmployee.getWorkEmail(),updatedEmployee.getEmployee_ID()));
}
//New Phone Number
else if(updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
updatedEmployee.getAge()== newAge && !updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail)
&& updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail) && updatedEmployee.getEmployee_ID().equalsIgnoreCase(emp_id)){
employees.put(emp_id,new Employee(updatedEmployee.getfName(), updatedEmployee.getlName(), updatedEmployee.getAge(),newPhoneNumber,
updatedEmployee.getPersonalEmail(),updatedEmployee.getWorkEmail(),updatedEmployee.getEmployee_ID()));
}
//New Personal Email address
else if(updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && !updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
updatedEmployee.getAge() == newAge && updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && !updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail)
&& updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail) && updatedEmployee.getEmployee_ID().equalsIgnoreCase(emp_id)){
employees.put(emp_id,new Employee(updatedEmployee.getfName(), updatedEmployee.getlName(), updatedEmployee.getAge(),updatedEmployee.getPhoneNumber(),
newPersonalEmail,updatedEmployee.getWorkEmail(),updatedEmployee.getEmployee_ID()));
}
//New Work Email address
else if(updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && !updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
updatedEmployee.getAge()== newAge && updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail)
&& !updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail) && updatedEmployee.getEmployee_ID().equals(emp_id)) {
employees.put(emp_id, new Employee(updatedEmployee.getfName(), updatedEmployee.getlName(), updatedEmployee.getAge(), updatedEmployee.getPhoneNumber(),
newPersonalEmail, newWorkEmail, updatedEmployee.getEmployee_ID()));
}
//Even though the above work-around works, I feel that this is not the correct way. This was possible only because I have 6 updatable fields.
// This is an unfeasible solution where there are large number of fields. Keeping that in mind, what would be the least costly approach.
}
public void deleteAnEmployee(String emp_id){
//just learned that this will possibly only delete the key. What about the values? Is there a way where, by passing the key, the key
//and its corresponding values are also deleted?
employees.remove(emp_id);
System.out.println("Employee details have been deleted.");
}
public Collection<Employee> getAllEmployees(){
return employees.values();
}
String menu = """
Welcome to Employee Management System
-------------------------------------
1. Create an Employee
2. See all Employees
3. Update details of existing employee
4. Delete an employee details
5. Exit
---------------------------------------
Please select the desired number from the above menu\040
""";
Scanner scan = new Scanner(System.in);
public void showMenu(){
int choice;
System.out.println(menu);
while(scan.hasNext()){
choice = scan.nextInt();
switch (choice){
case 1 -> createEmployees(scan);
case 2 -> seeAllEmployees();
case 3 -> updateDetailsOfCurrentEmployee(scan);
case 4 -> deleteEmployee(scan);
case 5 -> leaveApplication();
default -> System.out.println("Number chosen out of range. Please select a number between 1 and 5.");
}
}
showMenu();
}
public void createEmployees(Scanner scan){
try {
System.out.println("Please enter your first name: ");
String fname = scan.nextLine().strip();//the first name is not being printed on the screen. Why?
scan.nextLine();
System.out.println("Please enter your last name: ");
String lname = scan.nextLine().strip();
System.out.println("Please enter your age: ");
int age = scan.nextInt();
System.out.println("Please enter your 10-digit phone number: ");
String phoneNumber = scan.next();
phoneNumber = checkPhoneNumberLength(phoneNumber);
scan.nextLine();
System.out.println("Please enter your personal email address: ");
String personalEmail = scan.nextLine();
System.out.println("Please confirm the fields entered: \n" +
"Name: " + fname+" "+lname +
"\nAge: " + age +
"\nPhone Number: " + phoneNumber +
"\n Alternate Email: " + personalEmail);
System.out.println("All the details are confirmed?(y/n): ");
char userChoice = scan.nextLine().charAt(0);
EmployeeService employeeService = getEmployeeServiceInstance();
if (userChoice == 'y') {
employeeService.createAnEmployee(fname, lname, age, phoneNumber, personalEmail);
} else if (userChoice == 'n') {
System.out.println("Which field would you like to change?");
String userAnswer = scan.nextLine();
switch (userAnswer.toLowerCase()) {
case "first name" -> {
System.out.println("Please enter the confirm first name: ");
fname = scan.nextLine();
}
case "last name" -> {
System.out.println("Please enter the confirm last name: ");
lname = scan.nextLine();
}
case "age" -> {
System.out.println("Please enter the confirm age: ");
age = scan.nextInt();
}
case "phone number" -> {
System.out.println("Please enter the confirm phone number: ");
phoneNumber = scan.nextLine();
phoneNumber = checkPhoneNumberLength(phoneNumber);
}
case "personal email" -> {
System.out.println("Please enter the confirm alternative email address: ");
personalEmail = scan.nextLine();
}
default -> System.out.println("Changes completed.");
}
employeeService.createAnEmployee(fname, lname, age, phoneNumber, personalEmail);
}
}catch (NullPointerException e){
e.getLocalizedMessage();
}
System.out.println();
showMenu();
}
private String checkPhoneNumberLength(String phNumber){
if(!phNumber.matches("[0-9]+")){
System.out.println("Invalid number");
}
if(phNumber.length() == 10){
System.out.println("Phone number accepted!");
}else{
System.out.println("Please enter the correct phone number: ");
phNumber = scan.nextLine();
}
return phNumber;
}
public void seeAllEmployees(){
EmployeeService employeeService = getEmployeeServiceInstance();
System.out.println(employeeService.getAllEmployees());
System.out.println();
showMenu();
}
public void updateDetailsOfCurrentEmployee(Scanner scan){
System.out.println("Please enter the employee id: ");
String employee_id = scan.nextLine(); //already takes a null value. Why?
EmployeeService employeeService = getEmployeeServiceInstance();
System.out.println(employeeService.getEmployeeDetails(employee_id));
System.out.println("Which field would you like to update?");
String userAnswer = scan.nextLine();
String fname = "",lname = "",personalEmail = "",wEmail ="", phoneNumber = "";
int age = 0;
switch (userAnswer.toLowerCase(Locale.ROOT)) {
case "first name" -> {
System.out.println("Please enter the confirm first name: ");
fname = scan.nextLine();
}
case "last name" -> {
System.out.println("Please enter the confirm last name: ");
lname = scan.nextLine();
}
case "age" -> {
System.out.println("Please enter the confirm age: ");
age = scan.nextInt();
}
case "phone number" -> {
System.out.println("Please enter the confirm phone number: ");
phoneNumber = scan.nextLine();
String pNumber = phoneNumber;
phoneNumber = checkPhoneNumberLength(pNumber);
}
case "personal email" -> {
System.out.println("Please enter the confirm alternative email address: ");
personalEmail = scan.nextLine();
}
case "work email" -> {
System.out.println("Please enter the correct work email address: ");
wEmail = scan.nextLine();
}
default -> System.out.println("Changes completed.");
}
employeeService.updateDetails(fname,lname,age,phoneNumber,personalEmail,employee_id,wEmail);
System.out.println("PFB the updated details: \n" +
"Name: "+fname.concat(lname)+
"\nAge: "+age+
"\nPhone Number: "+phoneNumber+
"\n Alternate Email: "+personalEmail);
System.out.println();
showMenu();
}
public void deleteEmployee(Scanner scan){
EmployeeService employeeService = getEmployeeServiceInstance();
System.out.println("Please enter the employee id of the employee to be deleted: ");
String emp_id = scan.nextLine();
System.out.println(employeeService.getEmployeeDetails(emp_id));
System.out.println("Please confirm if you want to delete the details(y/n): ");
char userChoice = scan.nextLine().charAt(0);
if(userChoice == 'y'){
employeeService.deleteAnEmployee(emp_id);
}else if(userChoice == 'n'){
System.out.println("No data deleted.");
}
System.out.println();
showMenu();
}
public void leaveApplication(){
System.exit(0);
}
}
`
EmployeeMain.java
`package main;
import menu.EmployeeMenu;
public class EmployeeMain {
public static void main(String[] args){
EmployeeMenu employeeMenu = new EmployeeMenu();
employeeMenu.showMenu();
}
}
`
The text was updated successfully, but these errors were encountered:
List of issues:
Including all the classes below:
Employee.java
`package model;
import java.util.Objects;
import java.util.regex.Pattern;
//Creating the base model class defining the fields and methods for the employee object.
//since i have set the fields final we cannot use the setter methods but i am running short of ideas since there are certain fields that need to be updated.
//Any other option other than setters?
public class Employee {
private final String fName;
private final String lName;
private final int age;
private final String phoneNumber;
private String workEmail;
private String personalEmail;
private String employee_ID;
}
`
EmployeeService.java
`package service;
import model.Employee;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class EmployeeService {
//Since we are creating a newly creating a new employee he cannot have an employee_id or a work email address. We created the below method to assign to the
// newly created employee a unique id and work-email address
private String[] createEmployeeIDAndEmail(String fname, String lname){
Random random = new Random();
int count = random.nextInt(111,99999);
String[] empData = new String[2];
if(fname != null && lname != null){
empData[0] = Integer.toString(random.nextInt(0,999999999));
empData[1] = fname+"."+lname+count+"@learnermedia.com";
}
return empData;
}
//Even though the above work-around works, I feel that this is not the correct way. This was possible only because I have 6 updatable fields.
// This is an unfeasible solution where there are large number of fields. Keeping that in mind, what would be the least costly approach.
}
}
`
EmployeeMenu.java
`package menu;
import service.EmployeeService;
import java.util.Locale;
import java.util.Scanner;
import static service.EmployeeService.getEmployeeServiceInstance;
public class EmployeeMenu {
}
`
EmployeeMain.java
`package main;
import menu.EmployeeMenu;
public class EmployeeMain {
public static void main(String[] args){
}
![Issue-1](https://user-images.githubusercontent.com/36161080/232105764-acbd7dc7-6c2a-4c82-8b0d-c4a7897b82a6.png)
![issue-2](https://user-images.githubusercontent.com/36161080/232105777-3a1f0b86-9439-4924-9094-5e6f9b357d6e.png)
`
The text was updated successfully, but these errors were encountered: