swt23w23/src/main/java/catering/staff/StaffRepository.java

54 lines
1 KiB
Java
Raw Normal View History

2023-11-10 17:09:39 +01:00
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();
}
}