小米手机(实现查看,新增,删除。销售,销售商品排行榜,退出系统功能)

创建类Article,添加四个属性

public class Article {
    public String name; // 商品名称
    public int amount; // 商品库存数量
    public double price; // 商品价格
    public int number; // 商品售出数量

一个方法展示商品信息

 public void print(int index) {
        System.out.print(index+"\t"+name+"\t"+price+"\t"+amount+"\t"+number);
    }

    public void price(int i) {
    }

创建ArticleSet 类定义一个数组

public class ArticleSet {
    Article[] articles = new Article[50];
}

创建类ArticleManage,创建ArticleSet对象articleSet实现初始化数据的方法,初始化4件商品信息

/*
 * 商品管理类
 * */
public class ArticleManage {
    ArticleSet articleSet = new ArticleSet();
    Scanner input = new Scanner(System.in);
    /*
     * 初始化商品
     * */
    public void initial() {
        Article xiaoMi9 = new Article();
        xiaoMi9.name = "小米9";
        xiaoMi9.price = 2799 ;
        xiaoMi9.amount = 60 ;
        xiaoMi9.number = 0;
        Article xiaoMiMIX3 = new Article();
        xiaoMiMIX3.name = "小米8";
        xiaoMiMIX3.price = 2049 ;
        xiaoMiMIX3.amount = 40;
        xiaoMiMIX3.number = 0;
        Article redMiNote7Pro = new Article();
        redMiNote7Pro.name = "Redmi 7";
        redMiNote7Pro.price = 699;
        redMiNote7Pro.amount = 80;
        redMiNote7Pro.number = 0;
        Article xiaoMiPlay = new Article();
        xiaoMiPlay.name = "小米6X";
        xiaoMiPlay.price = 749;
        xiaoMiPlay.amount = 100 ;
        xiaoMiPlay.number = 0;
        articleSet.articles[0] = xiaoMi9;
        articleSet.articles[0] = xiaoMiMIX3;
        articleSet.articles[0] = redMiNote7Pro;
        articleSet.articles[0] = xiaoMiPlay;
    }

在ArticleManage类中编写菜单显示和切换的startMenu 方法。

 /*
     * 菜单切换
     * */
    public void startMenu() {
        boolean flag = true ; // 是否操作
        do {
            System.out.print("欢迎使用前程商城后台管理系统");
            System.out.print("------------------------------------------------------");
            System.out.print("1. 查看商品信息");
            System.out.print("2. 新增商品信息");
            System.out.print("3. 删除商品信息");
            System.out.print("4. 卖出商品");
            System.out.print("5. 商品销售排行榜");
            System.out.print("6. 退出");
            System.out.print("------------------------------------------------------");
            System.out.print("请选择要执行的操作: ");
            int choice = input.nextInt();
            switch (choice) {
                case 1:
                    System.out.print("查看商品信息");
                    break;
                    case 2:
                    System.out.print("新增商品信息");
                    break;
                case 3:
                    System.out.print("删除商品信息");
                    break;
                case 4:
                    System.out.print("卖出商品");
                    break;
                case 5:
                    System.out.print("商品销售排行榜");
                    break;
                case 6:
                    System.out.print("谢谢使用");
                    flag = false;
                    break;
                default:
                    System.out.print("输入不符合要求请重新选择");
                    break;
            }
        }while (flag);
    }

测试类代码

/*
 * 测试类
 * */
public class Demo {
    public static void main(String[] args) {
        ArticleManage articleManage = new ArticleManage();
        articleManage.initial();
        articleManage.startMenu();
    }
}

查看商品信息,在ArticleManage类中编写查看商品信息的search方法。

/*
    * 查看商品信息
    * */
    public void search() {
        System.out.print("编号\t名称\t价格\t库存\t售出");
        for (int i = 0; i < articleSet.articles.length; i++) {
            if (articleSet.articles[i] != null ){
                articleSet.articles[i].price(i+1);
            }
        }
    }

在ArticleManage类中编写添加商品的add方法

/*
     * 新增商品
     * */
    public void add() {
        System.out.print("请输入商品名称: ");
        String name = input.next();
        System.out.print("请输入价格: ");
        int price = input.nextInt();;
        System.out.print("请输入库存: ");
        int amount = input.nextInt();
        Article article = new Article();
        article.name = name;
        article.price = price;
        article.amount = amount;
        article.number = 0;
    }

在ArticleManage类中编写删除商品信息的delete方法

/*
    * 删除商品
    * */
    public void delete(){
        System.out.print("请输入商品编号: ");
        boolean flag = true; // 是否删除成功
        int card = input.nextInt();
        for (int i = 0; i < articleSet.articles.length; i ++) {
            if (articleSet.articles[i] != null&&(i+1)==card){
                int j=i;
                while (articleSet.articles[j+1] !=null) {
                    articleSet.articles[j] = articleSet.articles[j+1];
                    j++ ;
                }
                articleSet.articles[j] = null;
                flag = true;
                break;
            }else {
                flag = false;
            }
        }
        if (flag) {
            System.out.print("删除成功! ");
        }else {
            System.out.print("删除失败,请重新操作! ");
        }
    }

在ArticleManage类中编写卖出商品的sell方法

    /*
    * 销售业务
    * */
    public void sell(){
        System.out.print("请输入你要卖出的商品名称: ");
        String name = input.next();
        boolean flag = true ; //是否卖出成功
        for (int i = 0 ; i < articleSet.articles.length; i++) {
            if (articleSet.articles[i] != null
            && articleSet.articles[i].name.equals(name)){
                System.out.print("请输入您要卖出的数量");
                int number = input.nextInt();
                if (number <= articleSet.articles[i].amount) {
                    articleSet.articles[i].number = articleSet.articles[i].number+number;
                    articleSet.articles[i].amount = articleSet.articles[i].amount-number;
                    flag = true;
                }else {
                    System.out.print("商品数量不够,请抓紧进货! ");
                    flag = false;
                }
                break;
            }else {
                flag = false;
            }
        }
        if (flag){
            System.out.print("卖出商品成功!");
        }else {
            System.out.print("卖出商品失败!");
        }
    }

在ArticleManage类中编写商品销售排行榜的leaderboard方法

/*
    * 商品销售排行榜
    * */
    public void leaderboard(){
        Article[] articles = new Article[50];
        for (int i = 0; i < articles.length; i++) {
            if (articleSet.articles[i] != null) {
                articles[i] = articleSet.articles[i];
            }
        }
        for (int i = 0; i < articles.length-1; i ++ ){
            for (int j = 0; j < articles.length-i-1; j++){
                if (articles[j+1] !=null){
                    if (articles[j].number < articles[j+1].number){
                        Article tempArticle = articles[j];
                        articles[j] = articles[j+1];
                        articles[j+1] = tempArticle;
                    }
                }
            }
        }
        System.out.print("*********************************");
        System.out.print("名次\t销售数量\t商品名称");
        for (int i = 0; i < articles.length; i++){
            if (articles[i] != null){
                System.out.print(i+1+"\t"+articles[i].number+"\t"+articles[i].name);
            }
        }
    }
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值