inventory.java
来自「此部分的源程序可使用Jdk1.3以上的任何版本编译和运行」· Java 代码 · 共 44 行
JAVA
44 行
package chapter1;
import javax.swing.JOptionPane;
public class Inventory extends SortedList {
//存货清单构造函数
public Inventory() {
super(4, new ItemComparer());
add(new InventoryItem("0001", "电池", 2.30, 15));
add(new InventoryItem("2002", "彩电", 1280.00, 10));
add(new InventoryItem("0015", "电扇", 128.00, 10));
add(new InventoryItem("3009", "台灯", 32.00, 20));
}
//根据用户输入的id,返回相应的存货条目,不存在的话,返回null
public InventoryItem getPart(String _id) {
int index = indexOf(new Item(_id));
if (index >= 0)
return (InventoryItem) get(index);
else
return null;
}
//在存货清单中,根据用户输入的id和用户输入的数量,增加相应货物的数量
public void addUnits(String _id, int _number) {
InventoryItem item = getPart(_id);
if (item != null)
item.add(_number);
else
JOptionPane.showMessageDialog(null, "没有找到和输入ID对应的项!", "警告",
JOptionPane.ERROR_MESSAGE);
}
//在存货清单中,根据用户输入的id和用户输入的数量,减少相应货物的数量
public void removeUnits(String _id, int _number) {
InventoryItem item = getPart(_id);
if ((item != null) && (item.units >= _number) && (_number >= 0))
item.remove(_number);
else
JOptionPane.showMessageDialog(null, "没有找到和输入ID对应的项或空间不足!", "警告",
JOptionPane.ERROR_MESSAGE);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?