2021-04-15

超市后台管理项目

package edu.xcdq.shop;

/**
 *  商品物品类
 */
public class Article {
    //   名字  单价  库存
    public String   name;
    public double   price;
    public int amount;
    public int number;
    public int amout;

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

    public void setArticle(String mingzi , double danjia, int kucun , int yishou){
        name = mingzi;
        price = danjia;
        amount = kucun;
        number = yishou;
    }
}

继续创建菜单

package edu.xcdq.shop;

import com.sun.deploy.security.BadCertificateDialog;

import javax.swing.text.Style;
import java.util.Scanner;



public class ArticleManage {
    ArticleSet  articleSet = new ArticleSet();
    private int i;

    //
    public void initial(){
        Article xiaomi11 = new Article();
        xiaomi11.name = "小米11";
        xiaomi11.number = 30;
        xiaomi11.amount = 0;
        xiaomi11.price = 1999;
        xiaomi11.setArticle("小米11",1999,30,0);

        Article xiaomiPro = new Article();
        xiaomiPro.setArticle("小米11pro",2999,10,0);

        Article xiapmiUltra = new Article();
        xiapmiUltra.setArticle("小米11至尊版",3999,20,0);

        articleSet.articles[0] = xiaomi11;
        articleSet.articles[1] = xiaomiPro;
        articleSet.articles[2] = xiapmiUltra;

    }


    //   启动菜单
    public void startMenu() {
        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 funNo = scanner.nextInt();
        switch (funNo) {
            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("排行榜");
                paihangbang();
                break;
            case 6:
                System.out.println("谢谢,已经退出");
                break;
            default:
                System.out.println("请输入的功能编号有错误");
                break;
        }
        boolean flag = true;
        do {

        } while (flag);

    }

    private 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);
            }
        }
    }
    private void paihangbang() {
        /*

        for () 所有元素全部参与排序
           for () 当前元素之和后面的元素比较
                if ( 当前元素  < 后续元素小)
                往后拍
         */
        //  排序  (冒泡排序)

        for (int i = 0; i < articleSet.articles.length - 1; i++) ;
        for (int j = 0; i < articleSet.articles.length - i - 1; j++) {
            if (articleSet.articles[j] != null && articleSet.articles[j + i] != null) {
                if (articleSet.articles[j].number < articleSet.articles[j + i].number) {
                    Article newTemp = articleSet.articles[j];
                    articleSet.articles[j] = articleSet.articles[j + 1];
                    articleSet.articles[j + 1] = newTemp;
                }
            }
        }
    }



    private void sell() {
        System.out.println("请输入你要售卖的商品名称: ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int i;
        for (i = 0; i < articleSet.articles.length; i++) ;
        if ((articleSet.articles[i].name).equals(name)) {
            System.out.println("请输入你要售卖的数量: ");
            int shoumai = scanner.nextInt();
            if (shoumai < articleSet.articles[i].amout) {
                articleSet.articles[i].amout = articleSet.articles[i].number - shoumai;
                articleSet.articles[i].number = articleSet.articles[i].amount + shoumai;
            }
            System.out.println("售卖成功");


        }
    }


    private void delete() {
        System.out.println("请输入要删除的商品的编号: ");
        Scanner scanner = new Scanner(System.in);
        int
                bianhao = scanner.nextInt();
        boolean flag = true;
        for ( int i =0 ; i  < articleSet.articles.length ;i++) {
            if ( articleSet.articles[i] !=null && (i+1) == bianhao ) {
                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;
            }
        }
    }

    private 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 count = scanner.nextInt();
        System.out.println("请输入已买的数量: ");
        int number = scanner.nextInt();

        Article newArticle = new Article();
        newArticle.setArticle(name ,price ,count ,number );
        for ( int i = 0 ; i < articleSet.articles.length; i++) {
            if ( articleSet.articles[i] == null ) {
                articleSet.articles[i] = newArticle;//  把新建对象放在数组中的第一个位置
                break;  //  后续的空位置直接跳过
            }
        }

    }


}

创建商品集合类

package edu.xcdq.shop;

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

创建测试代码

package edu.xcdq.shop;

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

以上代码仅供参考

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