could some one explain the code below

import java.util.Map;
import java.util.HashMap;
import java.util.Collection;
import java.util.ArrayList;


final class InventorySet {
  /** @invariant <code>_data != null</code> */
  private final Map<VideoObj,Record> _data = new HashMap<VideoObj,Record>();

  InventorySet() { }

  /**
   * Return the number of Records.
   */
  public int size() {
     // fill in the code
    return 0;
  }

  
  public Record get(VideoObj v) {
    // fill in the code

    return null;
  }


  public Collection<Record> toCollection() {
    // Recall that an ArrayList is a Collection.
    // TODO
    return null;
  }

  /**
   * Add or remove copies of a video from the inventory.
   * If a video record is not already present (and change is
   * positive), a record is created. 
   * If a record is already present, <code>numOwned</code> is
   * modified using <code>change</code>.
   * If <code>change</code> brings the number of copies to be zero,
   * the record is removed from the inventory.
   * @param video the video to be added.
   * @param change the number of copies to add (or remove if negative).
   * @throws IllegalArgumentException if video null, change is zero,
   *  if attempting to remove more copies than are owned, or if
   *  attempting to remove copies that are checked out.
   * @postcondition changes the record for the video
   */
  public void addNumOwned(VideoObj video, int change) {
    // TODO
  }

  /**
   * Check out a video.
   * @param video the video to be checked out.
   * @throws IllegalArgumentException if video has no record or numOut
   * equals numOwned.
   * @postcondition changes the record for the video
   */
  public void checkOut(VideoObj video) {
    // TODO
  }
  
  /**
   * Check in a video.
   * @param video the video to be checked in.
   * @throws IllegalArgumentException if video has no record or numOut
   * non-positive.
   * @postcondition changes the record for the video
   */
  public void checkIn(VideoObj video) {
    // TODO
  }
  
  /**
   * Remove all records from the inventory.
   * @postcondition <code>size() == 0</code>
   */
  public void clear() {
    // fill in the code
  }

  
  public String toString() {
    StringBuilder buffer = new StringBuilder();
    buffer.append("Database:\n");
    for (Record r : _data.values()) {
      buffer.append("  ");
      buffer.append(r);
      buffer.append("\n");
    }
    return buffer.toString();
  }
}

thanks in advance

Recommended Answers

All 5 Replies

what code? except for the toString method, there are only mock methods that are labelled as 'TODO'.
what exactly do you need help with?

what code? except for the toString method, there are only mock methods that are labelled as 'TODO'.
what exactly do you need help with?

say for example
the method

public int size() {
// TODO
_data.size(); //returns the size of the hashmap right. how should i code it when
// the size return 0 , does it have to have a if else to check the zero?
return 0;
}

have you tried coding it?

No, the size is the size. You can just return it, regardless of whether it's zero or more.
The return 0; is in there because the compiler will generate an error if that method doesn't return something. When you put in the correct code you can discard the return 0;

have you tried coding it?

yes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.