面向对象的基本应用案例

柜台类

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

    public Counter(){
        this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
        this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
        num = 2;
    }
    public void show(){
        System.out.println("-----------柜台的商品列表--------------");
        if (this.num==0) {
            System.out.println("警告,柜台没有商品");
            return;
        }
        for (int i = 0; i < this.goodses.length; i++) {
            if (goodses[i] !=null){
               System.out.println(goodses[i]);
            }
        }
        //System.out.println("还有"+num+"暂时为空");
    }
    //菜单方法
    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.getNum();
            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();
         int id=0;
         for (int i =0; i < this.goodses.length;i++) {
              if (this.goodses[i]!=null &&this.goodses[i].getId()==goodsId) {
                   id=i;
              }
         }System.out.println("商品的原价为:"+goodses[id].getPrice());
         System.out.println("请输入修改后的价格:");
         float newPrice = new Scanner(System.in).nextFloat();
         //需要重置商品信息
         Goods goods = new Goods(goodses[id].getId(), goodses[id].getGoodsName(), newPrice, goodses[id].getDesc());
         //把重置的商品信息放到之前的信息中
         this.goodses[id] = goods;
         System.out.println("价格修改成功");
     }
/*
* 下架商品
* */
    private void delete() {
        System.out.println("-->请输入要下架的商品编号:");
        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);
                //break;//跳出循环

                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 dese = new Scanner(System.in).next();

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

        //把商品对象保存柜台数组中为null的位置
        for (int i = 0; i < this.goodses.length; i++) {
            if (this.goodses[i]==null){
                this.goodses[i] = newGoods;
                this.num++;
                System.out.println("提示商品上架成功");
                return;
            }
        }
    }

商品信息类

public class Goods {
    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(int id, String goodsName, double price, String desc) {
        this.id = id;
        this.goodsName = goodsName;
        this.price = price;
        this.desc = desc;
    }

    public Goods() {
    }

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

工具类

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

测试类

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

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值