商品管理1

这是一个简单的Java商品管理系统,包括展示商品、上架商品、下架商品和调整价格等功能。用户通过控制台输入操作指令,系统根据输入进行相应操作。商品信息存储在静态数组中,下架商品时将商品对象置为null,更新商品价格时会创建新的Goods对象。系统还提供了异常处理,确保输入的合法性和有效性。
摘要由CSDN通过智能技术生成
package com.hp.Demo3;

import com.sun.javafx.scene.input.InputEventUtils;

import java.util.Scanner;

public class Counter {
    private static Goods[]goodses = new Goods[10];
    int num=0;
    public Counter(){
        this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
        this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
        num=2;//相当于两个商品
    }

    public void mian() {
        while (true) {
            System.out.println("------------------------------");
            System.out.println("-----1.展示商品   2.上架商品-----");
            System.out.println("-----3.下架商品   4.调整价格-----");
            System.out.println("-----0.退出    ----------------");
            System.out.println("输入功能编号");
            //int key = new Scanner(System.in).nextInt();
            int key = inputUtils.getNUm();
            switch (key) {
                case 1:
                    show();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    delect();
                    break;
                case 4:
                    update();
                    break;
                case 0:
                    System.exit(0);//退出JVM


            }

        }
    }
    public void show(){
        if (num == 00) {
            System.out.println("不输出null商品");
            return;
        }
        for (int i = 0; i < goodses.length; i++) {
            if (goodses[i] != null) {
                System.out.println(goodses[i]);
            }
        }
    }
    private void delect() {
        System.out.println("-->请输入要下架的商品编号");
        int goodsId = inputUtils.getGoodsId();
        for (int i = 0; i <this.goodses.length ; i++) {
            if (this.goodses !=null && this.goodses[i].getId()==goodsId) {
                //找到以后,删除
                this.goodses[i] = null;
                //商品数量减一
                this.num--;
                System.out.println("-->下架成功!!当前商品数量"+this.num);
                return;//跳出方法(只有找到商品才会执行)
            }
        }
        //如果没有找到商品,就会执行到这里
        System.out.println("-->警告,未找到商品");
    }

    private void update() {
        System.out.println("请输入要修改商品的id");
        int goodsId = inputUtils.getGoodsId();
        for (int i = 0; i <this.goodses.length ; i++) {
            if (this.goodses != null && this.goodses[i].getId() == goodsId) {
                System.out.println("商品的初始价格为"+this.goodses[i].getPrice());


                System.out.println("请输入价格");
                int g = inputUtils.getPrice();
                this.goodses[i].setPrice(g);
                Goods goods=new Goods(this.goodses[i].getId(),this.goodses[i].getGoodName());

                show();
                return;
            }
        }
    }

    private void add() {
        System.out.println("-->请输入商品id");
        int goodsId = inputUtils.getGoodsId();
        //丰富: id不能重复
        for (int i = 0; i < this.goodses.length; i++) {
            if (this.goodses != null && this.goodses[i].getId() == goodsId) {
                System.out.println("-->警告,当前编号已存在:请重新输入");
                add();
                return;//由于重复了,后续不执行
            }
        }
        //程序能走到这里,说明Id没有重复
        System.out.println("-->提示:商品id可以使用");
        System.out.println("-->请输入商品名称:");
        String name = new Scanner(System.in).next();
        System.out.println("-->请输入商品价格:");
        String price = new Scanner(System.in).next();
        System.out.println("-->请输入商品描述:");
        String desc = new Scanner(System.in).next();
        //把输入的信息装到Goods对像
        Goods newGoods=new Goods(goodsId,name,price,desc);

        for (int i = 0; i < this.goodses.length; i++) {
            if (this.goodses[i]==null) {
                this.goodses[i] = newGoods;
                this.num++;
                System.out.println("");
                return;
            }
        }
    }
}

package com.hp.Demo3;

import com.sun.javafx.scene.input.InputEventUtils;

import java.util.Scanner;

public class Counter {
    private static Goods[]goodses = new Goods[10];
    int num=0;
    public Counter(){
        this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
        this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
        num=2;//相当于两个商品
    }

    public void mian() {
        while (true) {
            System.out.println("------------------------------");
            System.out.println("-----1.展示商品   2.上架商品-----");
            System.out.println("-----3.下架商品   4.调整价格-----");
            System.out.println("-----0.退出    ----------------");
            System.out.println("输入功能编号");
            //int key = new Scanner(System.in).nextInt();
            int key = inputUtils.getNUm();
            switch (key) {
                case 1:
                    show();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    delect();
                    break;
                case 4:
                    update();
                    break;
                case 0:
                    System.exit(0);//退出JVM


            }

        }
    }
    public void show(){
        if (num == 00) {
            System.out.println("不输出null商品");
            return;
        }
        for (int i = 0; i < goodses.length; i++) {
            if (goodses[i] != null) {
                System.out.println(goodses[i]);
            }
        }
    }
    private void delect() {
        System.out.println("-->请输入要下架的商品编号");
        int goodsId = inputUtils.getGoodsId();
        for (int i = 0; i <this.goodses.length ; i++) {
            if (this.goodses !=null && this.goodses[i].getId()==goodsId) {
                //找到以后,删除
                this.goodses[i] = null;
                //商品数量减一
                this.num--;
                System.out.println("-->下架成功!!当前商品数量"+this.num);
                return;//跳出方法(只有找到商品才会执行)
            }
        }
        //如果没有找到商品,就会执行到这里
        System.out.println("-->警告,未找到商品");
    }

