EKzam

mail@pastecode.io avatar
unknown
java
2 years ago
2.3 kB
1
Indexable
Never
package ee.ttu.algoritmid.bond;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;

public class Activities {

    private String activity;
    private int whenWasLastDone;
    private int whenNextTime;

    public Activities(String activity, int whenWasLastDone, int whenNextTime){
        this.activity = activity;
        this.whenWasLastDone = whenWasLastDone;
        this.whenNextTime = whenNextTime;
    }

    List<Activities> nextActivities(List<Activities> activities, int count) {
        activities.sort(new MyComparator());
        return activities.stream().limit(count).toList();
    }

    public static class MyComparator implements Comparator<Activities> {
        @Override
        public int compare(Activities activities1, Activities activities2) {
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int exactTime1 = activities1.whenWasLastDone + activities1.whenNextTime;
            int exactTime2 = activities2.whenWasLastDone + activities2.whenNextTime;
            if (hour - exactTime1 < hour - exactTime2) {
                return 1;
            } else if (hour - exactTime1 > hour - exactTime2) {
                return -1;
            }
            return activities1.activity.compareTo(activities2.activity);
        }
    }

    public static void main(String args[]) {
        List<Activities> activities = new ArrayList<>();
        activities.add(new Activities("Toita kassi", 15, 4));
        activities.add(new Activities("Toita koera", 15, 5));
        activities.add(new Activities("Toita kassi", 19, 4));
        activities.add(new Activities("Toita koera", 20, 5));
        activities.add(new Activities("Toita kassi", 23, 4));
        activities.add(new Activities("Toita koera", 1, 6));
        activities.add(new Activities("Toita kassi", 3, 4));
        activities.add(new Activities("Toita koera", 6, 5));
        activities.add(new Activities("Toita kassi", 7, 4));
        activities.add(new Activities("Puhastada akvaariumi", 10, 24));

        Activities activities1 = new Activities("sa", 1, 1);
        System.out.print(activities1.nextActivities(activities, 3));
    }
}