invoice.java

来自「Java程序设计技巧与开发实例附书源代码。」· Java 代码 · 共 82 行

JAVA
82
字号

public class Invoice
    extends SortedList
    implements MenuUser
{
   protected static final String MENU[] =
       {
       "[A] Add Item", "[D] Delete Item", "[S] Show Invoice"};
   protected static final String TITLE = "Invoice Menu";
   protected String name;
   private Inventory inventory;

   public Invoice(Inventory _inventory, String _name)
   {
      super(new ItemComparer());
      inventory = _inventory;
      name = _name;
   }

   public void addItem()
   {
      InventoryItem item = inventory.getPart();
      int units = Util.getInt("Units");
      if ( (item != null) && (item.units >= units) && (units >= 0) && !isFull())
      {
         item.remove(units);
         add(new InvoiceItem(item, units));
      }
      else
      {
         System.out.println("List full, item not found or invalid units");
      }
   }

   public void delItem()
   {
      System.out.println(this);
      int index = indexOf(new Item(Util.getString("Enter item ID:")));
      if (index >= 0)
      {
         delete(index);
      }
   }

   public double getTotal()
   {
      double total = 0.0;
      for (int i = 0; i < getSize(); i++)
      {
         total += ( (InvoiceItem) get(i)).total;
      }
      return total;
   }

   public String toString()
   {
      return "\n" + name + ": " + Util.dollar(getTotal()) + super.toString();
   }

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

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

}

⌨️ 快捷键说明

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