COS315 Java Homework4 KHH200

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

COS315 - Java Programming Homework 4

Kiril Hadzhyiski 200146294


11/11/2022

I hereby declare that the submitted programming homework


solutions are all
my own work. I did not copy the solutions from someone or
somewhere else.
No one but me developed the solutions. I have not allowed my
solutions to be
copied by someone else.

Date:11/11/2022
Signature: Kiril Hadzhiyski

Name:Kiril Hadzhiyski
Screenshot 1 – Employee form before modification
Screenshot 2 – Employee form after modification
I’ve change the Job Title, Salary, Location and Email

Screenshot 3 – Employee details after modification


Screenshot 4- List of employees

Screenshot 5- List of employees after deleting employee 3


Modified code in EmployeeController.java.
Also, I tried to run the program with try-catch block if there is no employee with
the id we are searching for.
package edu.aubg.employees;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@SuppressWarnings("unused")
@Controller
@RequestMapping("/employee")
public class EmployeeController {

@Autowired
private EmployeeService employeeservice;

@RequestMapping(value="/listEmployees", method=RequestMethod.GET)

/* Method to show list of employees */


public String listEmployees(Model model) {

/* Get list of employees from database */


List<Employee> employeeList = employeeservice.listAll();
/* Make the list available to the view */
model.addAttribute("employees", employeeList);

return "showListEmployees";
}

/* Two methods that work in tandem */


/* to create an new employee database record */

@RequestMapping(value="/addEmployee", method=RequestMethod.GET)

/* Method to show add new employee web form */

public String addEmployee() {

/* Send web form to browser user */

return "showAddEmployeeForm";
}

@RequestMapping(value="/addEmployeeSubmit", method=RequestMethod.POST)

/* Method to add submitted new employee details to database, and then redirect to
listEmployees */

public String addEmployeeSubmit(Employee employee) {


/* Save web form data in database */

employeeservice.save(employee);

/* Finally display list of employees – including new one! */

return "redirect:/employee/listEmployees";
}
@RequestMapping(value="/editEmployee/{id}", method=RequestMethod.GET)
/* Browser request for update employee details form */
/* Employee identified by id */
public String editEmployee(@PathVariable("id") long id, Model model) {

// Code to send form with details of employee with id to browser


model.addAttribute(employeeservice.get(id));
return "showEmployeeEditForm.html";
}

@RequestMapping(value="/editEmployeeSubmit", method=RequestMethod.POST)
/* Update database with submitted edited employee details */
public String editEmployeeSubmit(Employee employee) {

// Code to process submitted form, updating record in database


employeeservice.save(employee);

// Then invoke listEmployees method to display employee list


// by redirecting to listEmployees method
return "redirect:/employee/listEmployees";
}

@RequestMapping(value="/deleteEmployee/{id}", method=RequestMethod.GET)
/* Delete from database record of employee identified by id, and then redirect to
listEmployees */
public String deleteEmployee(@PathVariable("id") long id) {

// Code to delete record specified by id


employeeservice.delete(id);

// Then invoke listEmployees method to display employee list


// by redirecting to listEmployees method
return "redirect:/employee/listEmployees";

@RequestMapping(value="/detailsEmployee/{id}", method=RequestMethod.GET)

/* Show details of employee identified by id from database */


public String detailsEmployee(@PathVariable("id") long id, Model model) {

/* Get details of employee identified by id from database */

Employee employee;
try {
employee = employeeservice.get(id);

}
catch (Exception a) {
employee = new Employee();
}
model.addAttribute("employee", employee);
return "showEmployeeDetails";

}
Part 2
Screenshot 1- list of the employee who have a salary of 6000.

Screenshot 1- list of the employee who have a salary of 6000 in H2 database


@Query("select s from EMPLOYEES s where s.salary = 6000") List<Employee>
findEmployee();

Screenshot 3 - List of all employees living in a Sofia or Plovdiv

@Query("select s from EMPLOYEES s where s.location = 'Sofia' or s.location =


'Plovdiv'") List<Employee> findEmployee();
Screenshot 4 – List of all employees living in Sofia or Plovdiv in H2 table

Screenshot 5- List of all employees who have a salary greater than 5000 and live
in Sofia

@Query("select s from EMPLOYEES s where s.location = 'Sofia' and s.salary >5000")


List<Employee> findEmployee();
Screenshot 6 - List of all employees who have a salary greater than 5000 and live
in Sofia H2 table
Screenshot 7- List of all employees who have a salary greater than or equal to
2000

@Query("select s from EMPLOYEES s where s.salary >=2000") List<Employee>


findEmployee();
}
Screenshot 8 – List of all employees who have a salary greater than or equal to
2000 H2 table

You might also like