LibrarySystem { (@ public void Add_It (User us) { /* Add User to the list of users */ if(get_users() != null ) get_users().set_first(new Nonempty_User_List ( us, get_users().get_first())); else set_users( new User_List (new Nonempty_User_List( us , null ))); } public void Add_It (Staff sf) { /* Add Staff to the list of staff personell */ if(get_staff() != null ) get_staff().set_first(new Nonempty_Staff_List( sf, get_staff().get_first())); else set_staff( new Staff_List ( new Nonempty_Staff_List ( sf, null ))); } public void Add_It (BookEntry be , Integer num) { /* Add book entry */ Copy_List list = new Copy_List(); list.set_first( generate_list(num) ); Book newbook = new Book( be, list ); if(get_books() != null) { Nonempty_Book_List newlist = new Nonempty_Book_List ( newbook , get_books().get_first()); get_books().set_first( newlist ); } else { Nonempty_Book_List newlist = new Nonempty_Book_List ( newbook, null); set_books( new Book_List (newlist) ); } } private Nonempty_Copy_List generate_list (Integer i) { /* service procedure */ if (i.intValue() == 1) return new Nonempty_Copy_List ( new Copy (i,(User) null, new Boolean(true) ), null ); else return new Nonempty_Copy_List ( new Copy (i,(User) null, new Boolean(true)), generate_list ( new Integer(i.intValue() - 1)) ); } @) traversal PrintUsers (PrintingVisitor pv) { bypassing Book_List to-stop User; } traversal Add_Copy (AddingVisitor av) { bypassing User_List to-stop Book; } traversal SearchBook (SearchingVisitorB svB) { bypassing User_List to-stop Book; } traversal FindBook (SearchingVisitorISBN svISBN) { bypassing User_List to-stop Book; } traversal SearchUser (SearchingVisitorU svU) { bypassing Book_List to-stop User; } traversal SearchStaff (SearchingVisitorS svS) { to-stop Staff; } } Book_List { traversal PrintBooks (PrintingVisitor pv) { to-stop { BookEntry, Copy }; } } Book { (@ public Copy get_copy(Integer number) { Nonempty_Copy_List copy = get_copies().get_first(); while(copy.get_it().get_number().equals(number) == false) copy = copy.get_next(); return copy.get_it(); } @) } User { (@ public void AddCheckOut(CheckedOut item) { /* Insert a book into the list of checked books */ if(get_books() != null) try { Nonempty_CheckedOut_List current_item = get_books().get_first(); do { if(current_item.get_it().get_entry().get_isbn().equals(item.get_entry().get_isbn())) throw new Exception("consistency violation"); if( current_item.get_next() != null ) current_item = current_item.get_next(); } while(current_item.get_next() != null); if(current_item.get_it().get_entry().get_isbn().equals(item.get_entry().get_isbn())) throw new Exception("consistency violation"); item.get_copy().set_status( new Boolean(false) ); current_item.set_next( new Nonempty_CheckedOut_List ( item, null)); } catch (Exception e) { System.out.println("can't check out this book - user has a copy of it");} else { item.get_copy().set_status( new Boolean(false) ); set_books( new CheckedOut_List( new Nonempty_CheckedOut_List (item,null))); } } public void RemoveCheckOut(Ident isbn) { /* Removes book from the list of cheked out books */ Nonempty_CheckedOut_List current = get_books().get_first(); if(current.get_it().get_entry().get_isbn().equals(isbn)) get_books().set_first(current.get_next()); else { while (current.get_next().get_it().get_entry().get_isbn().equals(isbn) == false) current = current.get_next(); current.set_next(current.get_next().get_next()); } set_nbooks( new Integer ( get_nbooks().intValue() -1 )); } @) traversal PrintCheckedOut(PrintingVisitor pv) { bypassing Copy to-stop BookEntry; } } ListOfCommands { (@ void Do_It(ActionVisitor av) { try { this.AllCommands(new GuardingVisitor(av), av); } catch (Exception e) { av.set_lsystem(av.get_store()); } System.out.println(" "); } @) traversal AllCommands(GuardingVisitor gv, ActionVisitor av) { to * ; } } ActionVisitor { before Nonempty_Command_List (@ System.out.println(" "); @) after Nonempty_Command_List (@ set_store(get_lsystem()); @) before UserType (@ get_lsystem().Add_It(new User(host.get_name(),host.get_quota(),null,new Integer(0))); System.out.println (" Creating user account - USER:"+ host.get_name()+" ..."); @) before StaffType (@ get_lsystem().Add_It(new Staff(host.get_name(),host.get_password())); System.out.println (" Creating staff account - STAFF:"+ host.get_name()+" ..."); @) before ListUsers (@ /* List Users Command */ System.out.println (" Printing users "); System.out.println (" ------------------------ "); PrintingVisitor pv = new PrintingVisitor(); get_lsystem().PrintUsers(pv); @) before AddBook (@ /* Add Book Command */ get_lsystem().Add_It( host.get_entry() , host.get_number()); System.out.println (" Adding book entry - Book: "+ host.get_entry().get_title()+" ..."); @) before AddCopy (@ /* Add additional copy of existing book */ get_lsystem().Add_Copy(new AddingVisitor(host.get_isbn())); System.out.println (" Adding copy of book - ISBN: "+ host.get_isbn()+" ..." ); @) before ListBooks (@ /* Printing the list of books */ System.out.println(" Printing books... "); get_lsystem().get_books().PrintBooks( new PrintingVisitor() ); @) before AuthorSearch (@ /* Search book by author */ SearchingVisitorB svB = new SearchingVisitorB (host.get_author(),null,null); get_lsystem().SearchBook(svB); System.out.println(" Looking for book by " + host.get_author() + "..."); svB.get_books().PrintBooks( new PrintingVisitor() ); @) before SubjectSearch (@ /* Search book by subject */ SearchingVisitorB svB = new SearchingVisitorB(null,host.get_subject(),null ); get_lsystem().SearchBook(svB); System.out.println(" Looking for book about " + host.get_subject() + "..."); svB.get_books().PrintBooks( new PrintingVisitor() ); @) before CheckOut (@ /* Check out a copy of the book */ SearchingVisitorISBN svISBN = new SearchingVisitorISBN (host.get_isbn(), null); get_lsystem().FindBook(svISBN); Copy copy = svISBN.get_book().get_copy(host.get_number()); if(copy.get_status().equals(new Boolean(true))) { SearchingVisitorU svU = new SearchingVisitorU ( new User(host.get_name(),null,null,null)); get_lsystem().SearchUser(svU); if(svU.get_user().get_nbooks().intValue() < svU.get_user().get_quota().intValue()) { svU.get_user().set_nbooks( new Integer(svU.get_user().get_nbooks().intValue() + 1 )); copy.set_borrower(svU.get_user()); System.out.println(" Checking out book - ISBN:" + host.get_isbn() + " copy#" + host.get_number() + " for user:" + host.get_name() + " ..."); svU.get_user().AddCheckOut(new CheckedOut(svISBN.get_book().get_entry(),copy)); } else System.out.println(" can't exceed quota "); } else System.out.println(" copy is not available "); @) before Return (@ /* Returning a copy of a book */ SearchingVisitorISBN svISBN = new SearchingVisitorISBN (host.get_isbn(),null); get_lsystem().FindBook(svISBN); Copy copy = svISBN.get_book().get_copy(host.get_number()); copy.set_status(new Boolean(true)); copy.get_borrower().RemoveCheckOut(host.get_isbn()); System.out.println(" Checking in book - ISBN:" + host.get_isbn()+" COPY#" +host.get_number()); @) before ShowStatus (@ /* Show books borrowed by user */ SearchingVisitorU svU = new SearchingVisitorU(new User(host.get_name(),null,null,null)); get_lsystem().SearchUser(svU); System.out.println(" Books borrowed by user " + host.get_name()+":"); svU.get_user().PrintCheckedOut(new PrintingVisitor()); @) before UserStatus (@ /* Show books borrowed by user - user version */ SearchingVisitorU svU = new SearchingVisitorU(new User(get_user(),null,null,null)); get_lsystem().SearchUser(svU); System.out.println(" Books borrowed by user " + get_user()+":"); svU.get_user().PrintCheckedOut(new PrintingVisitor()); @) before LastUser (@ /* Find out the user who checkes this book last */ SearchingVisitorISBN svISBN = new SearchingVisitorISBN (host.get_isbn(),null); get_lsystem().FindBook(svISBN); System.out.println(" Book - ISBN:"+host.get_isbn()+" COPY#" + host.get_number() + " has been last checked out by USER:" + svISBN.get_book().get_copy(host.get_number()).get_borrower().get_name()); @) before StartC (@ /* Execute a sequence of commands from file specified */ ListOfCommands commands = null; try { commands = ListOfCommands.parse(new FileInputStream(host.get_file().toString())); } catch (Exception e) { System.out.println("File not found");} commands.Do_It(this); @) before UserMode (@ /* Switch to the User Mode */ set_mode(new Integer(1)); set_user(host.get_name()); System.out.println(" user mode - USER:"+host.get_name()); @) before StaffMode (@ /* Switch to the Staff Mode */ SearchingVisitorS svS = new SearchingVisitorS(new Staff(host.get_name(), null)); get_lsystem().SearchStaff(svS); if(svS.get_staff().get_password().equals(host.get_password())) { set_mode(new Integer(2)); set_user(host.get_name()); System.out.println(" staff mode - STAFF:" + host.get_name()); } else System.out.println(" error: check name and password and try again"); @) before Logout (@ System.out.println(" logging out ..."); set_mode(new Integer(0)); @) } GuardingVisitor { before StaffCommand (@ if (get_av().get_mode().intValue() < 2 ) { System.out.println("permission denied - this command for staff use only"); get_av().set_lsystem(null); } @) before UserCommand (@ if (get_av().get_mode().intValue() < 1) { System.out.println("register as a user first..."); get_av().set_lsystem(null); } @) } SearchingVisitorB { before Book (@ if ( host.get_entry().get_author().equals(get_author()) || host.get_entry().get_subject().equals(get_subject())) if ( get_books() != null ) get_books().set_first( new Nonempty_Book_List ( host , get_books().get_first()) ); else set_books(new Book_List ( new Nonempty_Book_List (host , null) )); @) } SearchingVisitorU { before User (@ if ( host.get_name().equals(get_user().get_name()) ) set_user(host); @) } SearchingVisitorS { before Staff (@ if (host.get_name().equals(get_staff().get_name()) ) set_staff(host); @) } SearchingVisitorISBN { before Book (@ if ( host.get_entry().get_isbn().equals(get_isbn()) ) set_book(host); @) } PrintingVisitor { before User (@ System.out.println (" USER:" + host.get_name() + " QUOTA:" +host.get_quota() ); @) before BookEntry (@ System.out.println ("----------------------------------------"); System.out.println ( "Book: title:" + host.get_title() + " author:" + host.get_author() + " subject:" + host.get_subject() + " ISBN:" + host.get_isbn() ); @) after Nonempty_Copy_List (@ String stat; if (host.get_it().get_status().equals(new Boolean(false))) stat = new String("checked out"); else stat = new String("available"); System.out.println ( "Copy #" + host.get_it().get_number() + " -- Status:" + stat ); @) after CheckedOut (@ System.out.println ("Copy #"+host.get_copy().get_number()); @) } AddingVisitor { before Book (@ if( host.get_entry().get_isbn().equals(get_isbn()) ) { Nonempty_Copy_List newlist = new Nonempty_Copy_List ( new Copy (new Integer(host.get_copies().get_first().get_it().get_number().intValue() + 1), null, new Boolean(true)), host.get_copies().get_first() ); host.get_copies().set_first( newlist ); } @) } Main { (@ static public void main(String args[]) throws Exception { System.out.println(" *** START *** "); LibrarySystem lsys = new LibrarySystem (null, null, null); DataInputStream in = new DataInputStream( System.in ); ActionVisitor av = new ActionVisitor(lsys, new Integer(2), null, lsys); System.out.println("Welcome to the Library System"); System.out.print("library>"); System.out.flush(); String line = in.readLine(); while (line.equals("QUIT") == false) { try { ListOfCommands commands = ListOfCommands.parse(line); commands.Do_It(av); } catch (Exception e) { System.out.println("Syntax Error"); } System.out.print("library>");System.out.flush(); line = in.readLine(); } System.out.println(" *** DONE *** "); } @) }