package it.acxent.human; import it.acxent.db.ApplParmFull; import it.acxent.db.DBAdapter; import it.acxent.util.PasswordPolicy; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Random; public class Challenge { private String hid; private String hokpage; private long httl; private String theChallenge; private LinkedHashSet theQuestion; public static final String _HC = "_HC_"; private ApplParmFull apFull; public Challenge(ApplParmFull apFull) { this.apFull = apFull; } public Challenge() {} public Challenge(ApplParmFull apFull, int sizeChallenge, int sizeQuestion) { this.apFull = apFull; setTheChallenge(PasswordPolicy.getRandomPassword(sizeChallenge, false)); Random random = new Random(); while (getTheQuestion().size() < sizeQuestion) { int numeroCasuale = random.nextInt(sizeChallenge) + 1; if (!getTheQuestion().contains(Integer.valueOf(numeroCasuale))) getTheQuestion().add(Integer.valueOf(numeroCasuale)); } } public String getQuestion() { return getQuestion("it"); } public String getQuestion(String lang) { StringBuilder sb = new StringBuilder(getApFull().translate("Inserisci l'elemento delle posizioni", lang) + " "); int i = 0; for (Iterator iterator = getTheQuestion().iterator(); iterator.hasNext(); ) { int elemento = iterator.next(); i++; sb.append(getApFull().translate(DBAdapter.numberToString((double)elemento, false), lang)); if (i < getTheQuestion().size()) sb.append(", "); } return sb.toString(); } public boolean checkAnswer(String answer) { StringBuilder correctAnswer = new StringBuilder(); for (Iterator iterator = getTheQuestion().iterator(); iterator.hasNext(); ) { int elemento = iterator.next(); correctAnswer.append(getTheChallenge().charAt(elemento - 1)); } if (correctAnswer.toString().equals(answer)) return true; return false; } public String getTheChallenge() { return (this.theChallenge == null) ? "" : this.theChallenge.trim(); } public void setTheChallenge(String theChallenge) { this.theChallenge = theChallenge; } public LinkedHashSet getTheQuestion() { if (this.theQuestion == null) this.theQuestion = new LinkedHashSet<>(); return this.theQuestion; } public void setTheQuestion(LinkedHashSet theQuestion) { this.theQuestion = theQuestion; } public String getHtmlChallenge() { StringBuilder sb = new StringBuilder("
"); for (int i = 0; i < getTheChallenge().length(); i++) { char carattere = getTheChallenge().charAt(i); sb.append(""); sb.append(carattere); sb.append(""); } sb.append("
"); return sb.toString(); } public String getHid() { return this.hid; } public void setHid(String hid) { this.hid = hid; } public String getHokpage() { return this.hokpage; } public void setHokpage(String hokpage) { this.hokpage = hokpage; } public long getHttl() { return this.httl; } public void setHttl(long httl) { this.httl = httl; } public ApplParmFull getApFull() { return this.apFull; } public void setApFull(ApplParmFull apFull) { this.apFull = apFull; } }