mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
54 lines
1 KiB
Java
54 lines
1 KiB
Java
|
package catering.staff;
|
||
|
|
||
|
import org.springframework.stereotype.Component;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Collection;
|
||
|
import java.util.HashSet;
|
||
|
import java.util.Optional;
|
||
|
import java.util.Set;
|
||
|
|
||
|
@Component
|
||
|
public class StaffRepository {
|
||
|
|
||
|
private Set<Staff> staffs = new HashSet<>();
|
||
|
|
||
|
public StaffRepository() {
|
||
|
}
|
||
|
|
||
|
public boolean addStaff(Staff staff) {
|
||
|
return this.staffs.add(staff);
|
||
|
}
|
||
|
|
||
|
private int nextId = (int) (Math.random() * 10 * Math.random() * 10 + Math.random() * 10);
|
||
|
|
||
|
public void save(Staff staff) {
|
||
|
|
||
|
if (staff.getId() == 0) {
|
||
|
staff.setId(nextId++);
|
||
|
} else {
|
||
|
|
||
|
this.staffs.removeIf(p -> p.getId() == staff.getId());
|
||
|
}
|
||
|
|
||
|
this.staffs.add(staff);
|
||
|
}
|
||
|
|
||
|
public long count() {
|
||
|
return this.staffs.size();
|
||
|
}
|
||
|
|
||
|
public boolean removeStaff(int staffID) {
|
||
|
return this.staffs.removeIf(staff -> staff.getId() == staffID);
|
||
|
}
|
||
|
|
||
|
public Collection<Staff> getStaffs() {
|
||
|
return new ArrayList<>(this.staffs);
|
||
|
}
|
||
|
|
||
|
public Optional<Staff> findById(int id) {
|
||
|
return this.staffs.stream().filter(staff -> staff.getId() == id).findFirst();
|
||
|
}
|
||
|
|
||
|
}
|