/** * Sells a specified number of records Throws an exception if the record doesn't exist in the * store or if trying to sell more records than exist in the inventory * * @param record - the record to sell * @param quantity - the number of records to sell */ public void sellItem(AudioFormat record, int quantity) throws NullPointerException, IllegalArgumentException { // System.out.println("Selling: " + record.toString() + ", quantity: " + quantity); InventoryItem item = null; for (int i = 0; i < this.listOfItems.length; i++) { item = this.listOfItems[i]; // If it finds the record, break out of the loop if (record.equals(item.getRecord())) { break; } } // Handle the record not being in the inventory if (item == null) { throw new NullPointerException( "Error: there is no record of this type in stock " + "(record does not exist within the array)."); } // Handle selling more items than the store has if (item.getQuantity() - quantity < 0) { throw new IllegalArgumentException( "Error: you cannot sell more records than you have " + "(# of records sold makes quantity smaller than 0)."); } item.setQuantity(item.getQuantity() - quantity); }
/** * Adds a specified number of records to the inventory and updates the price * * @param record - the record to add * @param quantity - the number of records to add * @param price - the price to sell the record at */ public void addItem(AudioFormat record, int quantity, double price) { // System.out.println("Adding: " + record.toString() + ", quantity: " + quantity + ", price: " + // price); for (int i = 0; i < this.listOfItems.length; i++) { InventoryItem item = this.listOfItems[i]; // If the record is found, set the price and add the quantity and end the method if (record.equals(item.getRecord())) { item.setPrice(price); item.setQuantity(item.getQuantity() + quantity); return; } } // If it's an entirely new record, duplicates the old array and increases its size by 1 InventoryItem[] temp = new InventoryItem[this.listOfItems.length + 1]; for (int i = 0; i < this.listOfItems.length; i++) { temp[i] = this.listOfItems[i]; } this.listOfItems = temp; // Adds the new record to the end of the array this.listOfItems[this.listOfItems.length - 1] = new InventoryItem(record, quantity, price); }