Inventory.java 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import java.util.Hashtable;
  2. import java.util.Set;
  3. /**
  4. * Write a description of class Inventory here.
  5. *
  6. * @author (your name)
  7. * @version (a version number or a date)
  8. */
  9. public class Inventory
  10. {
  11. Hashtable<String, Product> productInventory;
  12. public Inventory() {
  13. productInventory = new Hashtable<String, Product>();
  14. }
  15. public void addItem(Product prod) {
  16. productInventory.put(prod.getID(), prod);
  17. }
  18. public void addItem(float price, int quantity, String id) {
  19. Product newItem = new Product(price, quantity, id);
  20. productInventory.put(id, newItem);
  21. }
  22. public Product getItem(String id) {
  23. return productInventory.get(id);
  24. }
  25. public int getOnHandQuantity(String id) {
  26. return productInventory.get(id).getOnHandQuantity() ;
  27. }
  28. public float getPrice(String id) {
  29. return productInventory.get(id).getPrice();
  30. }
  31. public void updatePrice(String id, float price) {
  32. productInventory.get(id).setPrice(price);
  33. }
  34. public void addUnits(String id, int quantity) {
  35. productInventory.get(id).addUnits(quantity);
  36. }
  37. public void removeUnits(String id, int quantity) {
  38. productInventory.get(id).removeUnits(quantity);
  39. }
  40. public double getInventoryValue() {
  41. double inventoryValue = 0;
  42. Set<String> productIDs = productInventory.keySet();
  43. for (String id : productIDs) {
  44. Product p = productInventory.get(id);
  45. int quantity = p.getOnHandQuantity();
  46. float price = p.getPrice();
  47. inventoryValue += quantity * price;
  48. }
  49. return inventoryValue;
  50. }
  51. }