小型超市,貨物管理(ArrayList使用)
阿新 • • 發佈:2019-01-07
貨物類:編號,名稱,價格,庫存
新增貨物:如果超市沒有該貨物,則新增
進貨:對已有貨物,增加庫存
修改:修改某個貨物的價格,庫存
下架:將某個貨物刪除
查詢:列印目前超市在售的貨物清單
測試類:
package com.zhiyou.shopping; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; import com.sweet.test.URLitem; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub List<GoodsManagement> goods = new ArrayList<>(); boolean isOK = true; while (isOK) { int i = screen(); if (i == 6) { isOK = false; System.out.println("謝謝使用!"); } function(i,goods); } } public static int screen() { System.out.println("-------------------歡迎進入貨物管理系統-----------------\n"); System.out.println(" 1.新增貨物:如果超市沒有該貨物,則新增"); System.out.println(" 2.進貨:對已有貨物,增加庫存"); System.out.println(" 3.修改:修改某個貨物的價格,庫存"); System.out.println(" 4.下架:將某個貨物刪除"); System.out.println(" 5.查詢:列印目前超市在售的貨物清單"); System.out.println(" 6.退出"); System.out.println("\n------------------------------------------------------"); System.out.println("請輸入你的操作:"); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); return i; } public static void function(int i,List<GoodsManagement> goods) { switch (i) { case 1: addGoods(goods); break; case 2: add(goods); break; case 3: reviseInfo(goods); break; case 4: deleteUrl(goods); break; case 5: queryInfo(goods); break; default: break; } } public static void addGoods(List<GoodsManagement> goods) { Scanner scanner = new Scanner(System.in); System.out.print("請輸入編號:"); String id = scanner.next(); System.out.print("請輸入名稱:"); String name = scanner.next(); System.out.print("請輸入價格:"); double price = scanner.nextDouble(); System.out.print("請輸入庫存:"); int num = scanner.nextInt(); Iterator<GoodsManagement> iterator = goods.iterator(); if (goods.size() != 0) { boolean isOK = false; while (iterator.hasNext()) { GoodsManagement goodsManagement = iterator.next(); if (goodsManagement.getName().equals(name)) { int num1 = goodsManagement.getNum() + num; goodsManagement.setNum(num1); isOK = true; } } if (isOK) { System.out.println("新增貨物成功!"); }else { goods.add(new GoodsManagement(id, name, price, num)); } }else { // 直接新增 goods.add(new GoodsManagement(id, name, price, num)); } } public static void add(List<GoodsManagement> goods) { Scanner scanner = new Scanner(System.in); System.out.print("請輸入要增加庫存的商品名稱:"); String name = scanner.next(); System.out.print("要增加的庫存量為:"); int num = scanner.nextInt(); boolean isOK = false; Iterator<GoodsManagement> iterator = goods.iterator(); while (iterator.hasNext()) { GoodsManagement goodsManagement = iterator.next(); if (goodsManagement.getName().equals(name)) { int num1 = goodsManagement.getNum() + num; goodsManagement.setNum(num1); isOK = true; } } if (isOK) { System.out.println("進貨成功!"); }else { System.out.println("沒有該商品,進貨失敗!"); } } public static void reviseInfo(List<GoodsManagement> goods) { if (goods.size() != 0) { Scanner scanner = new Scanner(System.in); System.out.print("請輸入要修改的商品名稱:"); String name = scanner.next(); System.out.print("請輸入修改的價格:"); double price = scanner.nextDouble(); System.out.print("請輸入修改的庫存量為:"); int num = scanner.nextInt(); boolean isOK = false; Iterator<GoodsManagement> iterator = goods.iterator(); while (iterator.hasNext()) { GoodsManagement goodsManagement = iterator.next(); if (goodsManagement.getName().equals(name)) { goodsManagement.setPrice(price); goodsManagement.setNum(num); isOK = true; } } if (isOK) { System.out.println("修改"+name+"商品的價格和庫存成功!"); }else { System.out.println("修改失敗!"); } }else { System.out.println("商品為空,無法修改!"); } } public static void deleteUrl(List<GoodsManagement> goods) { if (goods.size() != 0) { Scanner scanner = new Scanner(System.in); System.out.print("請輸入要下架的商品名稱:"); String name = scanner.next(); System.out.println("是否將該商品全部下架?還是隻下架該商品中的過期商品? 1 全部下架 2下架過期商品:"); int key = scanner.nextInt(); Iterator<GoodsManagement> iterator = goods.iterator(); if (key == 1) { while (iterator.hasNext()) { GoodsManagement goodsManagement = iterator.next(); if (goodsManagement.getName().equals(name)) { iterator.remove(); System.out.println("全部下架成功!"); } } }else { System.out.println("請輸入要下架的商品的數量:"); int num = scanner.nextInt(); while (iterator.hasNext()) { GoodsManagement goodsManagement = iterator.next(); if (goodsManagement.getName().equals(name)) { int num1 = goodsManagement.getNum() - num; goodsManagement.setNum(num1); System.out.println("下架部分商品成功!"); } } } }else { System.out.println("商品為空,無法下架!"); } } public static void queryInfo(List<GoodsManagement> goods) { if (goods.size() != 0) { boolean isOK = false; Iterator<GoodsManagement> iterator = goods.iterator(); while (iterator.hasNext()) { GoodsManagement goodsManagement = iterator.next(); System.out.println(goodsManagement); isOK = true; } if (!isOK) { System.out.println("查詢失敗!"); } }else { System.out.println("商品為空,無法展示!"); } } }
/** * */ package com.zhiyou.shopping; /** * @author Administrator * */ public class GoodsManagement { private String id; private String name; private double price; private int num; public GoodsManagement(String id, String name, double price, int num) { super(); this.id = id; this.name = name; this.price = price; this.num = num; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } @Override public String toString() { return "編號:" + id + " 名稱:" + name + " 價格:" + price + " 庫存:" + num + "\n"; } }