⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inventory.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:
public class Inventory
    extends SortedList
    implements MenuUser
{
   protected final static String MENU[] =
       {
       "[A] Add Units", "[D] Delete Units", "[S] Show Inventory"};
   protected static final String TITLE = "Inventory Menu";

   public Inventory()
   {
      super(4, new ItemComparer());
      add(new InventoryItem("0001", "Basic Widget", 3.44, 15));
      add(new InventoryItem("3002", "Expensive Widget", 1203.0, 10));
      add(new InventoryItem("0011", "Sales Widget", 123.0, 10));
      add(new InventoryItem("2001", "Safety Widget", 33.99, 20));
   }

   public InventoryItem getPart()
   {
      System.out.println(this);
      int index = indexOf(new Item(Util.getString("Enter ID of item")));
      if (index >= 0)
      {
         return (InventoryItem) get(index);
      }
      else
      {
         return null;
      }
   }

   public void addUnits()
   {
      InventoryItem item = getPart();
      if (item != null)
      {
         item.add(Util.getInt("Number of units to add"));
      }
      else
      {
         System.out.println("Inventory item not found");
      }
   }

   public void removeUnits()
   {
      InventoryItem item = getPart();
      int units = Util.getInt("Number of units to remove");
      if ( (item != null) && (item.units >= units) && (units >= 0))
      {
         item.remove(units);
      }
      else
      {
         System.out.println("Item not found or not enough units available.");
      }
   }

   public void performAction(String command)
   {
      if (command.equalsIgnoreCase("S"))
      {
         System.out.println(this);
      }
      else if (command.equalsIgnoreCase("A"))
      {
         addUnits();
      }
      else if (command.equalsIgnoreCase("D"))
      {
         removeUnits();
      }
   }

   public static void main(String args[])
   {
      Menu menu = new Menu(TITLE + " Test", MENU, new Inventory());
   }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -