/** * Interface ItemInt outlines the required methods to include with * implementations of this class. * @author David Thibodeau * @version 30-June-2002 */ public interface ItemInt { /** * getPrice method - Gets the current price of the item * * @param void * @return double - Current price of the item. */ double getPrice(); /** * setPrice method - Sets the current price of the item * * @param double - Sets the price of the item. * @return void */ void setPrice(double price); /** * getName method - Gets the name of the item * * @param void * @return String - Name of the item. */ String getName(); /** * getDescription method - Gets the description of the item * * @param void * @return String - Description of the item. */ String getDescription(); /** * getCode method - Gets the code of the item * * @param void * @return String - Code of the item. */ String getCode(); } /////////////////////////////////////////////////////////////// /** * Class Item holds the name, description, code and price information. * Item has methods to get and set prices, as well as getting the name, * description and code. Price is the only variable that can be changed * once the object has been created. * * @author David Thibodeau * @version 30-Jun-2002 */ public class Item implements ItemInt { private String name, description, code; // name, description, Universal Product Code private double price; // and price of Item /** * Constructor for objects of class Item */ public Item(String name, String description, String code, double price) { // Set class variables to their initial values. // Price is the only variable that can change after // the object has been created. this.name = name; this.description = description; this.code = code; this.price = price; } // end Item constructor /** * getPrice method - Gets the current price of the item * * @param void * @return double - Current price of the item. */ public double getPrice() { // Returns current price of item return price; } // end getPrice method /** * setPrice method - Sets the current price of the item * * @param double - Sets the price of the item. * @return void */ public void setPrice(double price) { this.price = price; } // end setPrice method /** * getName method - Gets the name of the item * * @param void * @return String - Name of the item. */ public String getName() { return name; // Returns name of item. } // end getName method /** * getDescription method - Gets the description of the item * * @param void * @return String - Description of the item. */ public String getDescription() { return description; // Returns description of item. } // end getDescription method /** * getCode method - Gets the code of the item * * @param void * @return String - Code of the item. */ public String getCode() { return code; // Returns code of item. } // end getCode method } // end class Item //////////////////////////////////////////////////////// /** * Class ItemStock uses the Item class to store basic information * about the Inventory object. It also gives methods to buy and * sell the Item. * @author David Thibodeau * @version 30-June-2002 */ public class ItemStock { // inventoryItem holds the Item object and // numberInStock holds quantity available private Item inventoryItem; private int numberInStock; /** * createItem method - Creates the item to hold in inventory * * @param String name - Name of the inventory item * @param String description - Description of the inventory item * @param String code - Code of the inventory item * @param double price - Price of the inventory item * @param int numberInStock - Number of units in stock * @return void */ public void createItem(String name, String description, String code, double price, int numberInStock) { inventoryItem = new Item(name, description, code, price); this.numberInStock = numberInStock; } // end createItem method /** * buyInventory method - Adds amount bought to inventory * * @param int numPurchased - The number of items purchased * @return void */ public void buyInventory(int numPurchased) { numberInStock += numPurchased; System.out.println(numPurchased + " units of " + inventoryItem.getName() + " purchased."); } // end buyInventory method /** * sellInventory method - Subtracts amount sold from inventory * * @param int numSold - The number of items sold * @return void */ public void sellInventory(int numSold) { if (numberInStock < numSold) { System.out.println("You only have " + numberInStock + " units in stock." + "\n You cannot sell " + numSold + " units."); } // end if else { numberInStock -= numSold; System.out.println(numSold + " units of " + inventoryItem.getName() + " sold."); } // end else } // end sellInventory method /** * report method - Gives name, description, code, price and * number in stock * @param void * @return void */ public void report() { System.out.println("Name: " + inventoryItem.getName() + "\tDescription: " + inventoryItem.getDescription() + "\tCode: " + inventoryItem.getCode() ); System.out.println("Retail Price: " + inventoryItem.getPrice() + "\tNumber In Stock: " + numberInStock); } // end report method /** * getPrice method - Gets the current price of the item * * @param void * @return double - Current price of the item */ public double getPrice() { // Returns current price of item return inventoryItem.getPrice(); } // end getPrice method /** * setPrice method - Sets the current price of the item * * @param double price - Sets the price of the item * @return void */ public void setPrice(double price) { inventoryItem.setPrice(price); } // end setPrice method /** * getName method - Gets the name of the item * * @param void * @return String - Name of the item */ public String getName() { return inventoryItem.getName(); // Returns name of item. } // end getName method /** * getDescription method - Gets the description of the item * * @param void * @return String - Description of the item */ public String getDescription() { return inventoryItem.getDescription(); // Returns description of item. } // end getDescription method /** * getCode method - Gets the code of the item * * @param void * @return String - Code of the item */ public String getCode() { return inventoryItem.getCode(); // Returns code of item. } // end getCode method } // end ItemStock class ///////////////////////////////////////////////////////////// /** * Class StoreInventory holds a collection of inventory objects in an array. * There is a limit of 100 inventory objects, but there can be more * than 1 unit of that item in stock. * @author David Thibodeau * @version 30-June-2002 */ public class StoreInventory { // instance variables - replace the example below with your own private String storeName, storeCity, storeState; private final int SIZE = 100; private ItemStock[] storeInventory; private int nElems = 0; // counter which holds number of active elements in the array /** * Constructor for objects of class StoreInventory */ public StoreInventory(String storeName, String storeCity, String storeState) { // initialise instance variables this.storeName = storeName; this.storeCity = storeCity; this.storeState = storeState; storeInventory = new ItemStock[SIZE]; } // end Store constructor /** * addInventoryItem method - Allows you to add an item to inventory * * @param String name - Name of the inventory item * @param String description - Description of the inventory item * @param String code - Code of the inventory item * @param double price - Price of the inventory item * @param int numberInStock - Number of units in stock * @return void */ public void addInventoryItem(String name, String description, String code, double price, int numberInStock) { if (nElems == 100) { System.out.println("Cannot add any more items to inventory. Maximum number" + " of 100 inventory items has been reached."); } else if (nElems == 0) { storeInventory[nElems] = new ItemStock(); storeInventory[nElems].createItem(name, description, code, price, numberInStock); nElems++; } // end else if else { for (int i = 0; i < nElems; i++) { if ( code == storeInventory[i].getCode() ) { System.out.println("Item already exists in inventory."); return; } } // end for storeInventory[nElems] = new ItemStock(); storeInventory[nElems].createItem(name, description, code, price, numberInStock); nElems++; // create the inventory item and increment the nElems counter } // end else } // end addInventoryItem method /** * storeReport method - Sends an inventory report out to the console. * * @param void * @return void */ public void storeReport() { System.out.println(" Store Name: " + storeName + "\tStore City: " + storeCity + "\tStore State: " + storeState); for (int i = 0; i < nElems; i++) { storeInventory[i].report(); } // end for System.out.println("\n\t- - - END OF REPORT - - -"); } // end storeReport method /** * inventoryItemReport method - Sends a report on one specific * inventory item out to the console. * @param int - The array index of the specific item to report on. * @return void */ public void inventoryItemReport(int inventoryIndex) { if ( checkIndex(inventoryIndex) ) { storeInventory[inventoryIndex].report(); } } // inventoryItemReport(int) method /** * inventoryItemReport method - Sends a report on one specific * inventory item out to the console. * @param String inventoryCode - The code of the item to report on. * @return void */ public void inventoryItemReport(String inventoryCode) { for (int i = 0; i < nElems; i++) { if ( inventoryCode == storeInventory[i].getCode() ) { storeInventory[i].report(); return; } // end if } // end for // Supplied inventoryCode not found System.out.println("Inventory Code " + inventoryCode + " not found."); } // end inventoryItemReport(String) method /** * checkIndex method - used within the class to check inventoryIndex * values passed to certain public methods and make sure the * inventoryIndex is valid. * @param int inventoryIndex - value of inventoryIndex to check * @return boolean - true if inventoryIndex is OK. */ private boolean checkIndex(int inventoryIndex) { if (inventoryIndex > (nElems - 1) ) { System.out.println("Inventory index of " + inventoryIndex + " is beyond the limit of " + (nElems - 1) + "."); return false; // lets calling method know that inventoryIndex is invalid. } else { return true; } // lets calling method know that inventoryIndex is OK. } // end checkIndex method /** * checkCode method - used within the class to check inventoryCode * values passed to certain public methods and make sure the * inventoryCode is valid. * @param String inventoryCode - inventory code to check * @return int - array index is code is valid, or -1 if code not found */ private int checkCode(String inventoryCode) { for (int i = 0; i < nElems; i++) { if ( inventoryCode == storeInventory[i].getCode() ) { return i; } // end if } // end for // Supplied inventoryCode not found System.out.println("Inventory Code " + inventoryCode + " not found."); return -1; // Lets caller know inventory code wasn't found. } // end checkIndex method /** * buyInventory method - Adds amount bought to inventory * * @param int numPurchased - The number of items purchased * @param int inventoryIndex - Index number of item purchased * @return void */ public void buyInventory(int numPurchased, int inventoryIndex) { if (checkIndex(inventoryIndex) ) { // add new stock to inventory storeInventory[inventoryIndex].buyInventory(numPurchased); } } // end buyInventory(int) method /** * buyInventory method - Adds amount bought to inventory * * @param int numPurchased - Number of items purchased * @param String inventoryCode - Code of item purchased * @return void */ public void buyInventory(int numPurchased, String inventoryCode) { int code = checkCode(inventoryCode); if (code > -1 ) { storeInventory[code].buyInventory(numPurchased); } } // end buyInventory(String) method /** * sellInventory method - Subtracts amount sold from inventory * * @param int numSold - Number of items sold * @param int inventoryIndex - Index number of item purchased * @return void */ public void sellInventory(int numSold, int inventoryIndex) { if ( checkIndex(inventoryIndex) ) { storeInventory[inventoryIndex].sellInventory(numSold); } } // end sellInventory(inventoryIndex) method /** * sellInventory method - Subtracts amount sold from inventory * * @param int numSold - Number of items sold * @param String inventoryCode - Code of inventory item * @return void */ public void sellInventory(int numSold, String inventoryCode) { int code = checkCode(inventoryCode); if (code > -1 ) { storeInventory[code].sellInventory(numSold); } } // end sellInventory(inventoryCode) method /** * getPrice method - Gets the current price of the item * * @param int inventoryIndex - Index number of item * @return double - Current price of the item */ public double getPrice(int inventoryIndex) { if ( checkIndex(inventoryIndex) ) { return storeInventory[inventoryIndex].getPrice(); } else { return -1; } } // end getPrice method /** * getPrice method - Gets the current price of the item * * @param String inventoryCode - Code of inventory item * @return double - Current price of the item */ public double getPrice(String inventoryCode) { int code = checkCode(inventoryCode); if (code > -1 ) { return storeInventory[code].getPrice(); } else { return -1; } } // end getPrice method /** * setPrice method - Sets the current price of the item * * @param double price - Sets the price of the item * @param int inventoryIndex - Index number of item * @return void */ public void setPrice(double price, int inventoryIndex) { if( checkIndex(inventoryIndex) ) { storeInventory[inventoryIndex].setPrice(price); } } // end setPrice(inventoryIndex) method /** * setPrice method - Sets the current price of the item * * @param double price - Sets the price of the item * @param String inventoryCode - Code of inventory item * @return void */ public void setPrice(double price, String inventoryCode) { int code = checkCode(inventoryCode); if (code > -1 ) { storeInventory[code].setPrice(price); } } // end setPrice(inventoryCode) method /** * getName method - Gets the name of the item * * @param int inventoryIndex - Index number of item * @return String - Name of the item */ public String getName(int inventoryIndex) { if( checkIndex(inventoryIndex) ) { return storeInventory[inventoryIndex].getName(); } else { return ""; } } // end getName(inventoryIndex) method /** * getName method - Gets the name of the item * * @param String inventoryCode - Code of inventory item * @return String - Name of the item */ public String getName(String inventoryCode) { int code = checkCode(inventoryCode); if (code > -1 ) { return storeInventory[code].getName(); } else { return ""; } } // end getName(inventoryCode) method /** * getDescription method - Gets the description of the item * * @param int inventoryIndex - Index number of item * @return String - Description of the item */ public String getDescription(int inventoryIndex) { if( checkIndex(inventoryIndex) ) { return storeInventory[inventoryIndex].getDescription(); } else { return ""; } } // end getDescription(inventoryIndex) method /** * getDescription method - Gets the description of the item * * @param String inventoryCode - Code of inventory item * @return String - Description of the item */ public String getDescription(String inventoryCode) { int code = checkCode(inventoryCode); if (code > -1 ) { return storeInventory[code].getDescription(); } else { return ""; } } // end getDescription(inventoryCode) method /** * getCode method - Gets the code of the item * * @param int inventoryIndex - Index number of item * @return String - Code of the item */ public String getCode(int inventoryIndex) { if( checkIndex(inventoryIndex) ) { return storeInventory[inventoryIndex].getCode(); } else { return ""; } } // end getCode(inventoryIndex) method } // end Store class