/** * A library member is defined by an ssn. The member can check out only one * book. * * @author CSC 142 * */ public class LibraryMember { // a library member is defined by // an ssn and a book // Note: we wrongly assume that an ssn doesn't start with // 0 (a String would be better, but an int is convenient) private int ssn; private Book book; /** * Creates a library member given his/her ssn * * @param theSSN * the ssn of the library member */ public LibraryMember(int theSSN) { if (checkSSN(theSSN)) { ssn = theSSN; } else { // It would better to throw an exception System.out.println("Invalid ssn value = " + theSSN); } } /** * Returns the book (if there is one) * * @return true if the operation was successfull, or false otherwise */ public boolean returnBook() { if (book != null) { // there is a book to return // \" prints a double quote System.out.println("The book titled \"" + book.getTitle() + "\" has been returned."); book = null; return true; } else { // there is no book to return System.out.println("There is no book to return."); return false; } } /** * Borrows a book given its title * * @param title * the title of the book * @return true if the book could be borrowed, or false otherwise */ public boolean borrowBook(String title) { if (book == null) { // the book can be borrowed book = new Book(title); System.out.println("The book titled \"" + title + "\" has been borrowed."); return true; } else { // can't borrow the book System.out .println("You can't borrow a book. You already have one (\"" + book.getTitle() + "\")!"); return false; } } /** * Returns true if the given ssn is valid, or false otherwise * * @param ssn * the ssn to check * @return true if the given ssn is valid, or false otherwise */ private boolean checkSSN(int ssn) { if (ssn <= 0) { return false; } // the ssn should have nine digits (but doesn't work if // the leading digits are 0's) if (ssn > 999999999) { return false; } if (ssn < 100000000) { return false; } // Other way to do the same thing: // Integer theSSN = new Integer(ssn); // if (theSSN.toString().length() != 9) { // return false; // } // if ( (ssn + "").length() != 9) { // return false; // } return true; } }