溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java如何實(shí)現(xiàn)商品的查找、添加、出庫、入庫操作

發(fā)布時(shí)間:2021-04-15 10:17:53 來源:億速云 閱讀:899 作者:小新 欄目:編程語言

小編給大家分享一下Java如何實(shí)現(xiàn)商品的查找、添加、出庫、入庫操作,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體如下:

package com.jredu.oopch08;
public class Goods1 {
    private int id;
    private String name;
    private double price;
    private String uom;
    private int balance;
    public Goods1(int id, String name, double price, String uom, int balance) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
        this.uom = uom;
        this.balance = balance;
    }
    public int getId() {
        return id;
    }
    public void setId(int 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 String getUom() {
        return uom;
    }
    public void setUom(String uom) {
        this.uom = uom;
    }
    public int getBalance() {
        return balance;
    }
    public void setBalance(int balance) {
        this.balance = balance;
    }
}
package com.jredu.oopch08;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class TestGoods1 {
    private static Map map = new HashMap<>();
    private static Scanner in = new Scanner(System.in);
    public static void get() {
        Goods1 goods1 = new Goods1(1001, "脈動(dòng)水蜜桃 ", 7.0, "1.5l", 50);
        Goods1 goods2 = new Goods1(1002, "桃李熟切片 ", 6.5, "400g", 10);
        Goods1 goods3 = new Goods1(1003, "吉白芝麻油 ", 9.5, "125ml", 20);
        Goods1 goods4 = new Goods1(1004, "雀巢奶咖啡", 1.5, "13g", 200);
        Goods1 goods5 = new Goods1(1005, "白玉黃豆芽 ", 2.4, "350g", 50);
        map.put(goods1.getId(), goods1);
        map.put(goods2.getId(), goods2);
        map.put(goods3.getId(), goods3);
        map.put(goods4.getId(), goods4);
        map.put(goods5.getId(), goods5);
    }
    public static boolean check(int id) {
        // 檢測(cè)匹配id
        if (!map.containsKey(id)) {
            // 沒有匹配id
            return false;
        } else {
            // 有匹配的id
            return true;
        }
    }
    public static void add() {// 新增商品
        System.out.println(">>新增商品");
        System.out.print("請(qǐng)輸入商品編號(hào):");
        int id = in.nextInt();
        if (new TestGoods1().check(id)) {
            // 有匹配的id
            System.out.println("對(duì)不起,此商品已存在!");
        } else {
            System.out.print("請(qǐng)輸入商品名稱:");
            String name = in.next();
            System.out.print("請(qǐng)輸入商品單價(jià):");
            double price = in.nextDouble();
            System.out.print("請(qǐng)輸入商品單位:");
            String uom = in.next();
            System.out.print("請(qǐng)輸入商品庫存:");
            int balance = in.nextInt();
            Goods1 goods6 = new Goods1(id, name, price, uom, balance);
            map.put(goods6.getId(), goods6);
            System.out.println("新增成功!");
        }
    }
    public static void show() {// 顯示商品信息
        System.out.println("商品編號(hào)\t商品名稱\t\t商品單價(jià)\t單位\t數(shù)量");
        Set<Map.Entry<Integer, Goods1>> entrySet = map.entrySet();
        Iterator<Map.Entry<Integer, Goods1>> iter = entrySet.iterator();
        while (iter.hasNext()) {
            Map.Entry<Integer, Goods1> entry = iter.next();
            System.out.print(entry.getKey() + "\t");
            System.out.println(entry.getValue().getName() + "\t\t" + entry.getValue().getPrice() + "\t"
                    + entry.getValue().getUom() + "\t" + entry.getValue().getBalance());
        }
    }
    public static void inStore() {// 入庫
        System.out.println(">>商品入庫");
        System.out.print("請(qǐng)輸入商品編號(hào):");
      int id = in.nextInt();
      for (int i = 0; i < map.size(); i++) {
          if (new TestGoods1().check(id)) {
                //有匹配的id
                System.out.print("請(qǐng)輸入入庫數(shù)量:");
                int count = in.nextInt();
                    int c = ((Goods1) map.get(id)).getBalance()+count;
                    ((Goods1) map.get(id)).setBalance(c);
                    break;
            }else{
                //沒有匹配的id
                System.out.println("對(duì)不起,此商品不存在!");
                break;
            }
        }
    }
    public void outStore() {// 出庫
        System.out.println(">>商品出庫");
        System.out.print("請(qǐng)輸入商品編號(hào):");
        int id = in.nextInt();
        for (int i = 0; i < map.size(); i++) {
          if (new TestGoods1().check(id)) {
                //有匹配的id
                System.out.print("請(qǐng)輸入出庫數(shù)量:");
                int count = in.nextInt();
                if(count>((Goods1)map.get(id)).getBalance()){
                    System.out.println("庫存不足,出庫失?。?quot;);
                }else{
                    int c = ((Goods1) map.get(id)).getBalance()-count;
                    ((Goods1) map.get(id)).setBalance(c);
                    break;
                }
            }else{
                //沒有匹配的id
                System.out.println("對(duì)不起,此商品不存在!");
                break;
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestGoods1 t = new TestGoods1();
        t.get();
        //t.add();
    //    t.show();
    //    t.inStore();
        t.show();
        t.outStore();
        t.show();
    }
}

以上是“Java如何實(shí)現(xiàn)商品的查找、添加、出庫、入庫操作”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI