从设计之初,把柜台商品管理的设计概要和需求,以及后续 一步一步如何设计的,文字+代码

本文介绍了Java编程中的Goods类,展示了如何定义属性、构造方法、get和set方法,以及toString方法。Counter类展示了如何操作商品数组,包括上架、下架和价格修改。涉及的关键技术有对象实例化、数据结构和基本操作。
摘要由CSDN通过智能技术生成

Goods类

package com.alibaba.demo03;
/**
 * 属性
 * 	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, int price, String desc) {
        this.id = id;
        this.goodsName = goodsName;
        this.price = price;
        this.desc = desc;
    }

    //get和set
    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;
    }

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

Counter类

package com.alibaba.demo03;

import java.util.Scanner;

public class Counter {
    Goods[]goodses = new Goods[10];
    int num;

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

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

    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);
            }
        }
    }
    //下架
    private void delete() {
        System.out.println("请输入要下架的商品id:");
        int id=InputUtils.getid();
        for (int i = 0; i < this.goodses.length; i++) {
            if (this.goodses[i]==null&&this.goodses[i].getId()==id){
                this.goodses[i]=null;
                System.out.println("下架商品成功!"+"当前商品数量为"+this.num);
                this.num--;
                return;
            }
        }
    }
//上架
    private void add() {
        if(this.num==this.goodses.length){
            System.out.println("柜台已满,不能再添加商品");
            return;
        }
        System.out.println("请输入商品id:");
        int goodsId = InputUtils.getGoodsId();
        for (int i = 0; i < this.goodses.length; i++) {
            if(this.goodses[i]!=null && this.goodses[i].getId()==goodsId){
                System.out.println("当前id已存在!!请重新输入");
                add();
                return;
            }
        }
        System.out.println("请输入商品的名称:");
        String name = new Scanner(System.in).next();
        System.out.println("请输入商品的价格:");
        double price = new Scanner(System.in).nextDouble();
        System.out.println("请输入商品的描述:");
        String desc = new Scanner(System.in).next();

        Goods newGoods = new Goods(goodsId, name, (int) 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("商品上架成功,柜台商品数量"+this.num);
                return;
            }
        }
    }
    //修改
    private void update() {
        System.out.println("请输入要修改的商品id:");
        int id = InputUtis.getid();
        for (int i = 0; i < this.goodses.length; i++) {
            if (this.goodses[i].getId() == id) {
                System.out.println(this.goodses[i]);
            }
            System.out.println("请输入要修改的商品价格:");
            double price = InputUtils.getprice();
            for (int j = 0; j < this.goodses.length; j++) {
                if (this.goodses[j].getPrice() != price) {
                    this.goodses[j].setPrice(price);
                    System.out.println("修改商品价格成功!" + "\n" + "修改之后的商品信息:" + this.goodses[i]);
                    return;
                }
            }
        }
    }
}

InputUtils类

package com.alibaba.demo03;

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();
            System.out.println("输入不正确,请重新输入");
            n = getNum();
        }

        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();
            System.out.println("输入不正确,请重新输入");
            n = getGoodsId();
        }
        return n;
    }

    public static double getPrice(){
        double n = 0;
        try {
            n = new Scanner(System.in).nextDouble();
        } catch (Exception e) {
            System.out.println("输入不正确,请重新输入");
            n = getPrice();
        }
        return n;
    }

    public static int getid(){
        int id =0;
        try {
            id=new Scanner(System.in).nextInt();
        } catch (Exception e) {
            id=getid();
        }
        return id;
    }
    public static double getprice(){
        double price =0;
        try {
            price=new Scanner(System.in).nextDouble();
        } catch (Exception e) {
            price=getprice();
        }
        return price;
    }
}

CounterText测试类

package com.alibaba.demo03;

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

运行结果

--------------------------
1.展示商品    2.上架商品
3.下架商品    4.调整价格
0.退出
请输入编号:
1
Goods{id=1001, goodsName='巧克力', price=25.0, desc='美味可口,恋爱必备!'}
Goods{id=1002, goodsName='卫龙辣条', price=1.0, desc='隔壁小孩馋哭了!'}
--------------------------
1.展示商品    2.上架商品
3.下架商品    4.调整价格
0.退出
请输入编号:
2
请输入商品id:
1003
请输入商品的名称:
蛋糕
请输入商品的价格:
27
请输入商品的描述:
香甜软糯
商品上架成功,柜台商品数量3
--------------------------
1.展示商品    2.上架商品
3.下架商品    4.调整价格
0.退出
请输入编号:
1
Goods{id=1001, goodsName='巧克力', price=25.0, desc='美味可口,恋爱必备!'}
Goods{id=1002, goodsName='卫龙辣条', price=1.0, desc='隔壁小孩馋哭了!'}
Goods{id=1003, goodsName='蛋糕', price=27.0, desc='香甜软糯'}
--------------------------
1.展示商品    2.上架商品
3.下架商品    4.调整价格
0.退出
请输入编号:
4
请输入要修改的商品id:
请输入要修改的商品价格:
38
修改商品价格成功!
修改之后的商品信息:Goods{id=1001, goodsName='巧克力', price=38.0, desc='美味可口,恋爱必备!'}
--------------------------
1.展示商品    2.上架商品
3.下架商品    4.调整价格
0.退出
请输入编号:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值