商品柜台管理

创建实体类

package cn.hp;

public class Goods {
    private int id;
    private String goodsName;
    private double price;
    private String desc;

    public Goods() {

    }

    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;
    }

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


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

添加功能

package cn.hp;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.SortedMap;

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

    public Counter() {
        this.goodses[0] = new Goods(10001,"乐事薯片",6.5,"最喜欢青柠味的");
        this.goodses[1] = new Goods(10002, "可口可乐", 3, "好喝。。嗝..");
        this.goodses[2] = new Goods(10003, "巧乐兹", 5.5, "雪糕刺客");

    }
    //判断是否为空
    public void main(){
        //菜单管理
        while(true){
            System.out.println("--------------------------------");
            System.out.println("-- 1.商品列表    2.上架商品---");
            System.out.println("-- 3.下架商品    4.调整价格---");
            System.out.println("-- 0.退出                ---");
            //选择功能
            int key = new Scanner(System.in).nextInt();
            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(){
        for (int i = 0; i <10; i++) {
            if (goodses[i]!=null) {
                System.out.println(goodses[i]);
            }
        }
    }
    //添加商品信息
    Scanner sc=new Scanner(System.in);

    public void add(){
        Goods good =new Goods();
        System.out.println("输入商品编号");
        int a=sc.nextInt();
        good.setId(a);
        System.out.println("输入商品名称");
        String b=sc.next();
        good.setGoodsName(b);
        System.out.println("输入商品价格");
        double c=sc.nextDouble();
        good.setPrice(c);
        System.out.println("输入商品介绍");
        String d=sc.next();
        good.setDesc(d);
        for (int i = 0; i < goodses.length; i++) {
            if(goodses[i]==null){
                goodses[i]=good;
                num++;
                System.out.println("--添加商品结束---");
                return;
            }
        }

    }
    public void delete(){
        System.out.println("输入要删除商品的序号");
        int a=sc.nextInt();
        for (int i = 0; i < goodses.length; i++) {
            if(goodses[i]!=null){
                goodses[a-1]=null;
                System.out.println("删除成功!!");
                break;
            }
        }

    }
    public void update(){
        System.out.println("输入要修改商品价格的序号");
        int a=sc.nextInt();
        System.out.println("输入要修改de价格");
        double b=sc.nextDouble();
        for (int i = 0; i < goodses.length ; i++) {
            if(goodses[i]!=null){
                goodses[a-1].setPrice(b);
                System.out.println("修改成功!!");
                break;
            }
        }
    }
}

测试输出方法

package cn.hp;

public class GoodsTest {
    public static void main(String[] args) {

        Counter counter = new Counter();

        counter.main();

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值