First a book is selected at random from the library's allBooks collection,
using the chooseBook method.
If the book is not already out, the borrower checks it out
by setting its myBorrower reference to self.
Otherwise, no action is taken.
The timerEvent method, see below, calls this method.
Instructions
- In the Class Browser, select the Borrower class.
- Select the Methods | New JADE Method... menu.
- Enter checkout and click the OK button.
- Copy and compile the code on the right.
|

checkout();
vars
bk : Book;
begin
bk := LendingLibrary.firstInstance.allBooks.chooseBook;
if bk.myBorrower = null then
bk.myBorrower := self;
endif;
end; | |
First a book is selected at random from the borrower's myBooks collection,
using the chooseBook method.
The borrower checks the book in by setting its myBorrower reference
to null.
The timerEvent method, see below, calls this method.
Instructions
- In the Class Browser, select the Borrower class.
- Select the Methods | New JADE Method... menu.
- Enter checkin and click the OK button.
- Copy and compile the code on the right.
|

checkin();
vars
bk : Book;
begin
bk := myBooks.chooseBook;
bk.myBorrower := null;
end; | |
This method is executed when a timer signal is received.
If the borrower has no books checked out, the checkout method is called.
If the borrower has 4 books (maximum allowed)
checked out, the checkin method is called.
Otherwise each method is called with a 50-50 chance.
Then the timer is restarted.
Change the timer period (currently 1000)
and the maximum number of books (currently 4) if you want.
Instructions
- In the Class Browser, select the Borrower class.
- Select the Methods | New JADE Method... menu.
- Enter timerEvent and click the OK button.
You are advised that you are reimplementing a superclass method.
Click Yes to continue.
- Copy and compile the code on the right.
|

timerEvent(eventTag : Integer);
begin
beginTransaction;
if myBooks.size = 0 then checkout;
elseif myBooks.size = 4 then checkin;
elseif app.random(1).isOdd then checkout;
else checkin;
endif;
commitTransaction;
beginTimer(1000, Timer_OneShot, 0);
end; | |