Java利用数组实现商品管理系统

利用java实现商品管理系统,主要利用二维动态数组,二维数组里面存空间为4的一维数组的地址值,然后添加商品时扩容,删除商品时候缩容

具有的功能1.添加商品信息2. 查看商品信息3. 最贵商品信息4. 商品进行排序 5.删除商品信息 6.退出管理系统

主要的成员变量有

//静态成员方法属于类的大家都可以用
static Scanner sc = new Scanner(System.in);
//定义一个静态二维数组存储商品
//这样每个方法更改后值都是会变的 不是静态则不可
static String[][] arr = new String[0][4];
//记录索引
static int index = 0;

1.打印表头信息

  //打印表头信息
      private static void print(){
    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("---------------------------------------");
}

2. 账号密码登录

//账号密码进行登录
private static void user(){
    int count = 3;
    while(true){
        Scanner s = new Scanner(System.in);
        System.out.println("尊敬的管理员");
        System.out.println("请输入你的账号:");
        String x = s.nextLine();
        System.out.println("请输入你的密码:");
        String y = s.nextLine();
        if (x.equals("蔡申友") && y.equals("521hx1314")) {
            break;
        }
        else {
            count--;
            System.out.println("你输入的账号密码不匹配\t你还有"+count+"次机会");
            if (count == 0){
                System.exit(0);
            }
        }
    }
}

3.扩容功能

//对二维数组进行扩容
private static void extend(){
    String[][] newArr = new String[arr.length + 1][4];
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j  < arr[i].length; j++) {
           //把老数组的数据都给扩容后的数组
            newArr[i][j] = arr[i][j];
        }
    }
    //扩容完成后在还给老数组
    arr = newArr;
}

4.缩容功能

//对二维数组进行缩容
private static void shorten(){
    String[][] newArr = new String[arr.length - 1][4];
    //这里用arr.length就越界了
    for (int i = 0; i < newArr.length; i++) {
        for (int j = 0; j  < arr[i].length; j++) {
            //把老数组的数据都给扩容后的数组
            newArr[i][j] = arr[i][j];
        }
    }
    //缩容完成后在还给老数组
    arr = newArr;
}

5.商品为空的操作

 //如果不添加就选其他功能
private static void breakdown(){
    while (true) {
        System.out.println("无商品信息,请先添加商品信息");
        System.out.println("请选择是否添加商品信息:");
        String choose = sc.next();
        switch (choose) {
            case "是":
                add();
                return;
            case "否":
                System.out.println("请你重新选择:");
                return;
            default :
                System.out.println("你的选择有误\t请用是否进行回答");
        }
    }
}

6.功能选择 

public static void main(String[] args) {
                user();
    while (true) {
        print();
        System.out.println("请选择你要进行的业务:");
        String choose = sc.next();
        switch (choose) {
            case "1":
                System.out.println("-----添加商品信息-----");
                add();
                break;
            case "2":
                System.out.println("-----查看商品信息-----");
                query();
                break;
            case "3":
                System.out.println("-----最贵商品信息-----");
                maxGoods();
                break;
            case "4":
                System.out.println("-----商品进行排序-----");
                sort();
                break;
            case "5":
                System.out.println("-----删除商品信息-----");
                delete();
                break;
            case "6":
                System.out.println("感谢您的使用!");
                System.exit(0);
            default:
                System.out.println("没有这个选项,请您重新选择");
                break;
        }
    }
}

 7.添加商品

 //添加商品
      private static void add() {
          extend();
          System.out.println("请输入商品的编号:");
          String id = sc.next();
          arr[index][0] = id;
          System.out.println("请输入商品的名称:");
          String name = sc.next();
          arr[index][1] = name;
          System.out.println("请输入商品的数量:");
          String number = sc.next();
          arr[index][2] = number;
          System.out.println("请输入商品的单价:");
          String price = sc.next();
          arr[index][3] = price;
          //下一个就是存后面一个索引
          index++;
}

8. 删除商品

//按商品编号删除商品
private static void delete() {
    //先判断是否为空
    if (arr.length != 0) {
        System.out.println("请输入你要删除的id:");
        String deleteId = sc.next();
        for (int i = 0; i < arr.length; i++) {
            if (deleteId.equals(arr[i][0])){
                for (int j = i + 1; j < arr.length; j++) {
                    arr[j - 1] = arr[j];
                }
                shorten();
                System.out.println("你的商品删除成功!");
                index--;
                //删除成功就跳出方法
                return;
            }
        }
        //删除不成功就执行这一步
        System.out.println("对不起,你的id不存在无法删除!");
    } else {
        //空的话提醒先添加
        breakdown();
    }
}

