Advertisement
desislava_topuzakova

03. Cafe

Jun 10th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. package cafe;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.List;
  6.  
  7. public class Cafe {
  8. //полета -> характеристики
  9. private String name;
  10. private int capacity;
  11. private List<Employee> employees;
  12.  
  13. //методи -> поведение
  14. //конструктор
  15.  
  16. public Cafe(String name, int capacity) {
  17. //нов празен обект
  18. this.name = name;
  19. this.capacity = capacity;
  20. this.employees = new ArrayList<>();
  21. }
  22.  
  23. public void addEmployee(Employee employee) {
  24. //мога да добавям ако текущия брой < капацитета
  25. //не мога да добавям текущия брой >= капацитета
  26. if (this.employees.size() < this.capacity) {
  27. this.employees.add(employee);
  28. }
  29. }
  30.  
  31. public boolean removeEmployee(String name) {
  32. for (Employee employee : this.employees) {
  33. if (employee.getName().equals(name)) {
  34. this.employees.remove(employee);
  35. return true;
  36. }
  37. }
  38. //преминали сме през всички служители и не сме намерили служител с даденото име
  39. return false;
  40. }
  41.  
  42. public Employee getOldestEmployee() {
  43. //1 начин
  44. return this.employees.stream().max(Comparator.comparingInt(Employee::getAge)).get();
  45.  
  46. //2 начин
  47. /*Employee oldestEmployee = new Employee("", 0, ""); //най-възрастния служител
  48. for (Employee employee : this.employees) {
  49. if (employee.getAge() > oldestEmployee.getAge()) {
  50. oldestEmployee = employee;
  51. }
  52. }
  53. return oldestEmployee;*/
  54. }
  55.  
  56. public Employee getEmployee(String name) {
  57. for (Employee employee : this.employees) {
  58. if (employee.getName().equals(name)) {
  59. return employee;
  60. }
  61. }
  62.  
  63. //преминали през всички служители и не сме намерили служител с даденото име
  64. return null;
  65. }
  66.  
  67. public int getCount() {
  68. return this.employees.size();
  69. }
  70.  
  71. public String report() {
  72. StringBuilder stringBuilder = new StringBuilder();
  73. stringBuilder.append("Employees working at Cafe " + this.name + ":").append(System.lineSeparator());
  74. this.employees.forEach(e -> stringBuilder.append(e.toString()).append(System.lineSeparator()));
  75. return stringBuilder.toString();
  76. }
  77. }
  78.  
  79.  
  80.  
  81. package cafe;
  82.  
  83. public class Employee {
  84. //полета -> характеристики
  85. private String name;
  86. private int age;
  87. private String country;
  88.  
  89. //методи
  90. //конструктор -> метод, чрез който създаваме обекти от класа
  91. //ALT + INSERT
  92. public Employee(String name, int age, String country) {
  93. //нов празен обект
  94. this.name = name;
  95. this.age = age;
  96. this.country = country;
  97. }
  98.  
  99. //Getters & Setters
  100. public String getName() {
  101. return name;
  102. }
  103.  
  104. public void setName(String name) {
  105. this.name = name;
  106. }
  107.  
  108. public int getAge() {
  109. return age;
  110. }
  111.  
  112. public void setAge(int age) {
  113. this.age = age;
  114. }
  115.  
  116. public String getCountry() {
  117. return country;
  118. }
  119.  
  120. public void setCountry(String country) {
  121. this.country = country;
  122. }
  123.  
  124. //toString -> представя всеки обект от класа под формата на текст
  125. @Override
  126. public String toString() {
  127. //"Employee: {name}, {age} from {country}"
  128. return String.format("Employee: %s, %d from %s", this.name, this.age, this.country);
  129. }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement