• 集合


    A: 创建一个接口BookManager

    B:创建一个实现类:BookManagerImpl{

             成员变量:List<Map> books;

             public boolean addBooks(){

                       创建一个Map ;scanner录入:书本名称  书本编号   书本单价 库存

                       遍历books判断这个书本编号是否已经添加过,如果已经添加过返回false

                       否则:封装Map放入books列表中

    }

    Public int search(String 书本编号){

             根据编号查找书本信息

    }

    Public Boolean  instor(编号,入库数量){

             //入库

             查找进行入库,如果没有这个商品返回false

             否则在原有数量上+新的数量

    }

    Public Boolean outstore(编号,出库数量){

             //出库

    }

    }

    C:书写一个main方法进行测试

    接口:

    1 package Text;
    2 
    3 public interface BookManager {
    4     boolean addBooks();
    5     int search(int num);
    6     boolean instor(int num,int innum);
    7     boolean outstor(int num,int outnum);
    8 }

    实现类:

      1 package Text;
      2 
      3 import java.util.*;
      4 
      5 public class BookManagerlmpl implements BookManager {
      6     List<Map> books=new ArrayList<>();
      7     Scanner scanner = new Scanner(System.in);
      8     @Override
      9     public boolean addBooks() {
     10          System.out.println("请输入添加的书本编号");
     11          int booknum = scanner.nextInt();
     12          int search = search(booknum);
     13          if(search==-1){
     14             System.out.println("请输入添加的书本名称");
     15             String bookname = scanner.next();
     16             System.out.println("请输入添加的书本价格");
     17             double bookprice = scanner.nextInt();
     18             System.out.println("请输入书本库存");
     19             int count = scanner.nextInt();
     20             Map<String,Object> map = new HashMap<>();
     21             map.put("bookname",bookname);
     22             map.put("bookprice",bookprice);
     23             map.put("booknum",booknum);
     24             map.put("bookcount",count);
     25             books.add(map);
     26         }else{
     27              System.out.println("该编号已存在");
     28         }
     29         return true;
     30     }
     31 
     32     @Override
     33     public int search(int num) {
     34         int index = -1;
     35         for (int i = 0; i <books.size() ; i++) {
     36             if(books.get(i).get("booknum").equals(num)){
     37                 index = i;
     38                 break;
     39             }
     40         }
     41         return index;
     42     }
     43 
     44     @Override
     45     public boolean instor(int num, int innum) {
     46         int index= search(num);
     47         if(index!=-1){
     48             int count=(int)books.get(index).get("bookcount")+innum;
     49             books.get(index).put("bookcount",count);
     50             return true;
     51         }else{
     52             return false;
     53         }
     54 
     55     }
     56 
     57     @Override
     58     public boolean outstor(int num, int outnum) {
     59         int index= search(num);
     60         if(index!=-1){
     61             int count=(Integer) books.get(index).get("bookcount")-outnum;
     62             books.get(index).put("bookcount",count);
     63             return true;
     64         }else{
     65             return false;
     66         }
     67     }
     68 
     69     public static void main(String[] args) {
     70         BookManagerlmpl managerlmpl = new BookManagerlmpl();
     71         Scanner scanner = new Scanner(System.in);
     72         while(true) {
     73             System.out.println("请选择操作  1.添加  2.查询  3.入库  4.出库  5.退出");
     74             int choose = scanner.nextInt();
     75             if(choose==1){
     76                 managerlmpl.addBooks();
     77                 System.out.println(managerlmpl.books);
     78             }else if( choose==2){
     79                 System.out.println("请输入要查询的书本编号");
     80                 int booknum = scanner.nextInt();
     81                 int search = managerlmpl.search(booknum);
     82                 if(search!=-1){
     83                     System.out.println("查询成功");
     84                     System.out.println(managerlmpl.books.get(search));
     85                 }else{
     86                     System.out.println("查询失败");
     87                 }
     88             }else if(choose==3){
     89                 System.out.println("请输入入库的书本编号");
     90                 int booknum = scanner.nextInt();
     91                 System.out.println("请输入要入库的书本数量");
     92                 int innum = scanner.nextInt();
     93                 boolean instor = managerlmpl.instor(booknum, innum);
     94                 if(instor){
     95                     System.out.println("入库成功");
     96                     int search = managerlmpl.search(booknum);
     97                     System.out.println(managerlmpl.books.get(search));
     98                 }else{
     99                     System.out.println("入库失败");
    100                 }
    101 
    102             }else if(choose==4){
    103                 System.out.println("请输入出库的书本编号");
    104                 int booknum = scanner.nextInt();
    105                 System.out.println("请输入要出库的书本数量");
    106                 int outnum = scanner.nextInt();
    107                 boolean outstor = managerlmpl.outstor(booknum, outnum);
    108                 if(outstor){
    109                     System.out.println("出库成功");
    110                     int search = managerlmpl.search(booknum);
    111                     System.out.println(managerlmpl.books.get(search));
    112                 }else{
    113                     System.out.println("出库失败");
    114                 }
    115             }else if(choose==5){
    116                 System.out.println("已退出");
    117                 break;
    118             }
    119         }
    120     }
    121 }
  • 相关阅读:
    mysql笔记
    ssh学习笔记
    oracle数据向历史表数据迁移————procedure
    关于避免模糊查询索引时效问题
    css圣杯布局和双飞翼布局篇
    什么是hasLayout
    高度自适应的水平垂直居中布局
    MAC的GIF动图录屏软件LICECAP
    sublime text 快捷键shortcuts
    为表格合并边框的样式
  • 原文地址:https://www.cnblogs.com/zhanhaoze/p/13221102.html
Copyright © 2020-2023  润新知