-
Notifications
You must be signed in to change notification settings - Fork 3
/
CompratoreCasuale.java
83 lines (69 loc) · 2.72 KB
/
CompratoreCasuale.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
/*
Copyright 2022 Massimo Santini
This file is part of "Programmazione 2 @ UniMI" teaching material.
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This material is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this file. If not, see <https://www.gnu.org/licenses/>.
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
/**
* Classe concreta che rappresenta un compratore che acquista da ciascuna bancarella il massimo
* numero di giocattoli scegliendo per prime le bancarelle che offrono il minor prezzo unitario.
*/
public class CompratoreCasuale extends AbstractCompratore {
/** Il generatore di numeri casuali usato da questa classe. */
private final Random rng = new Random();
/**
* Costruisce un compratore (permettendo di specificare il seme del generatore, per consentire la
* riproduciblità dell'esecuzione).
*
* @param bancarelle le bancarelle.
* @param seed il seme del genratore casuale.
* @see AbstractCompratore
*/
public CompratoreCasuale(Set<Bancarella> bancarelle, final long seed) {
super(bancarelle);
rng.setSeed(seed);
}
/**
* Costruisce un compratore (il seme è dato dal tempo corrente all'esecuzione).
*
* @param bancarelle le bancarelle.
* @see AbstractCompratore
*/
public CompratoreCasuale(Set<Bancarella> bancarelle) {
this(bancarelle, System.currentTimeMillis());
}
@Override
public Acquisto compra(int num, Giocattolo giocattolo) {
Objects.requireNonNull(giocattolo, "Il giocattolo non può essere null.");
if (num <= 0) throw new IllegalArgumentException("Il numero deve essere positivo");
if (quantità(giocattolo) < num)
throw new IllegalArgumentException("Non ci sono abbastanza: " + giocattolo);
final Acquisto acquisto = new Acquisto(giocattolo);
int rimanenti = num;
final List<Bancarella> aCaso = new ArrayList<>(bancarelle);
Collections.shuffle(aCaso, rng);
for (final Bancarella b : aCaso) {
if (rimanenti == 0) break;
final int daComprare = Math.min(b.quantità(giocattolo), rimanenti);
if (daComprare == 0) continue;
b.vende(daComprare, giocattolo);
acquisto.aggiungi(daComprare, b);
rimanenti -= daComprare;
}
return acquisto;
}
}