I am constructing a employee and project management application which i am not able to get to run because of 1 error left. The error is on line 147 (which is in case 3 of my switch statement "string role = "sample role") i have a local variable role error can someone please help me fix this. I am quite new to coding so apologies if this is a simple mistake
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
class Employee {
private static int employeeIDCounter = 1;
private int employeeID;
private String firstName;
private String lastName;
private String department;
private String role;
public Employee(String firstName, String lastName, String department, String role) {
this.employeeID = employeeIDCounter++;
this.firstName = firstName;
this.lastName = lastName;
this.department = department;
this.role = role;
}
// Getters for Employee properties
public int getEmployeeID() {
return employeeID;
}
public String getFullName() {
return firstName + " " + lastName;
}
public String getRole() {
return role;
}
public String getDepartment() {
return department;
}
}
class Project {
private static int projectIDCounter = 1;
private int projectID;
private String projectName;
private String projectDescription;
private String startDate;
private String endDate;
private Map<Employee, String> assignedTeam;
public Project(String projectName, String projectDescription, String startDate, String endDate) {
this.projectID = projectIDCounter++;
this.projectName = projectName;
this.projectDescription = projectDescription;
this.startDate = startDate;
this.endDate = endDate;
this.assignedTeam = new HashMap<>();
}
// Getters for Project properties
public int getProjectID() {
return projectID;
}
public String getProjectName() {
return projectName;
}
public String getProjectDescription() {
return projectDescription;
}
public String getStartDate() {
return startDate;
}
public String getEndDate() {
return endDate;
}
public Map<Employee, String> getAssignedTeam() {
return assignedTeam;
}
public void assignEmployee(Employee employee, String role) {
assignedTeam.put(employee, role);
}
}
public class ProjectEmployeeManagement {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
List<Project> projects = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Choose operation:");
System.out.println("1. Add Project");
System.out.println("2. Add Employee");
System.out.println("3. Assign Employee to Project");
System.out.println("4. Display Projects and Team Members");
System.out.println("5. Search Employees for Project");
System.out.println("6. Search Projects for Employee");
System.out.println("7. Update Project or Employee");
System.out.println("8. Delete Project");
System.out.println("9. Exit");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
// Add Project
System.out.println("Enter project details:");
System.out.print("Project Name:");
String projectName = scanner.nextLine();
System.out.print("project Description: ");
String projectDescription = scanner.nextLine();
System.out.print("Start Date: ");
String startDate = scanner.nextLine ();
System.out.print(" End Date: ");
String endDate = scanner.nextLine();
Project newProject = new Project(projectName, projectDescription, startDate, endDate);
projects.add(newProject);
break;
case 2:
// Add Employee
System.out.println("Enter employee details:");
System.out.print("First Name: ");
String firstName = scanner.nextLine();
System.out.print("Last Name: ");
String lastName = scanner.nextLine();
System.out.print("Department: ");
String department = scanner.nextLine();
System.out.print("Role: ");
String role = scanner.nextLine();
Employee newEmployee = new Employee(firstName, lastName, department, role);
employees.add(newEmployee);
break;
case 3:
// Assign Employee to Project
System.out.println("Enter Project ID: ");
int projectID = scanner.nextInt();
System.out.println("Enter Employee ID: ");
int employeeID = scanner.nextInt();
scanner.nextLine();
String role = "Sample Role"; // Replace with actual role input
Project projectToAssign = null;
Employee employeeToAssign = null;
// Find the project and employee objects
for (Project project : projects) {
if (project.getProjectID() == projectID) {
projectToAssign = project;
break;
}
}
for (Employee employee : employees) {
if (employee.getEmployeeID() == employeeID) {
employeeToAssign = employee;
break;
}
}
// Assign the employee to the project with the specified role
if (projectToAssign != null && employeeToAssign != null) {
projectToAssign.assignEmployee(employeeToAssign, role);
} else {
System.out.println("Project or Employee not found.");
}
break;
case 4:
// Display Projects and Team Members
for (Project project : projects) {
System.out.println("Project ID: " + project.getProjectID());
System.out.println("Project Name: " + project.getProjectName());
System.out.println("Project Description: " + project.getProjectDescription());
System.out.println("Start Date: " + project.getStartDate());
System.out.println("End Date: " + project.getEndDate());
System.out.println("Assigned Team Members:");
for (Map.Entry<Employee, String> entry : project.getAssignedTeam().entrySet()) {
System.out.println("Employee ID: " + entry.getKey().getEmployeeID());
System.out.println("Employee Name: " + entry.getKey().getFullName());
System.out.println("Role: " + entry.getValue());
}
}
break;
case 5:
// Search Employees for Project
System.out.println("Enter Project ID: ");
int searchProjectID = scanner.nextInt();
Project searchProject = null;
// Find the project object
for (Project project : projects) {
if (project.getProjectID() == searchProjectID) {
searchProject = project;
break;
}
}
if (searchProject != null) {
System.out.println("Assigned Team Members for Project " + searchProjectID + ":");
for (Map.Entry<Employee, String> entry : searchProject.getAssignedTeam().entrySet()) {
System.out.println("Employee ID: " + entry.getKey().getEmployeeID());
System.out.println("Employee Name: " + entry.getKey().getFullName());
System.out.println("Role: " + entry.getValue());
}
} else {
System.out.println("Project not found.");
}
break;
case 6:
// Search Projects for Employee
System.out.println("Enter Employee ID: ");
int searchEmployeeID = scanner.nextInt();
Employee searchEmployee = null;
// Find the employee object
for (Employee employee : employees) {
if (employee.getEmployeeID() == searchEmployeeID) {
searchEmployee = employee;
break;
}
}
if (searchEmployee != null) {
System.out.println("Projects assigned to Employee " + searchEmployeeID + ":");
for (Project project : projects) {
for (Map.Entry<Employee, String> entry : project.getAssignedTeam().entrySet()) {
if (entry.getKey().getEmployeeID() == searchEmployeeID) {
System.out.println("Project ID: " + project.getProjectID());
System.out.println("Project Name: " + project.getProjectName());
System.out.println("Role: " + entry.getValue());
}
}
}
} else {
System.out.println("Employee not found.");
}
break;
case 7:
// Update Project or Employee
System.out.println("Enter 'P' to update a Project or 'E' to update an Employee: ");
String updateChoice = scanner.nextLine();
if (updateChoice.equalsIgnoreCase("P")) {
// Update Project
System.out.println("Enter Project ID to update: ");
int projectIDToUpdate = scanner.nextInt();
Project projectToUpdate = null;
// Find the project object
for (Project project : projects) {
if (project.getProjectID() == projectIDToUpdate) {
projectToUpdate = project;
break;
}
}
if (projectToUpdate != null) {
// Here you can implement the logic to update project details
// You can ask for new project details (name, description, dates, etc.)
System.out.println("Project updated successfully.");
} else {
System.out.println("Project not found.");
}
} else if (updateChoice.equalsIgnoreCase("E")) {
// Update Employee
System.out.println("Enter Employee ID to update: ");
int employeeIDToUpdate = scanner.nextInt();
Employee employeeToUpdate = null;
// Find the employee object
for (Employee employee : employees) {
if (employee.getEmployeeID() == employeeIDToUpdate) {
employeeToUpdate = employee;
break;
}
}
if (employeeToUpdate != null) {
// Here you can implement the logic to update employee details
// You can ask for new employee details (first name, last name, department, role, etc.)
System.out.println("Employee updated successfully.");
} else {
System.out.println("Employee not found.");
}
} else {
System.out.println("Invalid choice.");
}
break;
case 8:
// Delete Project
System.out.println("Enter Project ID to delete: ");
int projectIDToDelete = scanner.nextInt();
Project projectToDelete = null;
// Find the project object
for (Project project : projects) {
if (project.getProjectID() == projectIDToDelete) {
projectToDelete = project;
break;
}
}
if (projectToDelete != null) {
projects.remove(projectToDelete);
System.out.println("Project deleted successfully.");
} else {
System.out.println("Project not found.");
}
break;
case 9:
// Exit the application
System.out.println("Exiting the application.");
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please choose a valid operation.");
}
}
}
}
In java, local variables are 'scoped' to the nearest pair of braces when looking to its immediate surroundings. The variable exists throughout all of it (even if you declare it halfway down a block), and doesn't exist outside of it.
In addition, you are not allowed to declare the same variable more than once. That is simply an error. This:
doesn't compile (the first line decrees 'I hereby notify you that there shall exist a variable named
x', and the second line makes the same decree, that's the problem. Justx = "bar";is fine, it's theString x = "bar";that's the problem).That rule is literally how it works, and that means your basic switch blocks are really bizarre. All of it is within a single pair of braces so any variable you declare anywhere inside a switch counts for the whole switch block.
Your case, therefore, boils down to the above case: In a single block, you wrote:
Solutions
One simple-ish solution is to just add braces to each case. Instead of:
you write:
Now you no longer need to worry about name collisions between your case blocks.
Alternatively, you can just declare
String role;once before the switch block, and then never declare it again (just writerole = scanner.nextLine();). I don't recommend this action because you're now messing up the outer scope with an unnecessary variable.A third solution is to ensure each variable you declare in an entire switch block is unique. I don't recommend this action because you'll inevitably name your variables
role0,role1, and that rather quickly and obviously becomes a needless maintenance headache.