CheckDigits.java
contents ::
  addmod
  AppCheck.java
  CheckDigits.java
  ex3.data
  ex3.expected
  indata
  letters

/*
  James Little
  17/3/01 - Exercise 3 - cosc 241
  Program to check ISBN numbers
*/

package jlittle_ex3;
import basicIO.*;

public class CheckDigits {
    private int [] digits= new int[10];  
    private int count;
    private String valid;
    private String temp;
    private boolean possible=true;

    public CheckDigits(int [] isbnten, int count, String isbn){
         System.arraycopy(isbnten, 0, digits, 0, isbnten.length); // copy parameter array to local int array
         this.count=count;         // limits the for loops if array is not full
         temp=isbn;                // passed input as string
    }

    public CheckDigits(String isbn, boolean possible){
         temp=isbn;                // passed input string
         this.possible=possible;   // this not possible
         valid="INVALID";          
    }

    public int getMod(){
         int calculation=0;

         for(int i=0; i<count; i++){
             
             calculation=calculation+(digits[i]*(i+1));  // the formula
         }

         calculation=calculation%11; // the rest of the formula
         return calculation; 
    }

    public String validISBN(){
         if(getMod()==0)             // input invalid if mod is not equal to zero
             valid="VALID";
         else
             valid="INVALID";
         return valid;
    }

    public void displayData(){
         int mod = getMod();
         
         if(possible){              // if its possibly an isbn number
             if(count==9){          // and count is nine
                  if(mod<10){        // and mod is X
                      Out.println(temp+"-"+mod+" VALID");
                  }else{
                      char modx='X'; // else mod is X
                      Out.println(temp+"-"+modx+" VALID");
                  }
             }else                  // otherwise we just check and see if its valid
                  Out.println(temp+" "+validISBN());
             
         }else
             Out.println(temp+" "+valid); // or its just not valid

    }
}// end class CheckDigits

James Little