package ch.bfh.swed.i2ateam4;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;/** * This class represent an activation code, which is a String of * fixed length containing only digit. */public class ActivationCode { private static final int length = 6; private int code; /** * Generates a new random activation code with the sha1 algorithm, * which should be available on all platforms. * @throws NoSuchAlgorithmException */ public ActivationCode(){ SecureRandom sr; try{ sr = SecureRandom.getInstance("SHA1"); code = sr.nextInt((int)(Math.pow(10, length))); }catch (NoSuchAlgorithmException e){ //TODO This algorithm is provided by the standard jre. Yes I'm lazy. System.err.println(e.getMessage()); System.exit(0); } } /** * Construct an activation code with the given code. * This is intended for client and testing. * @param code * @throws IllegalArgumentException */ public ActivationCode(int code) throws IllegalArgumentException{ if(code < 0 || code > Math.pow(10, length)){ throw new IllegalArgumentException(Integer.toString(code)); } this.code = code; } /** * @return the integer value of the code */ public int getCode(){ return code; } /** * Compare an ActivationCode objects with the current * object for equality * @param ac the other object * @return true if both object contains the same code */ public boolean equal(ActivationCode ac){ return (this.code == ac.getCode()); }}