简易的柜台商品管理系统2.0

一,创建goods类

package cn.hp.dome3;

/**
 * 			id 商品编号
 * 			goodsName 商品名称
 * 			price 商品价格
 * 			desc 商品描述
 */
public class Goods {
    private int id;
    private String goodsName;
    private double price;
    private String desc;

    public Goods() {
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public double getPrice() {
        return price;
    }

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

    public String getDesc() {
        return desc;
    }

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

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

二,创建柜台类

public class Counter {
    private Goods[]goodses = new Goods[10];//柜台
    private int num; //柜台数量
}

三,无参构造器初始化2个商品

    public Counter() {
        this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
        this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
        num=2; //商品数量为2
    }

四,提供工具类

package cn.hp.dome3;

import java.util.Scanner;

/**
 * 提供一些输入的方法
 */
public class InputUtils {

    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 getNumid(){
        int n =0;
        try {
            n=new Scanner(System.in).nextInt();
        } catch (Exception e) {
            //e.printStackTrace();
            //如果输入的非数字,就会抛出异常,被catch语句捕获,在这里处理异常
            System.out.println("-->警告:输入非法数字!!,请重新输入");
            n=getNumid();//递归  调用自身
        }
        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 void main(){
        while (true) {
            System.out.println("-----------------------------");
            System.out.println("-----1.展示商品   2.上架商品----");
            System.out.println("-----3.下架商品   4.调整价格-----");
            System.out.println("-----0.退出-------------------");
            System.out.println("-----------------------------");

            //选择功能,控制台输入
            System.out.println("-->请输入功能编号:");
            int key = InputUtils.getNum();
            switch (key) {
                case 1:
                    show();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    update();
                    break;
                case 0:
                    System.exit(0);
            }
        }
    }

六,展示柜台(查)

 public void show(){
        System.out.println("----------- 柜台的商品列表 ----------");
        if (this.num == 0) {
            System.out.println("警告:柜台没有商品..");
        }
        for (int i = 0; i < goodses.length; i++) {
            if (goodses[i]!= null) {
                System.out.println(goodses[i]);
            }
        }
        System.out.println("----------------------");
    }

七,上架商品(增)

    private void add() {
        //判断是否为有多余位置
        if (this.num == this.goodses.length) {
            System.out.println("-->警告:柜台已满,不能再添加商品");
            return;
        }
        System.out.println("-->请输入商品id:");
        int getNumid = InputUtils.getNumid();
        //丰富:id不能重复
        for (int i = 0; i < this.goodses.length; i++) {
            if (this.goodses[i]!=null && this.goodses[i].getId()==getNumid) {
                System.out.println("-->警告:当前编号已存在!!!!请重新输入");
                add();
                return;//由于重复了,后续不执行
            }
        }

        //程序能走到这里,说明id没有重复
        System.out.println("-->提示:商品id可以使用!!");

        System.out.println("-->请输入商品名称:");
        String name =new Scanner(System.in).next();

        System.out.println("-->请输入商品价格:");
        double price=InputUtils.getprice();

        System.out.println("-->请输入商品描述:");
        String desc=new Scanner(System.in).next();

        //把输入的信息装载到Goods对象
        Goods newGoods = new Goods(getNumid,name,price,desc);

        //把商品对象保存柜台数组中为null的位置
        for (int i = 0; i < this.goodses.length; i++) {
            //如果这个下标没有Goods商品对象,null,就把商品放进来
            if (this.goodses[i] == null) {
                this.goodses[i] = newGoods;
                this.num++; //商品数量加一
                System.out.println("-->提示:商品上架成功!!柜台商品数量"+this.num);
                return;
            }
        }
    }

八,下架商品(删)

private void delete() {
        System.out.println("-->请输入要下架的商品id:");
        int getNumid = InputUtils.getNumid();
        //第一件事 找到这个对应的商品id的商品对象
        for (int i = 0; i < this.goodses.length; i++) {
            //查找对应id的商品
            if (this.goodses[i]!=null && this.goodses[i].getId()==getNumid){
                //找到以后,删除
                this.goodses[i]=null;
                //商品数量减一
                this.num--;
                System.out.println("-->提示:下架成功"+this.num);
                return;//跳出方法(只有找到商品才执行)
            }
        }
        //如果没找到商品,就会执行到这里
        System.out.println("-->警告:未找到商品!!!");
    }

九,调整价格(改)

    private void update() {
        System.out.println("-->请输入要修改的商品id:");
        int updateid=InputUtils.getNumid();

        //第一件事 找到要修改的商品的id
        for (int i = 0; i < this.goodses.length; i++) {
            //查找到对应id商品
            if (this.goodses[i]!=null && this.goodses[i].getId()==updateid) {
                System.out.println("请输入商品新的价格:");
                this.goodses[i].setPrice(InputUtils.getprice());
                System.out.println("-->修改成功");
                return;
            }
        }
        System.out.println("-->修改失败,请检查后重新修改");
        update();
        return;
    }

十,测试

    public static void main(String[] args) {
        Counter ps=new Counter();
        ps.main();
    }

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值