public void Update(Item item) { int i, sta = 0; for (i = 0; i < list.size(); i++) if (list.get(i).getNumber() == item.getNumber()) { sta = 1; break; } if (sta == 0) System.out.println("仓库中没有此编号商品"); else { list.get(i).setStock(item.getStock()); System.out.println("已更新仓库"); } }
public void Add(Item item) { int sta = 0; for (int i = 0; i < list.size(); i++) if (list.get(i).getNumber() == item.getNumber()) { sta = 1; break; } if (sta == 1) // 如果已经存在,打印通知 System.out.println("仓库中已有该商品,请用Update更新"); else { // 如果没有,直接加入 list.add(item); System.out.println("已添加到仓库中"); } }
// 读写的方法 public void Save() { try { file.delete(); FileWriter writer = new FileWriter(file); for (Item i : list) writer.write( i.getNumber() + ";" + i.getCategory() + ";" + i.getName() + ";" + i.getModel() + ";" + i.getStock() + "\n"); writer.close(); } catch (Exception e) { e.printStackTrace(); } }
public void show() { Collections.sort( list, new Comparator<Item>() { public int compare(Item i1, Item i2) { if (i1.getNumber() > i2.getNumber()) return 1; else return -1; } }); for (Item i : list) System.out.println( i.getNumber() + ";" + i.getCategory() + ";" + i.getName() + ";" + i.getModel() + ";" + i.getStock()); }