Skip to content

Commit 906c4b7

Browse files
committed
Jobs Enum & Salaries Implemented
1 parent 3f03fc8 commit 906c4b7

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

src/main/java/com/itacademy/CrudEmpleats/controller/ControllerEmployees.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
import java.io.IOException;
44
import java.util.List;
5-
import java.util.NoSuchElementException;
65

76
import javax.servlet.http.HttpServletResponse;
87

98
import org.springframework.beans.factory.annotation.Autowired;
10-
import org.springframework.http.HttpStatus;
119
import org.springframework.web.bind.annotation.*;
12-
import org.springframework.web.server.ResponseStatusException;
1310

1411
import com.itacademy.CrudEmpleats.domain.Employee;
1512
import com.itacademy.CrudEmpleats.persistence.EmployeeRepository;
@@ -59,11 +56,7 @@ public List<Employee> allEmployees() {
5956
// Busca un empleat per Id
6057
@GetMapping("/Empleat/{id}")
6158
public Employee getFirstEmployee(@PathVariable("id") int id) {
62-
try {
6359
return repositori.getEmployeeById(id);
64-
} catch (NoSuchElementException e) {
65-
return null;
66-
}
6760
}
6861

6962
// Actualitzar Empleat
@@ -77,7 +70,7 @@ public Employee updateEmployee(@RequestBody Employee employee, @PathVariable("id
7770
@DeleteMapping("/Empleat/{id}")
7871
public String deleteEmployee( @PathVariable("id") int id) {
7972
repositori.deleteEmployee(id);
80-
if (getFirstEmployee(id)==null) {
73+
if(repositori.getEmployeeById(id)==null) {
8174
return "Empleat eliminat correctament.";
8275
}
8376
return "L'emplat no s'ha pogut eliminar.";

src/main/java/com/itacademy/CrudEmpleats/domain/Employee.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ public class Employee {
1414
private int id;
1515
private String name;
1616
private String job;
17+
private double salary ;
1718

1819

1920

2021
public Employee(String name, String job) {
2122
this.id=idCount++;
2223
this.name = name;
23-
this.job = job;
24+
this.job = Jobs.valueOf(job).getName();
25+
this.salary = Jobs.valueOf(job).getSalary();
2426
}
2527

2628
public int getId() {
@@ -39,7 +41,12 @@ public String getJob() {
3941
return job;
4042
}
4143
public void setJob(String job) {
42-
this.job = job;
44+
this.job = Jobs.valueOf(job).getName();
45+
this.salary = Jobs.valueOf(job).getSalary();
46+
}
47+
48+
public double getSalary() {
49+
return salary;
4350
}
4451

4552

src/main/java/com/itacademy/CrudEmpleats/domain/Jobs.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,28 @@ public enum Jobs {
66
* Les feines són fixes, és a dir ja estan definits en un ENUMERABLE.
77
*
88
*/
9+
10+
Director_Projectes("Director de projectes",50000),
11+
Programador_Senior("Programador Senior",35000),
12+
Programador_Mid("Programador Mid-Level",29000),
13+
Programador_Junior("Programador Junior",21000),
14+
Administrativa("Administrativa",25000);
15+
16+
private String name;
17+
private double salary;
18+
19+
private Jobs(String name, double salary) {
20+
this.name = name;
21+
this.salary = salary;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
double getSalary() {
29+
30+
return salary;
31+
}
32+
933
}

src/main/java/com/itacademy/CrudEmpleats/persistence/EmployeeRepository.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class EmployeeRepository {
1616

1717
private List<Employee> repository = new ArrayList<>(
1818
Arrays.asList(
19-
new Employee("Joan", "Director de Projectes"),
20-
new Employee("Gerard", "Programador"),
19+
new Employee("Joan", "Director_Projectes"),
20+
new Employee("Gerard", "Programador_Junior"),
2121
new Employee("Maria", "Administrativa")
2222
)
2323
);
@@ -26,8 +26,13 @@ public List<Employee> getAllEmployees() {
2626
return repository;
2727
}
2828

29-
public Employee getEmployeeById(int id) throws NoSuchElementException{
30-
return repository.stream().filter(employee -> employee.getId()==id).findFirst().get();
29+
public Employee getEmployeeById(int id){
30+
try {
31+
return repository.stream().filter(employee -> employee.getId()==id).findFirst().get();
32+
} catch (Exception e) {
33+
return null;
34+
}
35+
3136
}
3237

3338
public void addEmployee(Employee employee) {

0 commit comments

Comments
 (0)