 Hello and welcome back, this is the second part of our lecture on programming using structures. Here is a quick recap of some of the relevant topics that we have already studied. We have seen a brief introduction to object oriented programming. We have seen how structures can be defined in C++, how their various members can be accessed and how structures can be initialized and copied. In the last lecture, we also saw some examples of programming using structures. In particular, we saw how to implement a function for checking out a book in a library information management system. In this lecture, we are going to see more programming using structures. Specifically, we are going to implement some of the other functionalities in the library information management system we have already discussed in the earlier lectures. Some examples in this lecture are from the book An Introduction to Programming through C++ by Aviram G. Ranade published by McGraw-Hell Education in 2014. All such examples will be indicated in slides with the citation AGR book. Now recall from the previous lectures that we were trying to design a library information management system that was motivated by discussions in AGR book. And in our library information management system, every pattern would have a unique numerical ID, every book would have a unique accession number. And we are going to allow three different operations, check out, claim and return. What is checkout? A pattern can check out up to three books at any time. And we have seen how to implement this functionality in C++ in the last lecture. What is a claim? If pattern X has not already checked out three books, then we will allow her to claim one book that is checked out by another pattern Y. And when pattern Y returns this book, it is going to be held for pattern X and not lent out to others. And of course, the return functionality is that a pattern should be allowed to return a book that she had checked out sometime earlier. We have seen in the previous lectures that a good way of going about writing a C++ program to implement the library information management system is to identify a book object and a pattern object. And specifically, we are going to implement these objects as C++ structures. So, the structure book is going to have different members which are going to store different information about the book like the title, authors, price, accession number, checkout status and claimant ID. And all the books in the library are basically going to be stored in the area library shelf, which we had decided will have 1000 elements. We had decided that the library is going to have 1000 books. And for purposes of our programming, we had also assumed that every element of library shelf will have its checkout status and claimant ID members initialize to false and minus one. Checkout status being false means the book has not been checked out. And claimant ID being minus one means there are no current claimants for this book. The pattern structure similarly has information about patterns of our library. So, this will have members like name, address, unique ID, number of books checked out and the accession number of any book claimed by the pattern. And we are going to have all the patterns of our library information about them stored in this array library patterns of size 200. We had decided that we are going to have 200 patterns for our library. And of course, each element of this array library patterns is a structure of type pattern. And once again for purposes of our programming, we had assumed that every element in this array library patterns will have its members named num books checked out and claimed book accession number initialize to 0 and minus one. Num books checked out being 0 means that the pattern currently does not have any book checked out in her account. And claimed book accession number being minus one means that the pattern does not currently have a pending claim for any book. Now, in the earlier lecture, we had seen how to implement the checkout functionality. Let us now see how we can implement the claim functionality. So, what is the claim functionality? Let us say a pattern denoted by current pattern of type pattern wants to claim a book denoted by current book of type book. So, what is involved in this process? Well, we have to first check if current pattern has already checked out three books or if she already has a pending claim. In either of these cases, we are not going to allow current pattern to register a claim for another book. So, we are going to print an appropriate message and return. Otherwise, we will have to check if current book is available and has not yet been checked out by anybody. If so, then we can just tell current pattern that there is no need to place a claim for this book. She can just go ahead and check this book out. If both of these checks fail, then we have to still check if current book has already been claimed by a different pattern. If so, then we cannot let current pattern lay claim to current book. Otherwise, we can allow current pattern to register a claim for current book in which case we are going to store the accession number of current book in the member claimed book accession number of current pattern. And similarly, we are going to store the unique ID of current pattern in the member claimant ID of current book. Let us see how we might implement this as a C plus plus function. So, here is the claim book function. We are going to pass current pattern and current book to this function both by reference as was discussed in the previous lecture. What is the precondition of claim book that the members of current pattern and current book are properly initialized? There are no garbage values. And what is the post condition? We would like to ensure that a claim of current pattern on current book would be registered. If it is allowed, there are several conditions as we have just seen. And if those conditions allow us to register a claim of current pattern and current book, we would like that claim to be registered and we would have to update the members of current pattern and current book appropriately. So, how does the code for claim book look like? Well, here is an initial part of the code. Here we are first checking if the number of books checked out by current pattern is already 3. If that is the case, then we cannot allow the current pattern to claim any book any further. Or if the current pattern already has made claim to a book. So, if the claimed book accession number of current pattern is not minus 1, then she has already laid claim to some other book. So, we cannot allow the pattern to claim any other book. If this check does not pass, basically these conditions both evaluate to false, then what do we do? We basically go out and check if the current book is still available and has not been checked out by others. So, the checkout status of current book is false. Then we are going to simply tell current pattern that this book has not yet been checked out. There is no need for a claim. Current pattern might as well go ahead and check out this book. Well, if that is also not the case, then we have to check if there is already somebody who has laid claim to current book and if that somebody is different from current pattern. So, we have to check if there is a claimant for current book, which means the claimant ID for current book is not minus 1 and if this claimant ID is different from current patron's unique ID, then we just go to output a message saying that sorry, the book has already been claimed by a patron and if none of these checks pass, then we can allow current patron to register a claim for current book and how does that happen? We basically copy current book's accession number to the claimed book accession number member of current patron and we copy the unique ID of current patron to the claimant ID member of current book and we return. So, this is how we can implement claiming of a book in our library information management system. Well, but if you just think about it, a strange scenario can arise. What happens if a patron has already checked out a book and then she is trying to claim this book again and let us say this is the only book that she has checked out. So, she has really checked out one book and she again comes back and tries to claim that same book again. What happens in such a scenario? I will encourage all of you to think about this and also to try to understand why does this happen. We will address this in a few slides ahead in this lecture. Well, how would we write a C++ function for returning a book? So, just like for claiming a book and for checking out a book, we would write a function to which we would pass current patron and current book as parameters passed by reference. Here as precondition, we are going to assume that members of current book and current patron are properly initialized and we will also assume very importantly that current patron had indeed checked out current book. That current patron is not trying to return a book that she had not checked out. And as post condition, we want the members of current patron and current book to be updated appropriately so that we register the return of current book by current patron. So, how does the code for return book look like? Well, we are going to check if the number of books checked out by current patron is greater than 0, then we are going to decrement the number of books checked out for current patron and we are going to set the checkout status for current book to false. Otherwise, if the number of books checked out for current patron is already 0 and if she is trying to return one more book, then something fishy is happening. So, we will just print out this message saying that are you trying to return somebody else's book. Well, that looks simple, but once again, a strange scenario can arise here. What if a patron is trying to return a book that she has not checked out, but somebody else has checked out? It looks like our function return book would basically reduce the number of books checked out count for current patron even though she is returning a book checked out by some other patron. And once again, I would encourage you to think why does this happen? We will address this soon in a few slides. So, well, in trying to answer why do those strange scenarios happen, one can make a few observations. When a patron checks out a book, the unique idea of the patron is never recorded with the book. Similarly, the accession numbers of the books that a patron has checked out are never recorded with the patron. So, one might ask that if we kept track of the above information, can we avoid those strange scenarios that we just saw? Well, so let us try to see how we would keep track of that additional information. So, desirable change, albeit a late change would be to add some additional members to our structures. So, you might want to add a member called borrower ID to the structure book and a member which is an array of size 3, each element of that array being an integer and this array is basically the accession numbers of the borrowed books. We would like to add this member array to the structure patron. But now after having implemented checkout book, claim book and return book, how do we change things now? Do we have to go back and restart everything all over again? Well, here is where object oriented modular programming comes to our rescue and let us see how we are actually going to incorporate these incremental changes. So, what we want is in structure book, we want an additional member called borrower ID and in structure patron, we want an additional member array called borrowed accession number. So, every element of the arrays, library patterns and library shelf would now have these additional members. And for purpose of our programming, we will assume that all of these members are initialized to minus 1. So, now how would checkout book be modified? Well, after ascertaining that current patron can indeed checkout current book, what we have to do is we will have to iterate through this array borrowed accession number for current patron. And whenever we find an element of the array that is minus 1, we can copy the accession number of current book to that entry of the array borrowed accession number for current patron. And of course, we have to copy the unique ID of current patron to the borrower ID member of current book. How do we modify claim book? Well, after ascertaining that current patron can indeed claim current book, we have to check if the borrower ID of the current book is the same as the unique ID of current patron. And then we can just print out this message saying that the claim book has already been checked out by the same patron. The black part here and in the previous slide was what we already had earlier, the bold faced red part is the new part that we are adding to the functionality. And how do we incrementally modify return book? Well, we have to first check if the borrower ID of current book matches the unique ID of current patron. And if so, we can decrement num books checked out, we can reset the check out status of current book to false. And we must also iterate through this array borrowed accession number for current patron. And whichever entry in this array matches the accession number of current book, we have to reset that entry to minus 1. And finally, we have to reset borrower ID of current book to minus 1. So, in summary in this lecture, we saw a little bit more on programming using structures. Specifically, we saw how the claim book and return book functionality could be implemented in the library information management system. We also learned how to incrementally change structure definitions and functions using them. Basically, this is modular incremental software development and the use of object oriented programming, the use of structures helps us to do this. Thank you.