inventoryitem.java

来自「java源程序 对初学者有很大的帮助 从简单到复杂」· Java 代码 · 共 36 行

JAVA
36
字号
//********************************************************************
//  InventoryItem.java       Author: Lewis/Loftus
//
//  Represents an item in the inventory.
//********************************************************************

import java.text.DecimalFormat;

public class InventoryItem
{
   private String name;
   private int units;    // number of available units of this item
   private float price;  // price per unit of this item
   private DecimalFormat fmt;

   //-----------------------------------------------------------------
   //  Sets up this item with the specified information.
   //-----------------------------------------------------------------
   public InventoryItem (String itemName, int numUnits, float cost)
   {
      name = itemName;
      units = numUnits;
      price = cost;
      fmt = new DecimalFormat ("0.##");
   }

   //-----------------------------------------------------------------
   //  Returns information about this item as a string.
   //-----------------------------------------------------------------
   public String toString()
   {
      return name + ":\t" + units + " at " + price + " = " +
             fmt.format ((units * price));
   }
}

⌨️ 快捷键说明

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