Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. package stringquiz;
  2.  
  3. import java.util.SortedMap;
  4. import java.util.TreeMap;
  5.  
  6. /**
  7. *
  8. * @author alex
  9. */
  10. public class StringQuiz {
  11.  
  12. private String segreta;
  13. private long timeOut;
  14. private static SortedMap<Thread,Integer> mappatura;
  15.  
  16. public StringQuiz(String s, long durata){
  17. this.segreta = s;
  18. this.timeOut = System.currentTimeMillis() + durata;
  19. mappatura = new TreeMap<Thread,Integer>();
  20. }
  21.  
  22. public synchronized boolean guess(String prova) throws Exception{
  23.  
  24. if(System.currentTimeMillis()>=timeOut)
  25. throw new Exception("tempo scarduto! ");
  26.  
  27. if(!mappatura.containsKey(Thread.currentThread()))
  28. mappatura.put(Thread.currentThread(),0);
  29. else{
  30. if(mappatura.get(Thread.currentThread())==3)
  31. throw new Exception("Il thread ci ha già provato 3 volte!");
  32. else
  33. if(prova.equals(segreta)==true)
  34. return true;
  35. else
  36. mappatura.replace(Thread.currentThread(), mappatura.get(Thread.currentThread())+1);
  37. }
  38. return false;
  39. }
  40.  
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement