tip
首先建一个Shop类用来定义商品的一些属性:
package com.softeem.demo01;
public class Shop {
private int sno;
private String sname;
private String type;
private double price;
private int num;
private String place;
public Shop() {
}
public Shop(int sno, String sname, String type, double price, int num, String place) {
super();
this.sno = sno;
this.sname = sname;
this.type = type;
this.price = price;
this.num = num;
this.place = place;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
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 getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public void show() {
System.out.println("商品编号为"+sno+"\t"+"商品名称为"+sname+"\t"+"类别为"+type+"\t"+"单价为"+price+"\t"+"库存量为"+num+"\t"+"生产地为"+place+"\t");
}
}
这里把show()方法写在商品类里方便后期调用。
重点是商品管理类,可选择用ArrayList来存放商品信息,我这里用可以扩展的数组来存放商品信息:
package com.softeem.demo01;
import java.util.Scanner;
public class ShopSys {
Shop [] list;
int index;
public ShopSys() {
list=new Shop[2];
}
public int size() {
return index+1;
}
public void extendArray() {
Shop [] newArr=new Shop[list.length+list.length/2];
System.arraycopy(list, 0, newArr, 0, list.length);
list=newArr;
}
public void add(Shop s) {
if(index<list.length) {
extendArray();
}
list[index++]=s;
System.out.println("添加成功");
}
//根据指定价位显示商品
public void findByPrice(double price,double price2) {
for(int i=0;i<list.length;i++) {
Shop s=list[i];
if(s!=null&&s.getPrice()>=price&&s.getPrice()<=price2) {
s.show();
}
}
}
//根据编号显示商品
public void findBySno(int sno) {
for(int i=0;i<list.length;i++) {
Shop s=list[i];
if(s!=null&&s.getSno()==sno) {
s.show();
}
}
}
//根据编号修改商品单价和库存
public void reShop(int sno) {
Scanner sc= new Scanner(System.in);
for(int i=0;i<list.length;i++) {
Shop s=list[i];
if(s!=null&&s.getSno()==sno) {
System.out.println("已经找到了,请输入新的商品单价!!");
double m=sc.nextDouble();
System.out.println("请输入新的库存哦!!");
int n=sc.nextInt();
s.setPrice(m);
s.setNum(n);
s.show();
}
}
}
//显示所有信息
public void showAll() {
for(int i=0;i<list.length;i++) {
Shop s=list[i];
if(s!=null) {
s.show();
}
}
}
//查询所有库存量低于指定数的商品
public void findByNum(int num) {
for(int i=0;i<list.length;i++) {
Shop s=list[i];
if(s!=null&&s.getNum()>=num) {
s.show();
}
}
}
}
下面来编写测试类:
package com.softeem.demo01;
public class TestShop {
public static void main(String[] args) {
ShopSys m=new ShopSys();
m.add(new Shop(1,"康师傅","方便面",3.5,200,"湖北"));
m.add(new Shop(5,"统一","天然水",4.5,100,"湖南"));
m.add(new Shop(3,"农夫山泉","饮用水",5,150,"湖北"));
m.add(new Shop(4,"怡宝","饮用水",2.5,160,"湖北"));
m.add(new Shop(5,"娃哈哈","奶制品",4.0,130,"湖北"));
m.add(new Shop(6,"爽歪歪","奶制品",4.5,120,"湖北"));
System.out.println("=============");
m.findBySno(1);
System.out.println("================");
m.findByPrice(4.0, 4.5);
System.out.println("============");
m.showAll();
System.out.println("==============");
m.findByNum(150);
System.out.println("===========");
m.reShop(3);
}
}