Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import sun.rmi.server.InactiveGroupException;
- import java.awt.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- public class Main {
- static class AvgCalculator {
- public double calculateAvg(List<Integer> grades) {
- List<Integer> copy = removeExtrems(grades);
- return internalCalculateAvg(copy);
- }
- private List<Integer> removeExtrems(List<Integer> grades) {
- List<Integer> copy = new ArrayList<>(grades);
- if(grades.size() > 3) {
- final Integer max = Collections.max(copy);
- final Integer min = Collections.min(copy);
- copy.remove(max);
- copy.remove(min);
- }
- return copy;
- }
- private double internalCalculateAvg(List<Integer> grades) {
- double sum = 0;
- for (Integer grade : grades) {
- sum += grade;
- }
- return sum / grades.size();
- }
- }
- static class Book {
- private String title;
- private int year;
- public Book(String title, int year) {
- this.title = title;
- this.year = year;
- }
- public int getYear() {
- return year;
- }
- @Override
- public String toString() {
- return "Book{" +
- "title='" + title + '\'' +
- ", year=" + year +
- '}';
- }
- }
- static class Library {
- private final List<Book> books;
- public Library(List<Book> books) {
- this.books = books;
- }
- public List<Book> filterByMinYear(int year) {
- final List<Book> result = new ArrayList<>();
- for (Book book : books) {
- if(book.getYear() > year) {
- result.add(book);
- }
- }
- return result;
- }
- }
- public static void main(String[] args) {
- final Book a = new Book("a", 1998);
- final Book b = new Book("b", 2001);
- final Library library = new Library(Arrays.asList(a, b));
- System.out.println(library.filterByMinYear(1990));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement