Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cafe;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- public class Cafe {
- //полета -> характеристики
- private String name;
- private int capacity;
- private List<Employee> employees;
- //методи -> поведение
- //конструктор
- public Cafe(String name, int capacity) {
- //нов празен обект
- this.name = name;
- this.capacity = capacity;
- this.employees = new ArrayList<>();
- }
- public void addEmployee(Employee employee) {
- //мога да добавям ако текущия брой < капацитета
- //не мога да добавям текущия брой >= капацитета
- if (this.employees.size() < this.capacity) {
- this.employees.add(employee);
- }
- }
- public boolean removeEmployee(String name) {
- for (Employee employee : this.employees) {
- if (employee.getName().equals(name)) {
- this.employees.remove(employee);
- return true;
- }
- }
- //преминали сме през всички служители и не сме намерили служител с даденото име
- return false;
- }
- public Employee getOldestEmployee() {
- //1 начин
- return this.employees.stream().max(Comparator.comparingInt(Employee::getAge)).get();
- //2 начин
- /*Employee oldestEmployee = new Employee("", 0, ""); //най-възрастния служител
- for (Employee employee : this.employees) {
- if (employee.getAge() > oldestEmployee.getAge()) {
- oldestEmployee = employee;
- }
- }
- return oldestEmployee;*/
- }
- public Employee getEmployee(String name) {
- for (Employee employee : this.employees) {
- if (employee.getName().equals(name)) {
- return employee;
- }
- }
- //преминали през всички служители и не сме намерили служител с даденото име
- return null;
- }
- public int getCount() {
- return this.employees.size();
- }
- public String report() {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append("Employees working at Cafe " + this.name + ":").append(System.lineSeparator());
- this.employees.forEach(e -> stringBuilder.append(e.toString()).append(System.lineSeparator()));
- return stringBuilder.toString();
- }
- }
- package cafe;
- public class Employee {
- //полета -> характеристики
- private String name;
- private int age;
- private String country;
- //методи
- //конструктор -> метод, чрез който създаваме обекти от класа
- //ALT + INSERT
- public Employee(String name, int age, String country) {
- //нов празен обект
- this.name = name;
- this.age = age;
- this.country = country;
- }
- //Getters & Setters
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- //toString -> представя всеки обект от класа под формата на текст
- @Override
- public String toString() {
- //"Employee: {name}, {age} from {country}"
- return String.format("Employee: %s, %d from %s", this.name, this.age, this.country);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement