:!: Please look at all my message it is not long as it seems. My question is at the end and thank you in advance.
Here is a small class called Record that is a utility class for the InventorySet class.
final class Record implements Comparable
{
/**
* The video.
* <p><b>Invariant:</b> <code>video() != null</code>.</p>
*/
VideoObj video;
/**
* The number of copies of the video that are in the inventory.
* <p><b>Invariant:</b> <code>numOwned > 0</code>.</p>
*/
int numOwned;
/**
* The number of copies of the video that are currently checked out.
* <p><b>Invariant:</b> <code>numOut <= numOwned</code>.</p>
*/
int numOut;
/**
* The total number of times this video has ever been checked out.
* <p><b>Invariant:</b> <code>numRentals >= numOut</code>.</p>
*/
int numRentals;
/**
* Initialize all object attributes.
*/
Record(VideoObj video, int numOwned, int numOut, int numRentals) {
this.video = video;
this.numOwned = numOwned;
this.numOut = numOut;
this.numRentals = numRentals;
}
/**
* Return a copy of this record.
*/
public Record copy() {
return new Record(video,numOwned,numOut,numRentals);
}
/**
* Delegates to video.
* <p><b>Requires:</b> thatObject to be a Record.</p>
*/
public boolean equals(Object thatObject) {
return video.equals(((Record)thatObject).video);
}
Now here is the InventorySet class
final class InventorySet {
private Map _data;
int thesize;
InventorySet() {
// TODO
_data = new HashMap();
}
/**
* Return the number of Records.
*/
public int size() {
// TODO
thesize = _data.size();
return thesize;
}
/**
* Return a copy of the records as a collection.
* Neither the underlying collection, nor the actual records are returned.
*/
public Collection toCollection() {
// Recall that an ArrayList is a Collection.
// TODO
ArrayList list = new ArrayList();
String val;
Iterator i = _data.values().iterator();
while (i.hasNext())
{
val = (String) i.next();
list.add (val);
}
Collection copyList = list;
return copyList;
}
/**
* Check out a video.
* @param video the video to be checked out.
* @throws IllegalArgumentException if video has no record or numOut
* equals numOwned.
*/
public void checkOut(VideoObj video) {
// TODO
Object m = _data.get(video);
if(m == null )
throw new IllegalArgumentException();
_data.remove(video);
}
Now my question is: The professor wants us to throw an IllegalArgumentException in case there is no record or if numout is < 0. How can I access numOut a variable of the Record class if I don't have anything to allow me to create an object of the record class??
I even tried to do the following:
Record c; Record r = c.copy();
if(m == null || r.numout < 0)
throw new IllegalArgumentException();
However i got a compilation error indicating that c might not have been initialized. Well, that is right. How can i access numout ??
Thank you for your help.
Dounia