Java Reference
In-Depth Information
* @return The number of members who joined in that month.
*/
public int joinedInMonth(int month)
If the month parameter is outside the valid range of 1 to 12, print an error message and return
zero.
Exercise 4.55 Define a method in the Club class with the following description:
/**
* Remove from the club's collection all members who
* joined in the given month, and return them stored
* in a separate collection object.
* @param month The month of the membership.
* @param year The year of the membership.
* @return The members who joined in the given month and year.
*/
public ArrayList<Membership> purge(int month, int year)
If the month parameter is outside the valid range of 1 to 12, print an error message and return
a collection object with no objects stored in it.
Note: The purge method is significantly harder to write than any of the others in this class.
Exercise 4.56 Open the product project and complete the StockManager class through
this and the next few exercises. StockManager uses an ArrayList to store Product
items. Its addProduct method already adds a product to the collection, but the following
methods need completing: delivery , findProduct , printProductDetails , and
numberInStock .
Each product sold by the company is represented by an instance of the Product class,
which records a product's ID, name, and how many of that product are currently in stock. The
Product class defines the increaseQuantity method to record increases in the stock
level of that product. The sellOne method records that one item of that product has been
sold, by reducing the quantity field level by 1. Product has been provided for you, and you
should not need to make any alterations to it.
Start by implementing the printProductDetails method to ensure that you are able to
iterate over the collection of products. Just print out the details of each Product returned, by
calling its toString method.
Exercise 4.57 Implement the findProduct method. This should look through the collection
for a product whose id field matches the ID argument of this method. If a matching product is
found, it should be returned as the method's result. If no matching product is found, return null .
This differs from the printProductDetails method in that it will not necessarily have
to examine every product in the collection before a match is found. For instance, if the first
 
Search WWH ::




Custom Search