// LibrarySystem.pp // // Created by Cristina Videira Lopes on 12/20/94. // // This file contains the set of propagation patterns whose source vertex // is the LibrarySystem class; the operations defined here apply to objects // of this class. These are the current propagation patterns, logically // divided in three blocks: // // - Initialization and main loop: // *operation* void init_ops() // *operation* void run() // // - Menus and interaction with operator: // *operation* Operation* show_operations() // *operation* Operation* find_operation (int option) // // - Algorithms implemented over the Library System: // *operation* void show_books() // *operation* void show_users() // *operation* void search_book(IN char* keyword, OUT Book_List* bl) // *operation* void search_user(IN char* name, OUT User_List* ul) // // This file also contains three C functions for generic print outs and // string comparison, which are: // // void print_welcome() // void print_goodbye() // int partial_strcmp (char* s1, char* s2) // // // *operation* init_ops() // // Initialize list of operations provided by the Library System; for each // operation, instantiate the correspondent operation-object and initialize // it with the proper op_opcode. // *operation* void init_ops() *traverse* *from* LibrarySystem *to* {Exit, ShowBooks, SearchBook, ShowUsers, SearchUser, CheckOut, CheckIn, Invalid} *wrapper* LibrarySystem *prefix* (@ Operation_List *opl = new Operation_List; Operation *op = new Exit; opl->append (op); op = new ShowBooks; opl->append (op); op = new SearchBook; opl->append (op); op = new ShowUsers; opl->append (op); op = new SearchUser; opl->append (op); op = new CheckOut; opl->append (op); op = new CheckIn; opl->append (op); op = new Invalid; opl->append (op); this->set_ops(opl); @) *wrapper* Exit *prefix* (@ this->set_op_code (new DemNumber(EXIT)); @) *wrapper* ShowBooks *prefix* (@ this->set_op_code (new DemNumber(SHOW_BOOKS)); @) *wrapper* SearchBook *prefix* (@ this->set_op_code (new DemNumber(SEARCH_BOOK)); @) *wrapper* ShowUsers *prefix* (@ this->set_op_code (new DemNumber(SHOW_USERS)); @) *wrapper* SearchUser *prefix* (@ this->set_op_code (new DemNumber(SEARCH_USER)); @) *wrapper* CheckOut *prefix* (@ this->set_op_code (new DemNumber(CHECK_OUT)); @) *wrapper* CheckIn *prefix* (@ this->set_op_code (new DemNumber(CHECK_IN)); @) *wrapper* Invalid *prefix* (@ this->set_op_code (new DemNumber(INVALID)); @) // // *operation* void run() // // Main loop of Library System; display possibilities and execute the // chosen operation. // *operation* void run() *wrapper* LibrarySystem *prefix* (@ Operation *op = new Invalid; op->set_op_code (new DemNumber (INVALID)); print_welcome(); while (*(op->get_op_code()) != EXIT) { op = this->show_operations(); op->execute(this); } print_goodbye(); @) // // *operation* Operation* show_operations() // // Print out a menu with the possible operations on the Library System and // wait for human input. Returns the operation-object correspondent to the // number chosen by human operator. // *operation* Operation* show_operations() *traverse* *from* LibrarySystem *to* {Exit, ShowBooks, SearchBook, ShowUsers, SearchUser} *wrapper* LibrarySystem *prefix* (@ char option[100]; cout << "\n Possible operations are:\n"; @) *wrapper* LibrarySystem *suffix* (@ cout << "\n\n Enter your choice: " ; cin >> option; return_val = this->find_operation (atoi(option)); @) *wrapper* Exit *prefix* (@ cout << "\n " << *(this->get_op_code()) << " - Exit"; @) *wrapper* ShowBooks *prefix* (@ cout << "\n " << *(this->get_op_code()) << " - List of books"; @) *wrapper* SearchBook *prefix* (@ cout << "\n " << *(this->get_op_code()) << " - Search book(s)"; @) *wrapper* ShowUsers *prefix* (@ cout << "\n " << *(this->get_op_code()) << " - List of users"; @) *wrapper* SearchUser *prefix* (@ cout << "\n " << *(this->get_op_code()) << " - Search user(s)"; @) // // *operation* Operation* find_operation (int option) // // Given a number (op_code), return the correspondent operation-object; // the invalid-object will be returned for invalid op_codes. // *operation* Operation *find_operation (int option) *init* (@ NULL @) *traverse* *from* LibrarySystem *to* {Operation, CheckIn, CheckOut, Invalid} *wrapper* ~> Operation_List, Operation *suffix* (@ if (return_val != NULL) return; @) *wrapper* Operation *prefix* (@ if (option == *(this->get_op_code())) return_val = this; @) *wrapper* {CheckIn, CheckOut} *prefix* (@ @) *wrapper* Invalid *prefix* (@ return_val = this; @) // // *operation* void show_books() // // Print out a list of books // *operation* void show_books() *traverse* *from* LibrarySystem *to* Book_List *wrapper* LibrarySystem *prefix* (@ cout << "\n***Library System***\n"; @) *wrapper* Book_List *prefix* (@ this->print(); @) // // *operation* void show_users() // // Print out a list of users. // *operation* void show_users() *traverse* *from* LibrarySystem *to* User_List *wrapper* LibrarySystem *prefix* (@ cout << "\n ***Library System***\n"; @) *wrapper* User_List *prefix* (@ this->print(); @) // // *operation* void search_book (char* key, Book_List* bl) // // Given a key, search the Library System's list of books for occurrences // of that key, and return a list of books in which the key occurs. // *operation* void search_book (char* key, Book_List* bl) *traverse* *from* LibrarySystem *via* Book_List *bypassing* -> *, *, Copy_List *to* {Title, Author, Publisher, Subject} *carry* *in* Book* b = (@ this @), *inout* int match = (@ NOMATCH @) *along* *from* Book *to* {Title, Author, Publisher, Subject} *wrapper* Title *prefix* (@ if (partial_strcmp (key, *this->get_t()) == MATCH) { bl->append (b); match = MATCH; } @) *wrapper* Author *prefix* (@ if (match == NOMATCH) { if (partial_strcmp (key, *this->get_a()) == MATCH) { bl->append (b); match = MATCH; } } @) *wrapper* Publisher *prefix* (@ if (match == NOMATCH) { if (partial_strcmp (key, *this->get_p()) == MATCH) { bl->append (b); match = MATCH; } } @) *wrapper* Subject *prefix* (@ if (match == NOMATCH) { if (partial_strcmp (key, *this->get_s()) == MATCH) { bl->append (b); } } @) // // *operation* void search_user (char* name, User_List* ul) // // Given a name, search the Library System's list of users for occurrences // of that name (either in the users names or in the address), and return // the list of users in which the name occurs. // *operation* void search_user (char* name, User_List* ul) *traverse* *from* LibrarySystem *bypassing* -> *, *, User *to* User *wrapper* User *prefix* (@ if ((partial_strcmp (name, *this->get_fname()) == MATCH) || (partial_strcmp (name, *this->get_lname()) == MATCH)) { ul->append (this); } else { Address *a = this->get_address(); if ((partial_strcmp (name, *a->get_street()) == MATCH) || (partial_strcmp (name, *a->get_city()) == MATCH) || (partial_strcmp (name, *a->get_state()) == MATCH)) { ul->append (this); } } @) // // C generic functions // *C* (@ // this is a friendly welcome to the library system users void print_welcome() { cout << "\n"; cout << " ***********************************************************\n"; cout << " ********** Welcome to the Demeter Library System **********\n"; cout << " ***********************************************************\n"; } void print_goodbye() { cout << "\n"; cout << " ***********************************************************\n"; cout << " ********** Bye Bye **********\n"; cout << " ***********************************************************\n"; } int partial_strcmp (char* s1, char* s2) { int n = strlen (s1); int l = strlen (s2) - n; int i = 0; while (i <= l) { if (strncmp (s1, &s2[i++], n) == 0) return (MATCH); } return (NOMATCH); } @)