```
package com.company;
public class Article {
? ? //把常用的屬性 封禁
? ? public String name;? ? //名字
? ? public int amount ;? ? //庫存
? ? public double price;? //單價
? ? public int number;? ? //售出數量
? ? public? void print(int index) {
? ? ? ? System.out.println(index + "\t" + name + "\t" +amount + "\t" + price + "\t" + number);
? ? }
? ? //
? ? public void setArticle(String mingzi , int kucun , double danjia , int shouchu){
? ? ? ? name = mingzi;
? ? ? ? amount = kucun;
? ? ? ? price = danjia;
? ? ? ? number = shouchu;
? ? }
}
package com.company;
/*
*
* 商品集合 便于管理多個商品
* ArticleSet 商品集合
*/
public class ArticleSet {
?? // 準備一個數組,里面存儲商品,作為倉庫
?? Article [] articles = new Article[30];
package com.company;
import java.util.Scanner;
/*
* 商品管理員
*/
public class ArticleManage {
? ? // 創建一個商品集合(倉庫)實例
? ? ArticleSet articleSet = new ArticleSet();
? ? //對新建的倉庫進行初始化
? ? public void initial() {
? ? ? ? //創建商品
? ? ? ? Article xiaomi11 = new Article();
? ? ? ? xiaomi11.setArticle("小米11", 50, 2999, 1);
? ? ? ? Article xiaomi11pro = new Article();
? ? ? ? xiaomi11.setArticle("小米pro", 30, 3999, 2);
? ? ? ? //把商品放入倉庫中
? ? ? ? articleSet.articles[0] = xiaomi11;
? ? ? ? articleSet.articles[1] = xiaomi11pro;
? ? }
? ? //起始菜單
? ? public void startMenu() {
? ? ? ? boolean flag = true;
? ? ? ? do {
? ? ? ? ? ? System.out.println("*********************");
? ? ? ? ? ? System.out.println("1 查看商品信息");
? ? ? ? ? ? System.out.println("2 新增商品信息");
? ? ? ? ? ? System.out.println("3 刪除商品信息");
? ? ? ? ? ? System.out.println("4 賣出商品");
? ? ? ? ? ? System.out.println("5 商品銷售排行榜");
? ? ? ? ? ? System.out.println("6 退出系統");
? ? ? ? ? ? System.out.println("*********************");
? ? ? ? ? ? System.out.println("請輸入你要執行的操作");
? ? ? ? ? ? Scanner scanner = new Scanner(System.in);
? ? ? ? ? ? int gongnengbianhao = scanner.nextInt();
? ? ? ? ? ? switch (gongnengbianhao) {
? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? System.out.println("查看商品信息");
? ? ? ? ? ? ? ? ? ? chakan();? //調用查看商品信息的方法
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? System.out.println("新增商品信息");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? System.out.println("刪除商品信息");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? System.out.println("賣出商品");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? ? ? System.out.println("排行榜");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 6:
? ? ? ? ? ? ? ? ? ? System.out.println("退出系統");
? ? ? ? ? ? ? ? ? ? flag = false;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? System.out.println("你輸入的有誤");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? }while (flag) ;
? ? ? ? }
? ? //查看商品信息
? ? public void chakan(){
? ? ? ? System.out.println("編號: \t 名稱 \t 價格 \t 庫存 \t 售出數量");
? ? ? ? for ( int i = 0 ; i < articleSet.articles.length ; i++){
? ? ? ? ? ? if (articleSet.articles[i] !=null){
? ? ? ? ? ? ? ? articleSet.articles[i].print(i);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //添加商品
? ? public void add(){
? ? ? ? System.out.println("請輸入商品名稱");
? ? ? ? String name = input.next();
? ? ? ? System.out.println("請輸入價格");
? ? ? ? int price=input.nextInt();
? ? ? ? System.out.println("請輸入庫存");
? ? ? ? int amount=input.nextInt();
? ? }
? ? }
package com.company;
public class Demo {
? ? public static void main(String[] args) {
? ? ? ? ArticleManage articleManage = new ArticleManage();
? ? ? ? articleManage.initial();
? ? ? ? articleManage.startMenu();
? ? }
}
```