构造商品类

文章展示了如何在Java中设计一个商品类Goods,包含商品ID、价格和信息。通过HashMap实现ID查询功能,支持添加、删除、修改和查询商品信息。Goodsdeal类提供了商品管理的接口,实现了增删改查操作。
摘要由CSDN通过智能技术生成
public class Goods {
    private int GoodsId;
    private int GoodsPrice;
    private String GoodsInformation;
    public void Goods(){}
    public void Goods(int GoodsId,int GoodsPrice,String GoodsInformation){
        setGoodsId(GoodsId);
        setGoodsPrice(GoodsPrice);
        setGoodsInformation(GoodsInformation);
        System.out.println(toString());
    }

    public int getGoodsId() {
        return GoodsId;
    }

    public void setGoodsId(int goodsId) {
        GoodsId = goodsId;
    }

    public int getGoodsPrice() {
        return GoodsPrice;
    }

    public void setGoodsPrice(int goodsPrice) {
        GoodsPrice = goodsPrice;
    }

    public String getGoodsInformation() {
        return GoodsInformation;
    }

    public void setGoodsInformation(String goodsInformation) {
        GoodsInformation = goodsInformation;
    }
    public String toString(){
        return "商品id为:"+GoodsId+",商品价格为:"+GoodsPrice+",商品信息为:"+GoodsInformation;
    }
}

一个商品包含id,价格,和信息

设计id用来查询商品

import java.util.HashMap;

public class Goods {
    private int GoodsId;
    private int GoodsPrice;
    private String GoodsInformation;
    HashMap<Integer,String> search=new HashMap<>();

    public void Goods(){}
    public void IdSearch(int GoodsId){
        System.out.println(search.get(GoodsId));
    }
    public void Goods(int GoodsId,int GoodsPrice,String GoodsInformation){

        search.put(GoodsId,GoodsInformation+" "+GoodsPrice);
        setGoodsId(GoodsId);
        setGoodsPrice(GoodsPrice);
        setGoodsInformation(GoodsInformation);
        System.out.println(toString());

    }

    public int getGoodsId() {
        return GoodsId;
    }

    public void setGoodsId(int goodsId) {
        GoodsId = goodsId;
    }

    public int getGoodsPrice() {
        return GoodsPrice;
    }

    public void setGoodsPrice(int goodsPrice) {
        GoodsPrice = goodsPrice;
    }

    public String getGoodsInformation() {
        return GoodsInformation;
    }

    public void setGoodsInformation(String goodsInformation) {
        GoodsInformation = goodsInformation;
    }
    public String toString(){
        return "商品id为:"+GoodsId+",商品价格为:"+GoodsPrice+",商品信息为:"+GoodsInformation;
    }
}

利用HashMap创建IdSearch方法实现用id查询商品

import java.util.HashMap;

public class Goods {
    private int GoodsId;
    private int GoodsPrice;
    private String GoodsInformation;
    HashMap<Integer,String> search=new HashMap<>();

    public void Goods(){}
    public void IdSearch(int GoodsId){
        System.out.println(search.get(GoodsId));
    }
    public void deletegoods(int GoodsId){
        search.remove(GoodsId);
    }
    public void charge(int GoodsId,String xx){
        search.replace(GoodsId,xx);
    }


    public void Goods(int GoodsId,int GoodsPrice,String GoodsInformation){

        search.put(GoodsId,GoodsInformation+" "+GoodsPrice);
        setGoodsId(GoodsId);
        setGoodsPrice(GoodsPrice);
        setGoodsInformation(GoodsInformation);
        System.out.println(toString());

    }

    public int getGoodsId() {
        return GoodsId;
    }

    public void setGoodsId(int goodsId) {
        GoodsId = goodsId;
    }

    public int getGoodsPrice() {
        return GoodsPrice;
    }

    public void setGoodsPrice(int goodsPrice) {
        GoodsPrice = goodsPrice;
    }

    public String getGoodsInformation() {
        return GoodsInformation;
    }

    public void setGoodsInformation(String goodsInformation) {
        GoodsInformation = goodsInformation;
    }
    public String toString(){
        return "商品id为:"+GoodsId+",商品价格为:"+GoodsPrice+",商品信息为:"+GoodsInformation;
    }
}
import java.util.Scanner;

public class Goodsdeal {
    Scanner sc=new Scanner(System.in);
    public void add(){
        System.out.println("输入id");
        int id=sc.nextInt();
        System.out.println("输入价格");
        int price=sc.nextInt();
        System.out.println("输入信息");
        String infor=sc.next();
        Goods goods=new Goods();
        goods.Goods(id,price,infor);

    }
    public void delete(){
        System.out.println("输入id");
        int id=sc.nextInt();
        Goods goods=new Goods();
        goods.deletegoods(id);

    }
    public void charge(){
        System.out.println("输入id");
        int id=sc.nextInt();
        System.out.println("输入价格");
        int price=sc.nextInt();
        System.out.println("输入信息");
        String infor=sc.next();
        Goods goods=new Goods();
        goods.charge(id,infor+" "+price);

    }
    public void search(){
        System.out.println("输入id");
        int id=sc.nextInt();
        Goods goods=new Goods();
        goods.IdSearch(id);

    }
}

添加Goodsdeal实现商品类的增删改查

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值