完成一個電商系統的商品管理小程式
ps:註釋比較清晰 看題↓
/**
完成一個電商系統的商品庫存管理,包含兩個類:
-
- 商品類(編號,名稱,類別,單價,庫存量,生產地)
-
- 商品管理類
- 要求在管理類中完成如下功能:
-
- 商品新增
-
- 查詢指定價格範圍的商品並顯示
-
- 根據編號查詢商品資訊並顯示
-
- 根據編號修改商品的單價和庫存
-
- 顯示所有商品資訊
-
- 查詢所有庫存量低於指定數的商品資訊
- @author ZyKun.
商品類↓
public class Goods {
//商品編號
private int id;
//商品名
private String name ;
//商品型別
private String type ;
//商品價格
private double price;
//商品庫存
private int sum ;
//商品生產地
private String yieldly;
//有參構造器
public Goods(int id, String name, String type, double price, int sum, String yieldly) {
super();
this.id = id;
this.name = name;
this.type = type;
this.price = price;
this.sum = sum;
this.yieldly = yieldly;
}
//set get
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public String getYieldly() {
return yieldly;
}
public void setYieldly(String yieldly) {
this.yieldly = yieldly;
}
//輸出商品資訊
public void show() {
System.out.println(id+"\t"+name+"\t"+type+"\t"+price+"\t"+sum+"\t"+yieldly);
}
}
管理類↓
public class Manage {
//宣告一個Goods陣列
private Goods [] list;
//宣告一個索引
private int index;
//物件構造時初始一個數組
public Manage() {
list = new Goods[100];
}
//商品新增
public void add (Goods g) {
list [index++]=g;
}
//查詢指定價格範圍的商品並顯示
public void findGoodsByPrice(double max , double min) {
for(int i =0; i<list.length; i++) {
Goods g = list[i];
//判斷g物件是否為空,並且是否在引數的範圍內
if(g != null && g.getPrice()>=min && g.getPrice()<=max) {
//顯示當前學生的資訊
g.show();
}
}
}
//根據編號查詢商品資訊並顯示
public void findGoodsById(int id) {
int i = findByid(id);
//判斷i是否為-1
if(i != -1) {
list[i].show();
}
}
//根據編號修改商品的單價和庫存
public void updatePriceSum(int id,double price,int sum) {
int i =findByid(id);
//判斷i是否為-1
if(i!=-1) {
//修改索引為i的商品價格
list[i].setPrice(price);
//修改索引為i的商品庫存
list[i].setSum(sum);
System.out.println("修改成功!");
list[i].show();
}else {
System.out.println("位置找到此編號的商品");
}
}
//顯示所有商品資訊
public void showAll() {
for(int i =0; i<list.length; i++) {
Goods g = list[i];
//判斷商品物件在陣列中是否為空
if(g!=null) {
g.show();
}
}
}
//查詢所有庫存量低於指定數的商品資訊
public void findGoodsBySmallSum(int sum) {
for(int i =0; i<list.length; i++) {
Goods g =list[i];
if(g!=null && g.getSum()<=sum) {
g.show();
}
}
}
// //根據提供的編號查詢商品物件
public int findByid(int id) {
for(int i=0; i<list.length; i++) {
Goods g =list[i];
if(g!=null && g.getId() == id) {
return i ;
}
}
return -1;
}
}
測試類
public class Test {
public static void main(String[] args) {
//建立名為m的Manage物件
Manage m =new Manage();
//用名為m的物件去呼叫Manage中的add方法(方法中建立商品物件並傳入相應的引數)
m.add(new Goods(1,"奧利奧","餅乾",6.0,100,"鄂州"));
m.add(new Goods(2,"趣多多","餅乾",5.0,100,"武漢"));
m.add(new Goods(3,"紅牛","飲料",8.0,100,"荊州"));
m.add(new Goods(4,"衛龍","零食",3.0,100,"襄陽"));
m.add(new Goods(5,"良品鋪子","零食",10.0,100,"黃岡"));
//用名為m的物件去呼叫Manage中的showAll方法(打印出目前狀態下的所有商品資訊)
m.showAll();
System.out.println("--------------");
//用名為m的物件去呼叫Manage中的findGoodsByPrice方法(查詢指定價格範圍的商品並顯示)
m.findGoodsByPrice(7, 3);
System.out.println("---------------------");
//用名為m的物件去呼叫Manage中的findGoodsById方法(根據編號查詢商品資訊並顯示)
m.findGoodsById(4);
System.out.println("--------------");
//用名為m的物件去呼叫Manage中的updatePriceSum方法(根據編號修改商品的單價和庫存)
m.updatePriceSum(4, 0.5, 300);
System.out.println("---------");
//用名為m的物件去呼叫Manage中的findGoodsBySmallSum方法(查詢所有庫存量低於指定數的商品資訊)
m.findGoodsBySmallSum(300);
System.out.println("--------------");
}
}
最後看看效果叭
ps:小白一枚,僅供參考!
本文章已修改原文用詞符合繁體字使用者習慣使其容易閱讀
版權宣告:此處為CSDN博主「ZyKun、」的原創文章,依據CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/weixin_51877577/article/details/109542065