public class InventoryDriver { public static void main(String[] args) { InventoryItem item1 = new InventoryItem("d-145-blk", "Black Case-it Large Capacity 3-Inch Zipper Binder", 5.50, 65); InventoryItem item2 = new InventoryItem("d-145-blk", "Black Case-it Large Capacity 3-Inch Zipper Binder", 19.97, 123, 50, 0); System.out.println(item1); item1.equals(); System.out.println(); System.out.println(item2); item2.equals(); System.out.println("\n****Display Inventory for item2 after 3 items are purchased"); item2.receiveOrderedItems(3); System.out.println("Quantity Available: " + item2.getQuantityOnHand()); System.out.println("Number on order: " + item2.getNumberOnOrder()); item2.getCurrentInventoryValue(); System.out.println("Cost: " + item2.getCost()); System.out.println("\n****Display Inventory for item1 after item number is changed and 40 items are purchased"); item1.receiveOrderedItems(40); item1.setItemNumber("k-456-kdn"); System.out.println("Item number: " + item1.getItemNumber()); item1.getItemNumber(); System.out.println("Quantity Available: " + item1.getQuantityOnHand()); System.out.println("Number on order: " + item1.getNumberOnOrder()); item1.getCurrentInventoryValue(); System.out.println("Cost: " + item1.getCost()); System.out.println("\n****Display inventory for item2 after description is changed and 2 more items are purchased"); item2.setDescription("Purple Purse"); System.out.println("Description: " + item2.getDescription()); item2.receiveOrderedItems(2); System.out.println("Quantity Available: " + item2.getQuantityOnHand()); System.out.println("Number on order: " + item2.getNumberOnOrder()); item2.getCurrentInventoryValue(); System.out.println("Cost: " + item2.getCost());
public class InventoryItem { private String itemNumber; private int quantityOnHand; private int reorderLevel; private double cost; private int numberOnOrder; private String description; // Constructor that sets quantityOnHand and numberOnOrder to zero, and takes in data for the rest. public InventoryItem(String itemNumber, String description, double cost, int reorderLevel) { this.itemNumber = itemNumber.toUpperCase(); this.description = description; this.cost = cost; this.reorderLevel = reorderLevel; } // Constructor that takes and sets all the data public InventoryItem(String itemNumber, String description, double cost, int quantityOnHand, int reorderLevel, int numberOnOrder) { this.itemNumber = itemNumber.toUpperCase(); this.description = description; this.cost = cost; this.quantityOnHand = quantityOnHand; this.reorderLevel = reorderLevel; this.numberOnOrder = numberOnOrder; } // Special setter for itemNumber, data is always stored in upper-case. public void setItemNumber(String itemNumber) { this.itemNumber = itemNumber.toUpperCase(); } // Getter for itemNumber. public String getItemNumber() { return itemNumber; } // Setter for reorderLevel. public void setReorderLevel(int reorderLevel) { this.reorderLevel = reorderLevel; } // Getter for reorderLevel. public int getReorderLevel() { return reorderLevel; } // Setter for description. public void setDescription(String description) { this.description = description; } // Getter for description. public String getDescription() { return description; } // Getter for quantityOnHand. public int getQuantityOnHand() { return quantityOnHand; } // Getter for cost. public double getCost() { return cost; } // Getter for numberOnOrder. public int getNumberOnOrder() { return numberOnOrder; } // Method that takes in numOrdered and updates that number of items that are on order. public void orderItems(int numOrdered) { numOrdered = numberOnOrder + numOrdered; } // Method that takes in numberReceived and updates that number of items on hand and number that is still on order. public void receiveOrderedItems(int numberReceived) { this.quantityOnHand = quantityOnHand - numberReceived; this.numberOnOrder = numberOnOrder + numberReceived; } // Method that takes in numberSold and updates that the number of items on hand. public void sellItems(int numberSold) { this.quantityOnHand = quantityOnHand - numberSold; } // Method that returns the total cost of the item that is on hand. public void getCurrentInventoryValue() { this.cost = cost * numberOnOrder; } // Equal method comparing two instances public void equals() { if (itemNumber.equalsIgnoreCase(itemNumber)) System.out.println("item number's are the same."); else System.out.println("item number's are not the same."); if (description.equalsIgnoreCase(description)) System.out.println("item description's are the same."); else System.out.println("item description's are not the same."); if (itemNumber.equalsIgnoreCase(description)) System.out.println("item description and description are not the same."); } // toString method that displays the data about the instance of InventoryItem class. public String toString() { return "Item Number: " + itemNumber + "\nDescription: " + description + "\nCost: " + cost + "\nQuantity Available: " + quantityOnHand + "\nRe-order level: " + reorderLevel + "\nNumber on order: " + numberOnOrder; } }
When I run this program, my cost variable displays the correct cost the first time it is ran, but every time after that I get a price that is not correct. Why isn't the variable multiplying like I want it to? Help, please.
Thanks.