codice:
public class Anagraphics {
private static LinkedList<String> anagraphicList = null;
private static int minNum = 0;
private static int maxNum = 100;
private static Random r = new Random();
private Anagraphics(){
}
public static String getRandomName(){
if(anagraphicList == null){
anagraphicList = new LinkedList<String>();
anagraphicList.addAll(Arrays.asList(all100Names));
}
int number = r.nextInt(maxNum-minNum) + minNum;
String name = anagraphicList.get(number);
anagraphicList.remove(number);
maxNum--;
//System.out.println("Rimanenti: " + anagraphicList.size());
return name;
}
private static String[] all100Names = {"mFrancesco","mMarco","mShawn","mAaron","mJonah","mDaniel","mLiam","mRyan","mJames","mAlex"
,"mEthan","mAli","mHarry","mMichael","mDavid","mLuke","mMatthew","mJack","mTyler","mJoshua"
,"mJordan","mAndrew","mAlexander","mDylan","mNoah","mNiall","mSpencer","mAdam","mJoseph","mZayn"
,"mAiden","mNathan","mAustin","mLogan","mJohn","mKevin","mJayden","mBlake","mAnthony","mBrian"
,"mChristopher","mJason","mMason","mLouis","mJustin","mWilliam","mJackson","mMax","mBrandon","mKyle",
"fChloe","fEmily","fIatta","fEmma","fJennifer","fOlivia","fJessica","fHannah","fLily","fSarah"
,"fSavannah","fIsabella","fAva","fRebecca","fElla","fSophia","fGrace","fCharlotte","fElizabeth","fMia"
,"fAbigail","fSamantha","fAnna","fMegan","fNicole","fAshley","fSophie","fKatie","fZoe","fMadison"
,"fLauren","fJasmine","fJade","fAmy","fLucy","fAlyssa","fAmber","fAbby","fAmanda","fBella"
,"fNatalie","fLilly","fRachel","fTaylor","fEllie","fAlexis","fPaige","fBarbara","fVanessa","fAlice"};
}
codice:
public class TvRoom {
private final int waitingQueue = 20;
private final Map<Integer,Integer> channelDesiredMap = new HashMap<Integer,Integer>(1000);
private static TvRoom instanceTvRoom = new TvRoom();
private final static int maxPersonRoomCapacity = 30;
private final Lock lock = new ReentrantLock();
private final Lock lockExit = new ReentrantLock();
private final Condition iWantMyChannelCondition = lock.newCondition();
private int tvWatchers = 0;
private int currentChannel = 0; //Canale inesistente..validi da 1 a 8
private int getFirstMaxDesiredChannel(){
Map.Entry<Integer,Integer> maxEntry = null;
for (Map.Entry<Integer,Integer> entry : channelDesiredMap.entrySet())
{
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
{
maxEntry = entry;
}
}
System.out.println("Il Canale più desiderato è : " + maxEntry.getKey());
return maxEntry.getKey();
}
private boolean getCurrentFavouriteChannelViewers(Map<Integer,Integer> map,int favChannel){
return( map.get(favChannel) >0?true:false);
}
private void incrementCount(Map<Integer,Integer> map,Integer q) {
Integer oldVal, newVal;
oldVal = map.get(q);
newVal = (oldVal == null) ? 1 : (oldVal + 1);
map.put(q, newVal);
}
private void decrementCount(Map<Integer,Integer> map,Integer q) {
Integer oldVal, newVal;
oldVal = map.get(q);
newVal = (oldVal == null) ? 0 : (oldVal - 1);
map.put(q, newVal);
}
public boolean getCurrentChannelEquality(int favChannel) {
boolean bool = false;
//System.out.println("canale corrente: " + this.currentChannel + " fav: " + favChannel);
if(this.currentChannel == favChannel)
bool = true;
return bool;
}
private void trySetCurrentChannel2Favourite(Person person){
if(getTvWatchers()==1){
this.currentChannel = person.getChannel();
System.out.println(person.getPersonName() + " sintonizza il Canale " + person.getChannel()
+ " xkè sta guardando solo " + (person.getSex().equalsIgnoreCase("maschio")?"lui":"lei") +"!");
}
incrementCount(channelDesiredMap,person.getChannel());
}
private void observeTvScreen(){
tvWatchers++;
}
public int getTvWatchers() {
return tvWatchers;
}
private void quitWatching(){
tvWatchers--;
}
private TvRoom(){
System.out.println("\r\n======== SALA TV APERTA :-)) ========\r\n");
}
private static Semaphore semaphore = new Semaphore(maxPersonRoomCapacity, true);
public static TvRoom getEntranceTvRoom(){
return instanceTvRoom;
}
public void watchTV(Person person){
try {
semaphore.acquire();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
attemptWatchTV(person);
} finally {
/*
* esce dalla sala: prima di uscire, se nessuno sta più guardando il canale C,
* cambia il canale del
* televisore in quello per cui è in attesa il maggior numero di persone.
*/
lockExit.lock();
System.out.println(person.getPersonName()
+ " è uscit" + (person.getSex().equalsIgnoreCase("maschio")?"o":"a") +" dalla sala!");
/* */
decrementCount(channelDesiredMap,person.getChannel());
if(!getCurrentFavouriteChannelViewers(channelDesiredMap,person.getChannel())){
this.currentChannel = getFirstMaxDesiredChannel();
System.out.println("\"Accidenti! Pare che nessuno sia più interessato al mio Canale preferito...\" disse "
+ person.getPersonName() + " prima di sintonizzare la TV sul nuovo Canale " + this.currentChannel);
}
quitWatching();
System.out.println("Rumore della porta che si chiude dietro a " + person.getPersonName());
lockExit.unlock();
semaphore.release();
}
}
public void attemptWatchTV(Person person){
try {
System.out.println(person.getPersonName() +" (" + person.getSex()+ ") entra in sala TV (actual tot." + (maxPersonRoomCapacity-semaphore.availablePermits())+ " p.) e vuol vedere il Canale: " + person.getChannel());
lock.lock();
observeTvScreen();
//Tento di settare il canale C
trySetCurrentChannel2Favourite(person);
//Check se è il mio canale preferito C
while(!getCurrentChannelEquality(person.getChannel())){
System.out.println(person.getPersonName() + " attende il Canale " + person.getChannel() + " per " + this.waitingQueue + " secondi...mentre vede Canale " + this.currentChannel );
iWantMyChannelCondition.await(this.waitingQueue, TimeUnit.SECONDS);
}
iWantMyChannelCondition.signalAll();
lock.unlock();
System.out.println(person.getPersonName() + " guarda il suo canale preferito alla TV!!");
//Fisso la TV!
//guardo il canale C per un tempo compreso tra 30 e 300 secondi;
Thread.sleep(Time.getRandomWatchTime());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}