[柜台商品作业]

商品类Goods

package com.hp.demo0801;

public class Goods {
    //封装..提供get和set
    private  int id;
    private String  goodsName;
    private  double price;
    private  String 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 Goods() {
        super();
    }

    //有参构造器初始化初始化商品对象
    public Goods(int id, String goodsName, double price, String desc) {
        this.id = id;
        this.goodsName = goodsName;
        this.price = price;
        this.desc = desc;
    }
    //重写toString()方法可以直接显示数据

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






柜台类Counter

package com.hp.demo0801;

import java.util.Scanner;

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

    //构造器:无参构造器初始化2个商品
    public Counter(){
        this.goodses[0]=new Goods(1,"巧克力",25,"美味可口,恋爱必备!");
        this.goodses[1]=new Goods(2,"卫龙辣条",1,"隔壁小孩馋哭了!");
        num=2;
    }

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

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

    }
    //菜单方法
    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("----->请输入功能编号");
            int key = InputUtils.getGoodsId();
            switch (key){
                case 1:show();break;
                case 2:add();break;
                case 3:delete();break;
                case 4:update();break;
                case 0: System.exit(0);//退出jvm
        }

       }

    }
    //调整商品
    private void update() {
        System.out.println("---请输入要修改商品的id-------");
        int goodsId=InputUtils.getGoodsId();
        if (goodsId>10&&goodsId<1) {
            System.out.println("-->商品不存在");
            return;
        }else{
            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(goodsId, name, price, desc);

            for (int i = 0; i < this.goodses.length; i++) {
                if (i== goodsId-1){
                    this.goodses[i]=newGoods;
                    System.out.println("修改成功");
                    return;
                }

            }

        }



    }
    //下架商品
    private void delete() {
        int goodsId = InputUtils.getGoodsId();
        //找到对应商品id的商品对象
        for (int i = 0; i < this.goodses.length; i++) {
            //查找对应id的商品
            if (this.goodses[i]!=null & this.goodses[i].getId()==goodsId) {
                //找到以后,删除
                this.goodses[i] = null;
                //商品数量减一
                this.num--;
                System.out.println("-->下架成功"+this.num);
                return;
            }
        }
        //如果没找到商品就会执行到这里
        System.out.println("-->警告 未找到商品!!");
    }
    //上架商品
    private void add() {
        //判断是否有空余位置
        if(this.num==this.goodses.length){
            System.out.println("-->警告:柜台已满,不能再添加商品了");
            return;
        }
        System.out.println("-->请输入商品id:");
        int goodsId =InputUtils.getGoodsId();
        //id不能重复
        for (int i = 0; i < this.goodses.length; i++) {
            if (this.goodses[i]!=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("-->请输入商品价格");
        double price = InputUtils.getPrice();

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

        //把输入的信息装载到goods对象
        Goods newGoods = new Goods(goodsId,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;
            }
        }

    }

}

输入工具类InputUtils

package com.hp.demo0801;

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

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

}

测试类CounterTest

package com.hp.demo0801;

public class CounterTest {
    public static void main(String[] args) {
        //创建柜台对象
        Counter counter = new Counter();
        //开启菜单
        counter.main();
    }
}

运行结果

---------1.展示商品   2.上架商品-------
---------3.下架商品   4.调整商品-------
---------0 退出-------
----->请输入功能编号

1
-------------  柜台的商品列表  --------------
Goods{id=1, goodsName='巧克力', price=25.0, desc='美味可口,恋爱必备!'}
Goods{id=2, goodsName='卫龙辣条', price=1.0, desc='隔壁小孩馋哭了!'}
------------------------------------------
----------------------------
---------1.展示商品   2.上架商品-------
---------3.下架商品   4.调整商品-------
---------0 退出-------
----->请输入功能编号
2
-->请输入商品id:
3
--->提示:商品id可以使用
-->请输入商品名称
1
-->请输入商品价格
1
-->请输入商品描述
1
-->提示:商品上架成功!!柜台商品数量3
----------------------------
---------1.展示商品   2.上架商品-------
---------3.下架商品   4.调整商品-------
---------0 退出-------
----->请输入功能编号
1
-------------  柜台的商品列表  --------------
Goods{id=1, goodsName='巧克力', price=25.0, desc='美味可口,恋爱必备!'}
Goods{id=2, goodsName='卫龙辣条', price=1.0, desc='隔壁小孩馋哭了!'}
Goods{id=3, goodsName='1', price=1.0, desc='1'}
------------------------------------------
----------------------------
---------1.展示商品   2.上架商品-------
---------3.下架商品   4.调整商品-------
---------0 退出-------
----->请输入功能编号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值