完成一个电商系统的商品模块功能,商品类包含以下属性:商品ID,商品名,类别名,单价,库存量,产地,计量单 位等信息,要求实现商品管理功能,具体如下:
商品添加
商品列表
查询指定id的商品
根据商品id删除商品
根据id修改指定商品的价格
根据id修改指定商品的库存
根据商品类别查询所有商品
查询指定价格区间的商品信息
为了防止一个类中代码过长,我创建三个类来实现功能:
(1)创建商品类:根据商品的属性建立变量(私有化),通过创建set方法和get方法让外界获取,为了方便输入商品信息,创建带参数的构造器,为了方便输出语句创建toString()方法。
(2)创建商品管理类:根据需要实现的管理功能创建方法(这里简化不用Scanner,所以用void,方法中没有返回值)。
(3)创建测试类:通过创建测试类来测试方法的具体功能。
Comm类:
package com.softeem.homework; /** * * @author ASUS * */ public class Comm { private int cid; private String cname; private String lname; private double price; private int num; private String address; private String unit; public Comm(int cid, String cname, String lname, double price, int num, String address, String unit) { super(); this.cid = cid; this.cname = cname; this.lname = lname; this.price = price; this.num = num; this.address = address; this.unit = unit; } public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getUnit() { return unit; } public void setUnit(String unit) { this.unit = unit; } @Override public String toString() { return cid + "\t" + cname + "\t" + lname + "\t" + price + "\t" + num + "\t" + address + "\t" + unit ; } }CommManage类
package com.softeem.homework; import java.util.ArrayList; /** * 商品管理 * @author ASUS * */ public class CommManage { static ArrayList<Comm> list = new ArrayList<>(); /**添加商品*/ public void add(Comm comm){ list.add(comm); } /**商品列表*/ public void findAll(){ for(Comm c:list){ System.out.println(c); } } /**根据id查询商品*/ public Comm findByCid(int cid){ Comm comm = null; for(Comm c:list){ //判断是否找到匹配id的商品 if(c.getCid() == cid){ comm = c; msg(comm); break; } } return comm; } /**根据id删除商品*/ public boolean delete(int cid){ Comm comm = findByCid(cid); if(comm != null){ return list.remove(comm); } return false; } /**根据id修改商品价格*/ public boolean updatePrice(int cid,double price){ Comm comm = findByCid(cid); if(comm != null){ comm.setPrice(price); return true; } return false; } /**根据id修改商品库存*/ public boolean updateUnit(int cid,int num){ Comm comm = findByCid(cid); if(comm != null){ comm.setNum(num); return true; } return false; } /**根据商品类别查询所有商品*/ public ArrayList<Comm> findByLname(String lname){ ArrayList<Comm> comm = new ArrayList<>(); for(Comm c:list){ //查询集合中是否存在员工的部门名称跟参数部门名一致 if(lname.equals(c.getLname())){ comm.add(c); msg(c); } } return comm; } /**根据商品类别查询所有商品*/ public ArrayList<Comm> findByPrice(double price1,double price2){ ArrayList<Comm> comm = new ArrayList<>(); for(Comm c:list){ // 查询指定价格区间的商品 if(price1 <= c.getPrice()&& c.getPrice() <= price2){ comm.add(c); msg(c); } } return comm; } /**输出语句*/ public void msg(Object obj){ System.out.println(obj); } }Test类
package com.softeem.homework; public class Test { public static void main(String[] args) { Comm c1 = new Comm(101,"抱枕","家用",120,500,"杭州","件"); Comm c2 = new Comm(102,"棉被","家用",320,800,"杭州","件"); Comm c3 = new Comm(103,"床单","家用",80,600,"杭州","件"); Comm c4 = new Comm(104,"冰箱","电器",1200,500,"深圳","件"); Comm c5 = new Comm(105,"汽车","工具",32000,1500,"香港","辆"); Comm c6 = new Comm(106,"手机","电器",1120,3500,"香港","部"); CommManage cm = new CommManage(); cm.add(c1); cm.add(c2); cm.add(c3); cm.add(c4); cm.add(c5); cm.add(c6); // cm.findAll(); // cm.findByCid(103); // cm.delete(103); // cm.findAll(); // cm.updatePrice(101, 200); // cm.updateUnit(104, 1000); // cm.findAll(); // cm.findByLname("家用"); cm.findByPrice(100, 500); } }添加商品
查询指定id的商品,根据商品id删除商品
根据id修改指定商品的价格,根据id修改指定商品的库存
根据商品类别查询所有商品,查询指定价格区间的商品信息