    private void update() {
        System.out.println("请输入要修改商品的id");
        int goodsId = inputUtils.getGoodsId();
        for (int i = 0; i <this.goodses.length ; i++) {
            if (this.goodses != null && this.goodses[i].getId() == goodsId) {
                System.out.println("商品的初始价格为"+this.goodses[i].getPrice());


                System.out.println("请输入价格");
                int g = inputUtils.getPrice();
                this.goodses[i].setPrice(g);
                Goods goods=new Goods(this.goodses[i].getId(),this.goodses[i].getGoodName());

                show();
                return;
            }
        }
    }

    private void add() {
        System.out.println("-->请输入商品id");
        int goodsId = inputUtils.getGoodsId();
        //丰富: id不能重复
        for (int i = 0; i < this.goodses.length; i++) {
            if (this.goodses != null && this.goodses[i].getId() == goodsId) {
                System.out.println("-->警告,当前编号已存在:请重新输入");
                add();
                return;//由于重复了,后续不执行
            }
        }
        //程序能走到这里,说明Id没有重复
        System.out.println("-->提示:商品id可以使用");
        System.out.println("-->请输入商品名称:");
        String name = new Scanner(System.in).next();
        System.out.println("-->请输入商品价格:");
        String price = new Scanner(System.in).next();
        System.out.println("-->请输入商品描述:");
        String desc = new Scanner(System.in).next();
        //把输入的信息装到Goods对像
        Goods newGoods=new Goods(goodsId,name,price,desc);

        for (int i = 0; i < this.goodses.length; i++) {
            if (this.goodses[i]==null) {
                this.goodses[i] = newGoods;
                this.num++;
                System.out.println("");
                return;
            }
        }
    }
}

package com.hp.Demo3;

public class Goods {
    private int id;
    private String goodName;
    private int price;
    private String desc;

    public Goods(int goodsId, String name, String price, String desc) {
    }

    public Goods(int id, String goodName) {
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", goodName='" + goodName + '\'' +
                ", price=" + price +
                ", desc='" + desc + '\'' +
                '}';
    }

    public void setId(int id) {

        this.id = id;
    }

    public void setGoodName(String goodName) {
        this.goodName = goodName;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public int getId() {
        return id;
    }

    public String getGoodName() {
        return goodName;
    }

    public int getPrice() {
        return price;
    }

    public String getDesc() {
        return desc;
    }

    public Goods(int id, String goodName, int price, String desc) {
        this.id = id;
        this.goodName = goodName;
        this.price = price;
        this.desc = desc;
    }


}

package com.hp.Demo3;

import java.util.Scanner;

/**
 * 提供一些输入方法
 */
public class inputUtils {
    /**
     * 获取我们的功能序号
     *
     * @return
     */
    public static int getNUm() {
        int n = 0;
        try {
            n = new Scanner(System.in).nextInt();
        } catch (Exception e) {
            //e.printStackTrace();
            //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
            System.out.println("-->警告:输入非法数字!!,请重新输入");
            n = getNUm();
        }
        //必须是0-4
        if (n < 0 || n > 4) {
            System.out.println("--警告,非法命令!!请重新输入");//递归
            n = getNUm();//
        }
        return n;//返回输入的数字
    }

    public static int getGoodsId() {
        int n = 0;
        try {
            n = new Scanner(System.in).nextInt();
        } catch (Exception e) {
            //e.printStackTrace();
            //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
            System.out.println("-->警告:输入非法数字!!,请重新输入");
            n = getGoodsId();
        }
        if (n<0||n>100000) {
            System.out.println("请重新输入");
        }
        return n;//返回输入的数字
    }

    public static int getadd() {
        int n = 0;
        try {
            n = new Scanner(System.in).nextInt();
        } catch (Exception e) {
            //e.printStackTrace();
            //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
            System.out.println("-->警告:输入非法数字!!,请重新输入");
            n = getadd();
        }
        return n;//返回输入的数字
    }

    public static int getPrice() {
        int n = 0;
        try {
            n = new Scanner(System.in).nextInt();
        } catch (Exception e) {
            //e.printStackTrace();
            //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
            System.out.println("-->警告:输入非法数字!!,请重新输入");
            n = getPrice();
        }
        return n;//返回输入的数字
    }

    public static int getupdate() {
        int i = 0;
        try {
            i = new Scanner(System.in).nextInt();
        } catch (Exception e) {
            //e.printStackTrace();
            //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
            System.out.println("-->警告:输入正确的id!!,请重新输入");
            i = getupdate();
        }


        return i;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值