001package myhw3.data; 002 003import myhw3.command.Command; 004 005/** 006 * A static class for accessing data objects. 007 */ 008public class Data { 009 private Data() {} 010 /** 011 * Returns a new Inventory. 012 */ 013 static public final Inventory newInventory() { 014 return new InventorySet(); 015 } 016 017 /** 018 * Factory method for Video objects. 019 * Title and director are "trimmed" to remove leading and final space. 020 * @throws IllegalArgumentException if Video invariant violated. 021 */ 022 static public Video newVideo(String title, int year, String director) { 023 if ( (title == null) 024 || (director == null) 025 || (year <= 1800) 026 || (year >= 5000)) { 027 throw new IllegalArgumentException(); 028 } 029 title = title.trim(); 030 director = director.trim(); 031 if ( ("".equals(title)) 032 || ("".equals(director))) { 033 throw new IllegalArgumentException(); 034 } 035 return new VideoObj(title, year, director); 036 } 037 038 /** 039 * Returns a command to add or remove copies of a video from the inventory. 040 * <p>The returned command has the following behavior:</p> 041 * <ul> 042 * <li>If a video record is not already present (and change is 043 * positive), a record is created.</li> 044 * <li>If a record is already present, <code>numOwned</code> is 045 * modified using <code>change</code>.</li> 046 * <li>If <code>change</code> brings the number of copies to be less 047 * than one, the record is removed from the inventory.</li> 048 * </ul> 049 * @param video the video to be added. 050 * @param change the number of copies to add (or remove if negative). 051 * @throws IllegalArgumentException if <code>inventory</code> not created by a call to <code>newInventory</code>. 052 */ 053 static public Command newAddCmd(Inventory inventory, Video video, int change) { 054 if (!(inventory instanceof InventorySet)) 055 throw new IllegalArgumentException(); 056 return new CmdAdd((InventorySet) inventory, video, change); 057 } 058 059 /** 060 * Returns a command to check out a video. 061 * @param video the video to be checked out. 062 */ 063 static public Command newOutCmd(Inventory inventory, Video video) { 064 if (!(inventory instanceof InventorySet)) 065 throw new IllegalArgumentException(); 066 return new CmdOut((InventorySet) inventory, video); 067 } 068 069 /** 070 * Returns a command to check in a video. 071 * @param video the video to be checked in. 072 */ 073 static public Command newInCmd(Inventory inventory, Video video) { 074 if (!(inventory instanceof InventorySet)) 075 throw new IllegalArgumentException(); 076 return new CmdIn((InventorySet) inventory, video); 077 } 078 079 /** 080 * Returns a command to remove all records from the inventory. 081 */ 082 static public Command newClearCmd(Inventory inventory) { 083 if (!(inventory instanceof InventorySet)) 084 throw new IllegalArgumentException(); 085 return new CmdClear((InventorySet) inventory); 086 } 087}