2021-04-12


/* 商品的集合(仓库)*/
public class Demo02 {
    Article[] articles  =  new Article[30];
}
package Demo01;


/*商品类,对商品属性的封装*/
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 Demo01;

import java.util.Scanner;


/*商品管理类*/
public class ArticleMange {
    //创建一个实体的仓库对象 并实体化
    Demo02 articleSet = new Demo02();

    //初始化仓库,并放入起始商品
    public void inital() {
        Article xiaomi11 = new Article();
        xiaomi11.setArticle("小米11", 30, 1999, 0);
        Article xiaomi11pro = new Article();
        xiaomi11pro.setArticle("小米11pro", 40, 2999, 0);
        Article xiaomiUltra = new Article();
        xiaomiUltra.setArticle("小米增强版", 50, 3999, 0);
        articleSet.articles[0] = xiaomi11;
        articleSet.articles[1] = xiaomi11pro;
        articleSet.articles[2] = xiaomiUltra;
    }

    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("新增商品信息");
                    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("退出");
                    flag = false;
                default:
                    System.out.println("你输入的编号有无");
                    break;
            }
        } while (flag);
    }

    //
    //    //排行榜
    //销售排行榜
    public void leaderboard() {
        int count = 0;  // 统计原始数组使用的长度
        for (int i = 0; i < articleSet.articles.length; i++) {
            if (articleSet.articles[i] != null) {
                count++;
            }
        }
        //根据使用的长度临时新建一个数组,这个数组元素存满
        Article[] newTemp = new Article[count];
        // 吧就数组中的元素全部拷贝到新数组中,寻数组装满元素
        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++) {  //让当前元素和他后面的元素对比
                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销售数量 \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() {
        boolean flag = true;
        System.out.println("请输入你要卖出的商品的名字");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        for (int i = 0; i < articleSet.articles.length; i++) {
            if (articleSet.articles[i] != null && articleSet.articles[i].name.equals(name)) {
                System.out.println("请输入要卖出的数量");
                int maichu = scanner.nextInt();
                if (maichu < articleSet.articles[i].amount) {  //卖出数量要小于库存数
                    //新库存等于就库存
                    articleSet.articles[i].amount = articleSet.articles[i].amount - maichu;
                    //新售出 = 就售出 + 卖出数量
                    articleSet.articles[i].number = articleSet.articles[i].number + maichu;
                    flag = true;
                } else {
                    flag = false;
                    System.out.println("库存不够了");
                }
                break; //找到对应的位置,已经完成了修改,就绪的元素直接跳过,终端循环
            } else {
                flag = false;
            }
        }
        if (flag) {
            System.out.println("卖出成功");
        } else {
            System.out.println("卖出失败");
        }
    }

    //查看商品信息
    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 + 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("售出数量");
        int number = scanner.nextInt();

        //把接收到的数据封装到对象中
        Article newArticle = new Article();
        newArticle.setArticle(name, kucun, price, number);
        for (int i = 0; i < articleSet.articles.length; i++) {
            if (articleSet.articles[i] == null){  //从前往后遍历数组,找到第一个没有元素的位置
                articleSet.articles[i] = newArticle;// 找到空位置,吧新商品存入
                break;  //后续的位置不加入
            }
        }
    }

    //删除商品
    public void delete() {
        boolean flag = true;
        System.out.println("输入你要删除的商品编号");
        Scanner scanner = new Scanner(System.in);
        int delNo = scanner.nextInt();

        for (int i = 0; i < articleSet.articles.length; i++) {
            if (delNo == (i + 1) && articleSet.articles[i] != null) {
                int j = i;  // 备份要删除元素的下表
                while (articleSet.articles[j + 1] != null) {
                    articleSet.articles[j] = articleSet.articles[j + 1];
                }
                articleSet.articles[j] = null;
                flag = true;
                j++;

                break;  //  操作完成 直接中断for循环   后续的null元素无需操作
            } else {
                flag = false;
            }
        }
        if (flag) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败!");
        }
    }
}
package Demo01;


public class Demo {
    public static void main(String[] args) {
         ArticleMange articleMange = new ArticleMange();
         articleMange.inital();
         articleMange.startMenu();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值