/* * Id.java * * Created on February 8, 2005, 5:10 PM * originally Created on August 14, 2002, 3:24 PM */ package edu.lasalle.redmond.IDs; import java.util.Random; /** * * @author Mike */ public class Id implements Comparable { private String id = ""; private boolean checkDig = false; private int requiredLen = 4; // length a fully specified ID (including // check digit) should be) protected int secret = 5; // junk used for trying out protected access private static Random generator = new Random(); // uses time as a seed /** Creates a new instance of Id */ public Id() { } /** * Creates a new instance of Id - given a string */ public Id(String str) { id = str; // rest take defaults } /** * Creates a new ranom instance of Id - given a length of Id * assumes no check digit */ public Id(int len) { id = genRandomValidId(len); // static method requiredLen = len; } /** * Creates a new ranom instance of Id - given a length of Id and whether * a check digit is desired * assumes no check digit */ public Id(int len, boolean check) { checkDig = check; requiredLen = len; String res; if (check) { res = genRandomValidId(len-1); // save room for check digit char digit = generateCheckDigit(res); res = res + digit; } else { res = genRandomValidId(len); } id = res; } /////////////////////////////////////////////////////////////////// // Static methods /////////////////////////////////////////////////////////////////// /** * generate a random digit - static */ private static char genRandDigit() { int rand; rand = generator.nextInt(10); // generate an int from 0-9 String dummy = ""; dummy = dummy + rand; // implicit cast to char char result; result = dummy.charAt(0); // use the first char in the string - the one set randomly return result; } /** * generate a random account number * Length is not specified - make 4 digits by default */ public static String genRandomValidId() { // generate 4 digits String newOne = genRandomValidId(4); // dont know if need a check digit because don't have an object return newOne; } /** * generate a random account number * Length is specified */ public static String genRandomValidId(int len) { String newOne = ""; // generate appropriate number of digits for (int cnt = 0; cnt < len; cnt++) { // generate a random digit char newDig = genRandDigit(); // add to end of id so far newOne += newDig; } // dont know if need a check digit because don't have an object return newOne; } /** * Generate a check digit based on a given "rest" of an ID */ public static char generateCheckDigit(String basicPart) { // how many digits int len = basicPart.length(); int power = 1; int total = 0; // loop through all digits - starting with the ones place for (int dig = 0 ; dig < len; dig++) { // convert to digit char diget = basicPart.charAt(dig); int val = Character.digit(diget,10); // multiply digit by power and add to subtotal int mult = val * power; total += mult; // prepare for next loop power = power * 2; } // get modulus to generate check digit int rem = total % 10; // convert to char String conv = Integer.toString(rem); char res = conv.charAt(0); // should only be one char return res; } /////////////////////////////////////////////////////////////////// // Facilitators /////////////////////////////////////////////////////////////////// // not currently written /** * Check to see if an Id has a proper check digit */ public boolean checkCheckDigit() { // if the Id doesn't have a check digit, then it passes automatically if (! checkDig) { return true; } else { // get local copy of string for ease of working String whole = getId(); // strip off check digit int len = whole.length(); String basePart = whole.substring(0,len-1); char check = whole.charAt(len-1); // generate expected check digit to compare to check digit in Id char expected = Id.generateCheckDigit(basePart); if (expected == check) { return true; } else { return false; } } } /////////////////////////////////////////////////////////////////// // Inspectors /////////////////////////////////////////////////////////////////// /** * report actual id string */ public String getId() { return id; } /** * report whether id has a check digit */ public boolean getCheckDig() { return checkDig; } /** * report desired id string length */ public int getRequiredLen() { return requiredLen; } /** * report actual current id string length */ public int getLength() { return id.length(); } /////////////////////////////////////////////////////////////////// // Mutators /////////////////////////////////////////////////////////////////// /** * change actual id string */ public void setId(String str) { id = str; } /** * change whether id has a check digit */ public void setCheckDig(boolean check) { checkDig = check; } /** * change desired id string length */ public boolean setRequiredLen(int len) { // complain if try to set zero or less length if (len <= 0) { // would throw an exception if we'd covered them System.out.println("PROGRAM ERROR - cannot set a zero or id length"); return false; } else { requiredLen = len; } return true; } /** * change id to add a check digit to the string */ public char addCheckDigit() { // call static function to come up with check digit char check = Id.generateCheckDigit(id); // add it to the id id = id + check; // return the check digit in case it is needed return check; } /////////////////////////////////////////////////////////////////// // Override Object's version /////////////////////////////////////////////////////////////////// public String toString() { String res; res = getId() + " - " + getRequiredLen() + " - " + getCheckDig(); return res; } public String toShortString() { String res; res = getId(); return res; } /** * determine if a passed Id has the same contents as the * calling object (Id number, required length, AND check digit) */ public boolean equals (Id toCompare) { if ((getId().equals(toCompare.getId())) && (getRequiredLen() == toCompare.getRequiredLen()) && (getCheckDig() == toCompare.getCheckDig()) ) { return true; } else { return false; } } /** * determine if a passed Id has the same contents as the * calling object (Id number, required length, AND check digit) * Overrides version in java.lang.Object */ public boolean equals(Object toCompare) { Id realId = (Id) toCompare; // cast to Id return this.equals(realId); // call above to determine if same CONTENTS } /** * Compares two Ids - needed for sorting - required since implements Comparable interface * returns negative if object comparing to is less, zero if object comparing to is equal, * positive if object comparing to is greater */ public int compareTo(Object obj) { // convert the passed object into an Id Id other = (Id) obj; // pull out the strings to compare String otherId = other.getId(); String firstId = getId(); // do the comparison using String class's capabilities // and return the result return firstId.compareTo(otherId); } }