9.查看商品 

//查询所有商品信息
private static void query() {
    //先判断是否为空
    if (arr.length != 0) {
        System.out.println("所有商品列表为:");
        for (int i = 0; i < arr.length; i++) {
            System.out.println("编号\t名称\t数量\t单价");
            System.out.println(arr[i][0] + "\t" + arr[i][1]  + "\t" + arr[i][2]  + "\t" + arr[i][3] + "元" );
        }
    } else {
        //空的话提醒你添加
        breakdown();
    }
}

10.按价格排序商品 (这里用了冒泡排序)

//商品价格由低到高排序
private static void sort() {
    //先判断是否为空
    if (arr.length != 0) {
        //冒泡排序进行交换
        //定义外层循环
        for (int i = 0; i < arr.length - 1; i++) {
            //定义内层循环
            //内循环就是判断这个索引所有比他大的都要和他比
            //因为最后一个不需要比所以-1
            for (int j = 0; j < arr.length - 1 - i; j++) {
                //下面的三行代码用于交换俩个元素
                //包装类拆箱
                int a = Integer.parseInt(arr[j][3]);
                int b = Integer.parseInt(arr[j + 1][3]);
                if (a > b) {
                    String[] temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println("商品已经为你排序完成,请您查看一下吧!");
    }else {
        //为空提示你先添加
        breakdown();
    }
}

 11.输出最贵商品

//输出最贵的商品
private static void maxGoods() {
    //先判断是否为空
    if (arr.length != 0) {
        //拆箱
        int max = Integer.parseInt(arr[0][3]);
        //记录最大值的商品是哪一个
        //默认是第一个因为max默认是0
        int count = 0;
        for (int i = 1; i < arr.length; i++) {
            int newNumber = Integer.parseInt(arr[i][3]);
            if (max < newNumber){
                max = newNumber;
                //如果哪个数比max大哪个数就被记录
                count = i;
            }
        }
        System.out.println("编号\t名称\t数量\t单价");
        System.out.println(arr[count][0] + "\t" + arr[count][1]  + "\t" + arr[count][2]  + "\t" + arr[count][3] + "元");
    } else {
        //空的话提示你添加商品
        breakdown();
    }
}

12.全部内容 

package HomeWork;


import java.util.Scanner;

public class Manage {
    //静态成员方法属于类的大家都可以用
    static Scanner sc = new Scanner(System.in);
    //定义一个静态二维数组存储商品
    //这样每个方法更改后值都是会变的 不是静态则不可
    static String[][] arr = new String[0][4];
    //记录索引
    static int index = 0;
          public static void main(String[] args) {
                          user();
              while (true) {
                  print();
                  System.out.println("请选择你要进行的业务:");
                  String choose = sc.next();
                  switch (choose) {
                      case "1":
                          System.out.println("-----添加商品信息-----");
                          add();
                          break;
                      case "2":
                          System.out.println("-----查看商品信息-----");
                          query();
                          break;
                      case "3":
                          System.out.println("-----最贵商品信息-----");
                          maxGoods();
                          break;
                      case "4":
                          System.out.println("-----商品进行排序-----");
                          sort();
                          break;
                      case "5":
                          System.out.println("-----删除商品信息-----");
                          delete();
                          break;
                      case "6":
                          System.out.println("感谢您的使用!");
                          System.exit(0);
                      default:
                          System.out.println("没有这个选项,请您重新选择");
                          break;
                  }
              }
          }
          //账号密码进行登录
          private static void user(){
              int count = 3;
              while(true){
                  Scanner s = new Scanner(System.in);
                  System.out.println("尊敬的管理员");
                  System.out.println("请输入你的账号:");
                  String x = s.nextLine();
                  System.out.println("请输入你的密码:");
                  String y = s.nextLine();
                  if (x.equals("蔡申友") && y.equals("521hx1314")) {
                      break;
                  }
                  else {
                      count--;
                      System.out.println("你输入的账号密码不匹配\t你还有"+count+"次机会");
                      if (count == 0){
                          System.exit(0);
                      }
                  }
              }
          }

          //打印表头信息
          private static void print(){
        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("---------------------------------------");
    }

          //添加商品
          private static void add() {
              extend();
              System.out.println("请输入商品的编号:");
              String id = sc.next();
              arr[index][0] = id;
              System.out.println("请输入商品的名称:");
              String name = sc.next();
              arr[index][1] = name;
              System.out.println("请输入商品的数量:");
              String number = sc.next();
              arr[index][2] = number;
              System.out.println("请输入商品的单价:");
              String price = sc.next();
              arr[index][3] = price;
              //下一个就是存后面一个索引
              index++;
    }

          //输出最贵的商品
          private static void maxGoods() {
              //先判断是否为空
              if (arr.length != 0) {
                  //拆箱
                  int max = Integer.parseInt(arr[0][3]);
                  //记录最大值的商品是哪一个
                  //默认是第一个因为max默认是0
                  int count = 0;
                  for (int i = 1; i < arr.length; i++) {
                      int newNumber = Integer.parseInt(arr[i][3]);
                      if (max < newNumber){
                          max = newNumber;
                          //如果哪个数比max大哪个数就被记录
                          count = i;
                      }
                  }
                  System.out.println("编号\t名称\t数量\t单价");
                  System.out.println(arr[count][0] + "\t" + arr[count][1]  + "\t" + arr[count][2]  + "\t" + arr[count][3] + "元");
              } else {
                  //空的话提示你添加商品
                  breakdown();
              }
          }

          //查询所有商品信息
          private static void query() {
              //先判断是否为空
              if (arr.length != 0) {
                  System.out.println("所有商品列表为:");
                  for (int i = 0; i < arr.length; i++) {
                      System.out.println("编号\t名称\t数量\t单价");
                      System.out.println(arr[i][0] + "\t" + arr[i][1]  + "\t" + arr[i][2]  + "\t" + arr[i][3] + "元" );
                  }
              } else {
                  //空的话提醒你添加
                  breakdown();
              }
          }

          //按商品编号删除商品
          private static void delete() {
              //先判断是否为空
              if (arr.length != 0) {
                  System.out.println("请输入你要删除的id:");
                  String deleteId = sc.next();
                  for (int i = 0; i < arr.length; i++) {
                      if (deleteId.equals(arr[i][0])){
                          for (int j = i + 1; j < arr.length; j++) {
                              arr[j - 1] = arr[j];
                          }
                          shorten();
                          System.out.println("你的商品删除成功!");
                          index--;
                          //删除成功就跳出方法
                          return;
                      }
                  }
                  //删除不成功就执行这一步
                  System.out.println("对不起,你的id不存在无法删除!");
              } else {
                  //空的话提醒先添加
                  breakdown();
              }
          }

          //商品价格由低到高排序
          private static void sort() {
              //先判断是否为空
              if (arr.length != 0) {
                  //冒泡排序进行交换
                  //定义外层循环
                  for (int i = 0; i < arr.length - 1; i++) {
                      //定义内层循环
                      //内循环就是判断这个索引所有比他大的都要和他比
                      //因为最后一个不需要比所以-1
                      for (int j = 0; j < arr.length - 1 - i; j++) {
                          //下面的三行代码用于交换俩个元素
                          //包装类拆箱
                          int a = Integer.parseInt(arr[j][3]);
                          int b = Integer.parseInt(arr[j + 1][3]);
                          if (a > b) {
                              String[] temp = arr[j];
                              arr[j] = arr[j + 1];
                              arr[j + 1] = temp;
                          }
                      }
                  }
                  System.out.println("商品已经为你排序完成,请您查看一下吧!");
              }else {
                  //为空提示你先添加
                  breakdown();
              }
          }

          //对二维数组进行扩容
          private static void extend(){
              String[][] newArr = new String[arr.length + 1][4];
              for (int i = 0; i < arr.length; i++) {
                  for (int j = 0; j  < arr[i].length; j++) {
                     //把老数组的数据都给扩容后的数组
                      newArr[i][j] = arr[i][j];
                  }
              }
              //扩容完成后在还给老数组
              arr = newArr;
          }

          //对二维数组进行缩容
          private static void shorten(){
              String[][] newArr = new String[arr.length - 1][4];
              //这里用arr.length就越界了
              for (int i = 0; i < newArr.length; i++) {
                  for (int j = 0; j  < arr[i].length; j++) {
                      //把老数组的数据都给扩容后的数组
                      newArr[i][j] = arr[i][j];
                  }
              }
              //缩容完成后在还给老数组
              arr = newArr;
          }

          //如果不添加就选其他功能
         private static void breakdown(){
             while (true) {
                 System.out.println("无商品信息,请先添加商品信息");
                 System.out.println("请选择是否添加商品信息:");
                 String choose = sc.next();
                 switch (choose) {
                     case "是":
                         add();
                         return;
                     case "否":
                         System.out.println("请你重新选择:");
                         return;
                     default :
                         System.out.println("你的选择有误\t请用是否进行回答");
                 }
             }
         }
}
  • 14
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风止￴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值