-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLibrary.java
114 lines (101 loc) · 3.92 KB
/
Library.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.*;
public class Library {
private List<Book> books;
private int signupTime;
private int parallelCount;
private int libraryName;
/**
* @param books The list of Books contained in this library, it is copied and
* sorted by value.
*/
public Library(int libraryName, List<Book> books, int signupTime, int parallelCount) {
List<Book> sortedBooks = new ArrayList<>(books);
Collections.sort(sortedBooks, new BookComparator());
this.books = sortedBooks;
this.signupTime = signupTime;
this.parallelCount = parallelCount;
this.libraryName = libraryName;
}
public List<Book> getBooks() {
return books;
}
public int getSignupTime() {
return signupTime;
}
public int getParallelCount() {
return parallelCount;
}
public int getLibraryName() {
return libraryName;
}
/**
* @return The first book of the List, having the maximum value.
*/
public Book getMaxValueBook() {
return books.get(0);
}
/**
* Method removing the first book of the List and returning it;
*/
public Book popMaxValueBook() {
Book maxValBook = books.get(0);
books.remove(0);
return maxValBook;
}
/**
* Method removing {@code bookToRemove} from this library.
*
* @param bookToRemove The book to remove.
* @return true if the book was removed, false otherwise.
*/
public boolean removeBook(Book bookToRemove) {
return books.remove(bookToRemove);
}
/**
* Calculates how much score it can do in the given remaining days
*
*
* @param remainingDays Remaining days useful to scan
* @param scannedBooks Books already scanned
*/
public int getPossibleScore(int remainingDays) {
int internalBookCounter = 0; // Quanti libri sono stati contati da questa libreria
int usefulDays = remainingDays - signupTime; // Quanti giorni possiamo effettivamente inviare libri
int possibleScore = 0;
// System.out.println("getPossibleScore: nomeLibreria " + libraryName);
// Ciclo per i giorni rimanenti oppure per i libri
for (int i = 0; i < usefulDays && internalBookCounter < books.size(); i++) {
// j è quanti libri abbiamo mandato in questa giornata
// System.out.println("getPossibleScore: giorno " + i);
for (int j = 0; j < parallelCount && internalBookCounter < books.size(); j++) {
// System.out.println("getPossibleScore: scelgo il libro numero " +
// books.get(internalBookCounter).getId()+ " di valore " +
// books.get(internalBookCounter).getValue());
// I libri già mandati sono già stati rimossi da questa libreria
possibleScore += books.get(internalBookCounter).getValue();
internalBookCounter++;
}
}
// System.out.println("getPossibleScore: ritorno lo score " + possibleScore);
// return possibleScore;
return possibleScore / signupTime;
}
/**
* @param usefulDays remaining useful days (this means to exclude our signup
* time).
*/
public List<Book> getBooksToSend(int usefulDays) {
List<Book> booksToSend = new ArrayList<Book>();
long possiblebooks = (long) usefulDays * (long) parallelCount;
// System.out.print("getBookSand: libreria " + libraryName + " : " + usefulDays
// + " usefulDays " + usefulDays
// + " parallelCount " + parallelCount + " possiblebooks " + possiblebooks);
for (int i = 0; i < possiblebooks && i < books.size(); i++) {
booksToSend.add(books.get(i));
// System.out.print("Libro " + books.get(i).getId() + " valore " +
// books.get(i).getValue());
}
// System.out.println("");
return booksToSend;
}
}