倉庫管理

public class ArticleManage {

// 創(chuàng)建一個實體的倉庫對象,并初始化

ArticleSet articleSet = new ArticleSet();

// 初始化倉庫,放入起始商品

public void initial () {

Article xiaomi11 = new Article();

xiaomi11.setArticle("小米11" , 30 , 1999,0);

Article xiaomi11pro = new Article();

xiaomi11pro.setArticle("小米11pro",40 , 2999, 0);

Article xiaomuUltra = new Article();

xiaomuUltra.setArticle("小米增強版" ,50,3999,0 );

articleSet.articles[0] = xiaomi11;

articleSet.articles[1] = xiaomi11pro;

articleSet.articles[2] = xiaomuUltra;

}

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("請輸入你要執(zhí)行的功能編號:");

    Scanner scanner = new Scanner(System.in);

    int gongnengBianhao = scanner.nextInt();

    switch ( gongnengBianhao ) {

        case 1:

            System.out.println("查看商品信息");

            chakan();

            break;

        case 2:

            System.out.println("新增商品信息");

            add();

            break;

        case 3:

            System.out.println("刪除商品信息");

            delete();

            break;

        case 4:

            System.out.println("賣出商品");

            sell();

            break;

        case 5:

            System.out.println("排行榜");

            leaderboard();

            break;

        case 6:

            System.out.println("感謝使用,已經(jīng)退出");

            flag = false ;

            break;

        default:

            System.out.println("你輸入的功能編號有誤");

            break;

    }

}while ( flag );

}

// 排行榜

public void leaderboard(){

int count = 0 ; // 統(tǒng)計原始數(shù)組使用的長度

for ( int i = 0 ; i < articleSet.articles.length; i ++ ) {

    if ( articleSet.articles[i] != null ) {

        count ++ ;

    }

}

// 根據(jù)使用的長度臨時新建一個數(shù)組,這個數(shù)組元素存滿

Article[] newTemp = new Article[count];

// 把舊數(shù)組中的元素全部拷貝到新數(shù)組中,新數(shù)組裝滿元素

for ( int i =0 ; i < count ; i ++ ) {

    newTemp[i] = articleSet.articles[i];

}

// 排序  (冒泡排序)

for ( int i =0 ; i < newTemp.length-1 ; i ++ ) { // 讓所有元素參與排序

    for ( int j = 0 ; j < newTemp.length -i-1 ; j++ ) { // 讓當(dāng)前元素和它后面的元素對比

        if ( newTemp[j+1] != null ) { // 保證下一個要對比的元素存在

            if ( newTemp[j].number < newTemp[j+1].number ) {

                // 兩個元素交換位置,需要借助第三方臨時變量做存儲

                Article temp = newTemp[j];

                newTemp[j] = newTemp[j+1];

                newTemp[j+1] = temp;

            }

        }

    }

}

// 顯示名次

System.out.println("名次: \t 銷售數(shù)量 \t 商品名稱");

for (int i = 0; i < newTemp.length; i++) {

    System.out.println( (i+1) +"\t" + newTemp[i].number + "\t" + newTemp[i].name );

}

}

public void sell(){

System.out.println("請輸入你要賣出的商品的名字:");

Scanner scanner = new Scanner(System.in);

String name = scanner.next();

boolean flag = true;

for (int i= 0 ; i < articleSet.articles.length ; i++){  // 小米11  小米11pro 小米增強版

    if ( articleSet.articles[i] != null && articleSet.articles[i].name.equals(name) ) {

        System.out.println("請輸入要賣出的數(shù)量:");

        int maichu = scanner.nextInt();

        if ( maichu < articleSet.articles[i].amount ) { // 賣出數(shù)量 < 庫存數(shù)

            // 新庫存 = 舊庫存 - 賣出數(shù)量

            articleSet.articles[i].amount = articleSet.articles[i].amount - maichu;

            // 新售出 = 舊售出 + 賣出數(shù)量

            articleSet.articles[i].number = articleSet.articles[i].number + maichu;

            flag = true;

        }else {

            flag = false;

            System.out.println("庫存不夠了");

        }

        break;  // 找到對應(yīng)的位置,已經(jīng)完成了修改,后續(xù)的元素直接跳過,中斷循環(huán)

    } else {

        flag = false;

        // System.out.println("你要賣出的商品沒找到");

    }

}

if(flag) {

    System.out.println("賣出成功");

}else{

    System.out.println("賣出失敗");

}

}

private void delete() {

System.out.println("輸入你要刪除的商品編號:");

Scanner scanner = new Scanner(System.in);

int delNo = scanner.nextInt();

boolean flag = true;

for ( int i = 0 ; i < articleSet.articles.length ; i ++ ) {

    if ( delNo ==  (i+1) && articleSet.articles[i] != null ) {

        int j = i ; // 備份下標(biāo)

        while ( articleSet.articles[j+1] != null ) {

            articleSet.articles[j] = articleSet.articles[j+1] ;

            j++ ;

        }

        articleSet.articles[j] = null;

        flag = true;

        break;  // 操作完成,直接中斷for循環(huán),后續(xù)的null元素?zé)o需操作

    } else {

        flag =false;

    }

}

if (flag) {

    System.out.println("刪除成功");

}else {

    System.out.println("刪除失??!");

}

}

public void chakan() {

System.out.println("編號 \t 名稱 \t 庫存 \t 單價 \t 售出數(shù)量");

for (int i = 0; i < articleSet.articles.length; i++) {

    if ( articleSet.articles[i] != null ) {

        articleSet.articles[i].print(i+1);

    }

}

}

public void add() {

// 接受用戶的輸入

Scanner scanner = new Scanner(System.in);

System.out.println("請輸入商品的名字:");

String name = scanner.next();

System.out.println("價格:");

double price = scanner.nextDouble();

System.out.println("庫存:");

int kucun = scanner.nextInt();

System.out.println("售出數(shù)量:");

int number  = scanner.nextInt();

// 把接受到的數(shù)據(jù)封裝到對象中

Article newArticle = new Article();

newArticle.setArticle(name , kucun ,price , number );

for ( int i =0 ; i < articleSet.articles.length ; i++ ) {

    if ( articleSet.articles[i] == null ) {  //從前往后遍歷數(shù)組,找到第一個沒有存元素的位置

        articleSet.articles[i]  = newArticle;  // 找到空位置,把新商品存入

        break;  // 只加入第一個位置,后續(xù)的位置不再判斷

    }

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 夜鶯2517閱讀 128,155評論 1 9
  • 版本:ios 1.2.1 亮點: 1.app角標(biāo)可以實時更新天氣溫度或選擇空氣質(zhì)量,建議處女座就不要選了,不然老想...
    我就是沉沉閱讀 7,451評論 1 6
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂有人憂愁,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,834評論 28 54
  • 兔子雖然是枚小碩 但學(xué)校的碩士四人寢不夠 就被分到了博士樓里 兩人一間 在學(xué)校的最西邊 靠山 兔子的室友身體不好 ...
    待業(yè)的兔子閱讀 2,767評論 2 9

友情鏈接更多精彩內(nèi)容