001package myhw1; 002/** 003 * Utility class for Inventory. Fields are mutable and package-private. 004 * Does not override <code>equals</code> or <code>hashCode</code>. 005 * 006 * <p><b>Class Type:</b> Mutable Data Class</p> 007 */ 008final class Record { 009 /** 010 * The video. 011 * <p><b>Invariant:</b> <code>video != null</code></p> 012 */ 013 VideoObj video; 014 /** 015 * The number of copies of the video that are in the inventory. 016 * <p><b>Invariant:</b> <code>numOwned > 0</code></p> 017 */ 018 int numOwned; 019 /** 020 * The number of copies of the video that are currently checked out. 021 * <p><b>Invariant:</b> <code>numOut <= numOwned</code></p> 022 */ 023 int numOut; 024 /** 025 * The total number of times this video has ever been checked out. 026 * <p><b>Invariant:</b> <code>numRentals >= numOut</code></p> 027 */ 028 int numRentals; 029 030 /** 031 * Initialize all object attributes. 032 */ 033 Record(VideoObj video, int numOwned, int numOut, int numRentals) { 034 this.video = video; 035 this.numOwned = numOwned; 036 this.numOut = numOut; 037 this.numRentals = numRentals; 038 } 039 /** 040 * Return a shallow copy of this record. 041 */ 042 public Record copy() { 043 return new Record(video,numOwned,numOut,numRentals); 044 } 045 /** 046 * Return a string representation of the object in the following format: 047 * <code>"video [numOwned,numOut,numRentals]"</code>. 048 */ 049 public String toString() { 050 StringBuilder buffer = new StringBuilder(); 051 buffer.append(video); 052 buffer.append(" ["); 053 buffer.append(numOwned); 054 buffer.append(","); 055 buffer.append(numOut); 056 buffer.append(","); 057 buffer.append(numRentals); 058 buffer.append("]"); 059 return buffer.toString(); 060 } 061}