Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. package com.company;
  2.  
  3. import sun.rmi.server.InactiveGroupException;
  4.  
  5. import java.awt.*;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.Collections;
  9. import java.util.List;
  10.  
  11. public class Main {
  12.  
  13. static class AvgCalculator {
  14.  
  15. public double calculateAvg(List<Integer> grades) {
  16. List<Integer> copy = removeExtrems(grades);
  17. return internalCalculateAvg(copy);
  18. }
  19.  
  20. private List<Integer> removeExtrems(List<Integer> grades) {
  21. List<Integer> copy = new ArrayList<>(grades);
  22. if(grades.size() > 3) {
  23. final Integer max = Collections.max(copy);
  24. final Integer min = Collections.min(copy);
  25.  
  26. copy.remove(max);
  27. copy.remove(min);
  28. }
  29. return copy;
  30. }
  31.  
  32. private double internalCalculateAvg(List<Integer> grades) {
  33. double sum = 0;
  34. for (Integer grade : grades) {
  35. sum += grade;
  36. }
  37. return sum / grades.size();
  38. }
  39.  
  40. }
  41.  
  42. static class Book {
  43.  
  44. private String title;
  45. private int year;
  46.  
  47. public Book(String title, int year) {
  48. this.title = title;
  49. this.year = year;
  50. }
  51.  
  52. public int getYear() {
  53. return year;
  54. }
  55.  
  56. @Override
  57. public String toString() {
  58. return "Book{" +
  59. "title='" + title + '\'' +
  60. ", year=" + year +
  61. '}';
  62. }
  63. }
  64.  
  65. static class Library {
  66.  
  67. private final List<Book> books;
  68.  
  69. public Library(List<Book> books) {
  70. this.books = books;
  71. }
  72.  
  73. public List<Book> filterByMinYear(int year) {
  74. final List<Book> result = new ArrayList<>();
  75. for (Book book : books) {
  76. if(book.getYear() > year) {
  77. result.add(book);
  78. }
  79. }
  80. return result;
  81. }
  82. }
  83.  
  84. public static void main(String[] args) {
  85. final Book a = new Book("a", 1998);
  86. final Book b = new Book("b", 2001);
  87.  
  88. final Library library = new Library(Arrays.asList(a, b));
  89.  
  90. System.out.println(library.filterByMinYear(1990));
  91.  
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement