I have a (for me) a big perplexing problem. I am new to Java and am in a introductory class. We were suppose to create/"fix" a method for Formatting ISBN-10's. Our Professor gave us all the code and the classes but we need to implement another "design teams" code for the formatISBN method. I am going to post the code for all the classes. I don't know if i should post all of that, but I don't know how much someone would need to help me. There are three classes, Book, BookTest and BookUI. the only one that i did work in was the Book class and the only method i touched was the formatISBN() method. MY PROBLEM IS, when i run the BookUI and enter the data myself it verifies the ISBN like it should, it returns the formatted ISBN for a valid ISBN and a blank String for an invalid ISBN. It does all the ISBN's that way, through user input just fine. HOWEVER when I run the test unit it gives me an error. I use the BlueJ IDE, I create an object of the Book class and initialize to the same thing that is in the Unit Test. It looks like everything is working, however, when you run the formatISBN() method and put in a different ISBN than the one it initialized with for it to format, it just returns a blank String. It is suppose to return the formatted ISBN , which it does, except when running the Unit test and i guess Initializing a Book object to one set of parameters and then changing the ISBN during the formatISBN method is messing something up???? I have NO IDEA what is going on to cause this. Please if someone could help me i would be very grateful. Sorry for this post being so long too.
Thank you in advance ,
ERic
/** A class that represents a single book. For every book it maintains
* title, author, number of pages and ISBN number.
*
* All methods work except the formatISBN() method which
* you must implement based on the design from another team
*
* @author Team Wirth (our teams name)
* @version 2009
*/
class Book
{
// The fields.
private String author;
private String title;
private int pages;
private String isbn;
private String tempFinal = ""; // This holds the temporary string of what the final output will be if valid
private String finalString = ""; // if valid this takes over the tempFinal string and this is the String that is returned
private int count; // This variable keeps count of how many characters of the String you have moved through
private int contiguousDashes; // makes sure that the ISBN has no continuous dashes
private int dashes; // makes sure that the ISBN has no more than 3 dashs
private int isbnLength; // used to store the length of the ISBN (how many characters)
private int numbers; // keeps track to make sure the ISBN has 10 numbers at the end.
private int numberTotal; // Keeps a running total used to verify with modulus 11 that the ISBN is valid
private int tempValue; // Temp variable to hold value to be processed for numberTotal in the loop
private char character; // used to represent the character at a place in the String
private int counter; // Extra variable that i was just testing around with, i was going to take it out in final build
public Book(String bookAuthor, String bookTitle, int bookpages, String isbn) {
author = bookAuthor;
title = bookTitle;
pages = bookpages;
this.isbn = formatISBN(isbn);
counter = 0;
}
/** IMPLEMENT THIS METHOD!
*
* Processes input ISBN to a standard format that includes no
* spaces or dashes.
*
* @param origISBN the submitted ISBN
* @return a properly formatted ISBN or the empty String if the submitteed ISBN is invalid
*/
public String formatISBN(String origISBN) // Is receiving the ISBN that you input
{
isbnLength = origISBN.length(); // this is calculating the length of the String.
if (isbnLength != 10 && isbnLength != 13) // Makes sure that the ISBN is the appropriate length, either 10 chars without dashes, or 13 with dashes
return finalString; // returns blank string
count++; // increments the counter
do // Starts a loop to cycle through the String processing characters
{
character = origISBN.charAt(counter); // Finds the character at whatever counter is currently at and stores it in character
if (character >= 0 && character <= 9 && character != ' ' && character != '-' && character != 'X') // makes sure no invalid characters are in ISBN
return finalString;
if (count == 1 || count == 10 || count == 13) // makes sure ISBN doesn't start or end illegally
{
if (character == ' ' || character == '-') // if it starts or ends with a dash or space it is blank
{
return finalString;
}
}
if (character == ' ' || character == '-') // used to just process valid dashes or spaces
{
if (contiguousDashes == 1) // makes sure there are no dashes next to each other
return finalString;
contiguousDashes++;
dashes++;
if (dashes > 3) // makes sure there are no more than three dashes in ISBN
return finalString;
}
else
{
contiguousDashes = 0; // if it comes across a number the contDashes are reset
tempFinal += character; // These creates the new String that will ultimately be returned, assemblying it one at a time
numbers++; // increments numbers.
if (character == 'X') // finds if there is an X in the ISBN
{
if (numbers != 10) // Makes sure that the X isn't at any place in the ISBN EXCEPT at the end.
return finalString;
if (count != 10 && count != 13) // Makes sure that the appropriate numbers have been processed, if not at the end of processing, returns blank String
return finalString;
tempValue = 10; // Substitutes X for an actual value
}
else
tempValue = Character.getNumericValue(character); // converts the character to an actual number
numberTotal = numberTotal + (tempValue * (11 - numbers)); // Keeps running total then implements the formula finding valid ISBNS
}
if (numbers == 11) // makes sure that there are not an invalid amount of numbers in the new String.
return finalString;
count++;
counter++;
}while (counter < isbnLength); // Keeps the loop going till it reaches the end of the String being processed.
if (numbers != 10) // Makes sure that the final ISBN has 10 characters in it.
return finalString;
numberTotal = numberTotal % 11; // Finishes the formula for finding if an ISBN is valid by modulus 11, result must equal 0 to be valid
if (numberTotal != 0) // makes sure the result was 0
return finalString;
else
this.isbn = tempFinal; //assigns the final ISBN
return this.isbn;//TODO: put your code here // SUPPOSE TO RETURN THE VALID ISBN which is does? sort of?
//TODO: Replace this statement with your own.
}
public String getISBN() {
return this.isbn;
}
public void setISBN(String isbn) {
this.isbn = formatISBN(isbn);
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public int getPages() {
return pages;
}
public String toString() {
StringBuffer s = new StringBuffer(author);
s.append(", ") ;
s.append(title);
s.append(", ") ;
s.append(pages) ;
s.append(" pages, ISBN: ") ;
s.append(this.isbn);
return s.toString();
}
}
The next class is the Unit Test Class, BookTest
/**
* MODIFY THE testFormatISBN() method to thoroughly test the
* formatISBN() method in Book.
*
* The test class BookTest.
*
* @author Team Wirth (our team name)
* @version 2008
*/
public class BookTest extends junit.framework.TestCase
{
private Book book1;
/**
* Default constructor for test class BookTest
*/
public BookTest()
{
}
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
protected void setUp()
{
book1 = new Book("Gosling", "Java Programming Language 3e", 595, "0-201-70433-1");
}
/** TODO: Expand this method to provide thorough testing of the
* formatISBN() method
*/
public void testFormatISBN()
{
// some very simple data that does not thoroughly test the method
assertEquals("123456789X", book1.formatISBN("123456789X"));
assertEquals("123456789X", book1.formatISBN("1-234-5678-9X"));
assertEquals("", book1.formatISBN("1234567890"));
assertEquals("", book1.formatISBN("abc"));
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
protected void tearDown()
{
}
public void testGetTitle()
{
assertEquals("Java Programming Language 3e", book1.getTitle());
}
public void testToString()
{
assertEquals("Gosling, Java Programming Language 3e, 595 pages, ISBN: 0201704331", book1.toString());
}
}
And finally the BookUI class he made us so we could test it ourselves .
/** DO NOT MODIFY THIS CLASS!
*
* A command line user interface for the Book class
*
* @author Team Wirth
* @version 2008
*/
import java.util.Scanner ;
public class BookUI {
Scanner console = new Scanner(System.in);
public void mainMenu() {
boolean finished = false;
while (! finished) {
displayMenuChoices();
String s = console.nextLine();
char c = s.toUpperCase().charAt(0);
switch (c) {
case 'A':
addBookUI();
break;
case 'Q':
finished = true;
break;
default:
System.out.println("\nPlease enter one of the choices!\n");
}
} // end while loop
System.exit(0); // exit the program
} // end mainMenu()
public void addBookUI() {
System.out.println("Add book method") ;
System.out.print("Author: ");
String author = console.nextLine();
System.out.print("Book Title: ");
String title = console.nextLine();
System.out.print("Number of Pages: ");
int pages = 0;
if ( console.hasNextInt() )
pages = console.nextInt();
console.nextLine() ; // clear rest of line after parsing int
System.out.print("ISBN Number: ");
String isbn = console.nextLine();
Book newBook = new Book(author, title, pages, isbn);
System.out.println("\n Book created: " + newBook);
}
private void displayMenuChoices() {
System.out.println();
System.out.println(" Media Catalog Main Menu");
System.out.println(" --------------------");
System.out.println(" (A)dd book");
System.out.println(" --------------------");
System.out.println(" (Q)uit");
System.out.println(" --------------------");
System.out.print("Enter the first letter your choice: ");
}
public static void main(String[] args) {
BookUI bookUI = new BookUI();
bookUI.mainMenu() ;
}
} // end Catalog class