1. 程式人生 > >JAVA小白在多方幫助下的第二個系統—自助售賣系統

JAVA小白在多方幫助下的第二個系統—自助售賣系統

我們還是建立兩個類

寫一些屬性和 方法並建立set get方法
package com.lenovo.www.entity;

public class Sales {
    private String goodsid;
    private String goodsname;
    private double goodspay;
    private  int stock;
public Sales() {}


public Sales(String goodsid, String goodsname, double goodspay, int stock) {
    super
(); this.goodsid = goodsid; this.goodsname = goodsname; this.goodspay = goodspay; this.stock = stock; } public String getGoodsid() { return goodsid; } public void setGoodsid(String goodsid) { this.goodsid = goodsid; } public String getGoodsname() { return goodsname; } public
void setGoodsname(String goodsname) { this.goodsname = goodsname; } public double getGoodspay() { return goodspay; } public void setGoodspay(double goodspay) { this.goodspay = goodspay; } public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } }

然後

package com.lenovo.www.entity;

import java.util.ArrayList;
import java.util.Scanner;

public class Sell {
    static int abc = 0;
    public static ArrayList<Sales> shopList = new ArrayList<>();
    static int n;



    public static void main(String[] args) {
        init();
        do {
            shopList();

        } while (n != 0);
        System.out.println("程式退出");
    }

    private static void init() {
        // TODO Auto-generated method stub
        Sales s1 = new Sales("B001", "百事可樂", 3.5, 10);
        Sales s2 = new Sales("B002", "可口可樂", 3.5, 10);
        Sales s3 = new Sales("B003", "雪碧", 3.5, 10);
        Sales s4 = new Sales("B004", "泡椒鳳爪", 9.5, 10);
        shopList.add(s1);
        shopList.add(s2);
        shopList.add(s3);
        shopList.add(s4);
    }

    private static void shopList() {
        System.out.println("請輸入您要選擇的功能:");
        System.out.println("1:檢視全部");
        System.out.println("2:新增商品");
        System.out.println("3:購買商品");
        System.out.println("4:商品補貨");
        System.out.println("0:退出程式");
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        System.out.println("您選擇的功能序號是:" + n);
        switch (n) {
        case 1:
            listAlldrink();
            break;
        case 2:
            addshop();
            break;
        case 3:
            buyshop();
            break;

        case 4:
            tianjia();

        }

    }

    private static void buyshop() {
        int c = 0;
        int d = 0;
        System.out.println("請輸入你要購買商品的編號:");
        Scanner scan = new Scanner(System.in);
        String id = scan.next();
            for(int i  = 0 ; i<shopList.size();i++) {
                if(shopList.get(i).getGoodsid().equals(id)) {
                    System.out.println("編號:"+shopList.get(i).getGoodsid()+"名稱:"+shopList.get(i).getGoodsname()+"價格"+shopList.get(i).getGoodspay()+"庫存"+shopList.get(i).getStock());
                    c = 1;
                    d = i;
                }   
            }
            if(c==1) {
                System.out.println("請輸入購買的數量");
                int number = scan.nextInt();
                if(shopList.get(d).getStock()>=number) {
                    shopList.get(d).setStock(shopList.get(d).getStock()-number);
                    System.out.println("購買成功,買了:"+number+"瓶");
                }else {
                    System.out.println("對不起,庫存不足啦!");
                }

            }
        }


    private static void addshop() {
        Scanner sc = new Scanner(System.in);
        System.out.println("請您輸入新增的商品編號:");
        String goodsid = sc.nextLine();
        System.out.println("請您輸入新增的商品名稱:");
        String goodsname = sc.nextLine();
        System.out.println("請您選擇新增的商品價格:");
        double goodspay = sc.nextDouble();
        System.out.println("請您選擇新增商品的庫存:");
        int goodsstock = sc.nextInt();
        Sales sales = new Sales(goodsid, goodsname, goodspay, goodsstock);
        shopList.add(sales);
    }

    private static void listAlldrink() {

        for (Sales sales : shopList) {
            System.out.println("商品編號 " + sales.getGoodsid() + " 商品名稱: " + sales.getGoodsname() + "商品售價:"
                    + sales.getGoodspay() + " 商品庫存:" + sales.getStock());

        }
    }
    private static void tianjia() {
        int a = 0;
        int b = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入編號");
        String id = scan.next();
        for(int i = 0 ; i<shopList.size();i++) {
            if(shopList.get(i).getGoodsid().equals(id)) {
                System.out.println("編號:"+shopList.get(i).getGoodsid()+"名稱:"+shopList.get(i).getGoodsname()+"價格"+shopList.get(i).getGoodspay()+"庫存"+shopList.get(i).getStock());
                a=1;
                b=i;
            }
        }
        if(a==1) {
            System.out.println("請輸入新增的數量");
            int number = scan.nextInt();
            shopList.get(b).setStock(shopList.get(b).getStock()+number);
        }
    }

}

寫此程式碼多坎坷,感謝大神幫助