用java写一个简易的商品管理功能
功能要求
1创建一个商品类,商品的属性有(编号,名字,类型,价格,数量,出生地)
2创建一个商品管理类,功能有如下几点:
可以存储商品的信息
进行商品的添加功能
显示所有商品的信息
查询指定价格范围的商品并显示
根据编号查询商品的信息并显示
根据编号修改修改商品的单价和库存
查询所有库存量低于指定数的商品信息
代码实现
1.新建商品类
public class Store {
//属性
private int number;
private String name;
private String type;
private double price;
private int count;
private String bornPlace;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getBornPlace() {
return bornPlace;
}
public void setBornPlace(String bornPlace) {
this.bornPlace = bornPlace;
}
//构造器
public Store(int number,String name,String type,double price,int count,String bornPlace) {
this.number=number;
this.name=name;
this.type=type;
this.price=price;
this.count=count;
this.bornPlace=bornPlace;
}
//显示商品的信息
public void details() {
System.out.println(number + "\t" + name + "\t" + type + "\t" + price + "\t" + count + "\t" + bornPlace);
}
}
2.商品管理
public class StoreManage {
//新建一个数组管理商品
Store [] list =new Store[100];
//初始化索引
int index=0;
//商品添加功能
public void add(Store s) {
list[index]=s;
index++;
}
//显示所有商品的信息
public void showAll() {
for(int i=0;i<list.length;i++) {
Store s=list[i];
if(s!=null) {
s.details();
}
}
}
//查询指定价格范围的商品并显示
public void pricerange(double PriceHigh,double PriceLow) {
for(int i=0;i<list.length;i++) {
Store s=list[i];
if(s!=null&&(s.getPrice()<PriceHigh&&s.getPrice()>=PriceLow)) {
s.details();
}
}
}
//根据编号查询商品的信息并显示
public void AccordingNoRefer(int num) {
for(int i=0;i<list.length;i++) {
Store s=list[i];
if(s!=null&&s.getNumber()==num) {
s.details();
}
}
}
//根据编号修改修改商品的单价和库存
public void AccordingNoChange(int num,double changeprice,int changecount) {
for(int i=0;i<list.length;i++) {
Store s=list[i];
if(s!=null&&s.getNumber()==num) {
s.setPrice(changeprice);
s.setCount(changecount);
s.details();
}
}
}
//查询所有库存量低于指定数的商品信息
public void Undercount(int Count) {
for(int i=0;i<list.length;i++) {
Store s=list[i];
if(s!=null&&s.getCount()<Count) {
s.details();
}
}
}
}
3.测试
import java.util.Scanner;
public class StoreTest {
public static void main(String[] args) {
//新建一个测试类
StoreManage st=new StoreManage();
Scanner sc=new Scanner(System.in);
//添加商品
st.add(new Store(1, "外星人", "电脑", 9999, 1000, "上海"));
st.add(new Store(2, "苹果", "手机", 8000, 2000, "美国"));
st.add(new Store(3, "劳力士", "手表", 4000 ,5000, "中国"));
st.add(new Store(4, "耐克","鞋子", 2000, 3000, "中国"));
st.add(new Store(5, "雷蛇","键盘", 900 , 4000, "上海"));
st.add(new Store(6, "德芙", "巧克力", 50, 8000, "上海"));
//显示
st.showAll();
System.out.println("=============================================");
//查询指定价格范围的商品并显示
System.out.println("输入价格范围(先输入高价格)");
int i=sc.nextInt();
int j=sc.nextInt();
st.pricerange(i, j);
System.out.println("=============================================");
//根据编号查询商品的信息并显示
System.out.println("请输入商品的编号");
int m=sc.nextInt();
st.AccordingNoRefer(m);
System.out.println("=============================================");
//根据编号修改商品的单价和库存
System.out.println("请输入商品的编号");
int q=sc.nextInt();
System.out.println("请输入需要修改的单价");
int w=sc.nextInt();
System.out.println("请输入需要修改的库存");
int e=sc.nextInt();
st.AccordingNoChange(q, w, e);
System.out.println("=============================================");
//查询所有库存量低于指定数的商品信息
System.out.println("请输入指定库存");
int r=sc.nextInt();
st.Undercount(r);
}
}
运行结果
提示:这里对文章进行总结: