2021-04-16

public class ArticleManage {

ArticleSetarticleSet =new ArticleSet();

    Scannerinput;

    public ArticleManage() {

this.input =new Scanner(System.in);

    }

public void initial() {

Article xiaoMi9 =new Article();

        xiaoMi9.name ="小米9";

        xiaoMi9.price =2799.0D;

        xiaoMi9.amount =60;

        xiaoMi9.number =0;

        Article xiaoMiMIX3 =new Article();

        xiaoMiMIX3.name ="小米MIX3";

        xiaoMiMIX3.price =2049.0D;

        xiaoMiMIX3.amount =40;

        xiaoMiMIX3.number =0;

        Article redMiNote7Pro =new Article();

        redMiNote7Pro.name ="红米note7Pro";

        redMiNote7Pro.price =699.0D;

        redMiNote7Pro.amount =80;

        redMiNote7Pro.number =0;

        Article xiaoMiPlay =new Article();

        xiaoMiPlay.name ="小米Play";

        xiaoMiPlay.price =749.0D;

        xiaoMiPlay.amount =100;

        xiaoMiPlay.number =0;

        this.articleSet.articles[0] = xiaoMi9;

        this.articleSet.articles[1] = xiaoMiMIX3;

        this.articleSet.articles[2] = redMiNote7Pro;

        this.articleSet.articles[3] = xiaoMiPlay;

    }

public void startMenu() {

boolean flag =true;

        do {

System.out.println("欢迎使用超市管理系统");

            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("请选择要执行的操作: ");

            int choice =this.input.nextInt();

            switch(choice) {

case 1:

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

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("谢 谢 使 用!");

break;

            default:

System.out.println("输入不符合要求请重新选择!");

            }

}while(flag);

    }

public void search() {

System.out.println("编号\t 名称\t 价格\t 库存\t 售出");

        for(int i =0; i

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

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

            }

}

}

public void add() {

System.out.println("请输入商品名称:");

        String name =this.input.next();

        System.out.println("请输入价格:");

        int price =this.input.nextInt();

        System.out.println("请输入库存");

        int amount =this.input.nextInt();

        Article article =new Article();

        article.name = name;

        article.price = (double)price;

        article.amount = amount;

        article.number =0;

        for(int i =0; i

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

this.articleSet.articles[i] = article;

            }

}

}

public void delete() {

System.out.println("请输入商品编号:");

        boolean flag =true;

        int card =this.input.nextInt();

        for(int i =0; i

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

int j;

                for(j = i; this.articleSet.articles[j + i] !=null; ++j) {

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

                }

this.articleSet.articles[j] =null;

                flag =true;

break;

            }

flag =false;

        }

if (flag) {

System.out.println("删除成功!");

        }else {

System.out.println("删除失败,请重新操作!");

        }

}

public void sell() {

System.out.println("请输入你要卖出的商品名称:");

        String name =this.input.next();

        boolean flag =true;

        for(int i =0; i

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

System.out.println("请输入你要卖出的数量");

                int number =this.input.nextInt();

                if (number <=this.articleSet.articles[i].amount) {

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

                    this.articleSet.articles[i].amount -= number;

                    flag =true;

                }else {

System.out.println("商品数量不够,请抓紧进货");

                    flag =true;

                }

break;

            }

flag =false;

        }

if (flag) {

System.out.println("卖出商品成功!");

        }else {

System.out.println("卖出商品失败");

        }

}

public void leaderboard() {

Article[] articles =new Article[50];

        int i;

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

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

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

            }

}

for(i =0; i < articles.length -1; ++i) {

for(int j =0; j < articles.length - i -1; ++j) {

if (articles[j +1] !=null && articles[j].number < articles[j +1].number) {

Article tempArticle = articles[j];

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

                    articles[j +1] = tempArticle;

                }

}

}

System.out.println("***************************");

        System.out.println("名次\t 销售数量\t 商品名称");

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

if (articles[i] !=null) {

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

            }

}

}

}

你可能感兴趣的:(2021-04-16)