ECP03
Create a project and a folder for this exercise.
Copy the following classes from ECP04: Book, CD, Product, ProductCatalog and TestProductCatalog.
The Browseproducts use case (see below) describes that browsing should be done in pages containing a maximum of 10 products
Now we'll adapt TestProductCatalog so it implements something like that - but to avoid having to enter more than 10 books we'll only have a maximum of 2 products at the time!
Rename the text of the "Show list" button into "Previous" and add a button labeled "Next":
To control which and how many products to show in the output area we need two things added to the code for the form:
- a variable (idxOfFirst) which should hold the index value (from the productCatalog) for the very first product to be shown in the area
- a constant: private static final int PAGE_SIZE = 2; to tell the size of the page
The code for the "Previous" button should be changed according to the following instructions:
- if the idxOfFirst is 0no action should be performed (there is nothing previous to index 0)
- in all other situations the output area should be cleared
- then the idxOfFirst should be decremented with PAGE_SIZE
- notice that the prior action could make idxOfFirst negative (consider when this could happen) - if that is the case we must change it into 0 (so the first product to be shown is the one with index 0)
- finally perform a loop that gets the descriptions of the products from the productCatalog using the get() method. First product to be shown must be the one with the index equal to idxOfFirst. A maximum of PAGE_SIZE product should be added to the output area
Hint: To add a number of Strings to a JTextArea you can add each String using the append() method.
The code for the "Next" button works in a similar way:
- if the idxOfFirst is less than (number of products minus PAGE_SIZE) no action should be performed (last product is already shown)
- in all other situations the output area should be cleared
- then the idxOfFirst should be incremented with PAGE_SIZE
- notice that the prior action could make idxOfFirst too large. Consider when this could happen and what value is the largest allowed for idxOfFirst. If the value gets too large it must be adjusted.
- finally perform a loop that gets the descriptions of the products from the productCatalog using the get() method. First product to be shown must be the one with the index equal to idxOfFirst. A maximum of PAGE_SIZE product should be added to the output area
Run TestProductCatalog, enter a number of books and test that the two buttons work as they should - if not then correct your code!