001package myhw1; 002import java.util.Map; 003import java.util.HashMap; 004import java.util.Collection; 005 006// TODO: complete the methods 007/** 008 * An Inventory implemented using a <code>HashMap<Video,Record></code>. 009 * Keys are Videos; Values are Records. 010 * 011 * <p><b>Class Type:</b> Mutable Collection of Records</p> 012 * <p><b>Object Invariant:</b></p> 013 * Every key and value in the map is non-<code>null</code>. 014 * <p><b>Object Invariant:</b></p> 015 * Each value <code>r</code> is stored under key <code>r.video</code>. 016 */ 017final class InventorySet { 018 /** <p><b>Invariant:</b> <code>_data != null</code> </p>*/ 019 private final Map<VideoObj,Record> data = new HashMap<VideoObj,Record>(); 020 021 InventorySet() { } 022 023 /** 024 * Return the number of Records. 025 */ 026 public int size() { 027 // TODO 028 return 0; 029 } 030 031 /** 032 * Return a copy of the record for a given Video; if not present, return <code>null</code>. 033 */ 034 public Record get(VideoObj v) { 035 // TODO 036 return null; 037 } 038 039 /** 040 * Return a copy of the records as a collection. 041 * Neither the underlying collection, nor the actual records are returned. 042 */ 043 public Collection<Record> toCollection() { 044 // Recall that an ArrayList is a Collection. 045 // TODO 046 return null; 047 } 048 049 /** 050 * Add or remove copies of a video from the inventory. 051 * If a video record is not already present (and change is 052 * positive), a record is created. 053 * If a record is already present, <code>numOwned</code> is 054 * modified using <code>change</code>. 055 * If <code>change</code> brings the number of copies to be zero, 056 * the record is removed from the inventory. 057 * @param video the video to be added. 058 * @param change the number of copies to add (or remove if negative). 059 * @throws IllegalArgumentException if video null, change is zero, 060 * if attempting to remove more copies than are owned, or if 061 * attempting to remove copies that are checked out. 062 * <p><b>Postcondition:</b> changes the record for the video</p> 063 */ 064 public void addNumOwned(VideoObj video, int change) { 065 // TODO 066 } 067 068 /** 069 * Check out a video. 070 * @param video the video to be checked out. 071 * @throws IllegalArgumentException if video has no record or numOut 072 * equals numOwned. 073 * <p><b>Postcondition:</b> changes the record for the video</p> 074 */ 075 public void checkOut(VideoObj video) { 076 // TODO 077 } 078 079 /** 080 * Check in a video. 081 * @param video the video to be checked in. 082 * @throws IllegalArgumentException if video has no record or numOut 083 * non-positive. 084 * <p><b>Postcondition:</b> changes the record for the video</p> 085 */ 086 public void checkIn(VideoObj video) { 087 // TODO 088 } 089 090 /** 091 * Remove all records from the inventory. 092 * <p><b>Postcondition:</b> <code>size() == 0</code></p> 093 */ 094 public void clear() { 095 // TODO 096 } 097 098 /** 099 * Return the contents of the inventory as a string. 100 */ 101 public String toString() { 102 StringBuilder buffer = new StringBuilder(); 103 buffer.append("Database:\n"); 104 for (Record r : data.values()) { 105 buffer.append(" "); 106 buffer.append(r); 107 buffer.append("\n"); 108 } 109 return buffer.toString(); 110 } 111}