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 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 getStaffs() { return new ArrayList<>(this.staffs); } public Optional findById(int id) { return this.staffs.stream().filter(staff -> staff.getId() == id).findFirst(); } }