• Java 实现图书管理系统


    需求

    编写图书借阅程序,可以处理简单的书籍借阅情况,包括借书和还书等。图书类的数据成员包括书名、书号和借书学生等;方法包括借书、还书和显示书籍信息等。学生类的数据成员包括姓名、学号和所借书籍等;方法包括显示学生信息等。将数据保存在数据库中或文件中,采用图形化用户界面,实现图书的入库,借阅和归还。

    代码

    /*
        **说明
            书籍信息采用空格隔开,请勿在书名、书号之中添加空格
    
    
        **运行代码
            首先会在当前目录下检测是否存在books的文件,如果不存在,就创建一个,用来保存书籍信息;
            students 文件同理
    
            所有的操作都是在内存中完成,只有关闭窗口时会将内存写回文件
    
    
    
    */
    
    
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.lang.*;
    
    
    class Book{
        private String name;
        private String no;
        private String student_no;
    
        public void setName(String _name){
            this.name = _name;
        }
    
        public void setNo(String _no){
            this.no = _no;
        }
    
        public void setStudent_no(String _student_no){
            this.student_no = _student_no;
        }
    
        public String getName(){
            return this.name;
        }
    
        public String getNo(){
            return this.no;
        }
    
        public String getStudent_no(){
            return this.student_no;
        }
    
    
        public void Borrow(){
    
        }
    
        public void Return(){
    
        }
    }
    
    class Student{
        private String name;
        private String no;
        private String books_no;
    
        public void setName(String _name){
            this.name = _name;
        }
    
        public void setNo(String _no){
            this.no = _no;
        }
    
        public void setBooks_no(String _books_no){
            this.books_no = _books_no;
        }
    
        public void Show(){
            System.out.println("Student[name= " + this.name + " Student_no= " + this.no + " books=" + this.books_no + "]");
        }
    }
    
    class frame extends JFrame{
        //按钮
        private JButton borrow = new JButton("借书");
        private JButton back = new JButton("还书");
        private JButton append = new JButton("添加书籍");
        private JButton query = new JButton("借阅查询");
        private JButton list = new JButton("书籍查询");
        private ArrayList books = new ArrayList<>();
        private ArrayList students = new ArrayList<>();
        //Jpanel
        private JPanel options = new JPanel();
    
        public frame(){
            //读取文件
                //书籍
            File booksfile = new File("./books");
            if(!booksfile.exists()){
                try{
                    booksfile.createNewFile();
                } catch (Exception e){
                    System.out.println("书籍文件创建失败哦~");
                }
            }
                //学生
            File studentsfile = new File("./students");
            if(!studentsfile.exists()){
                try{
                    studentsfile.createNewFile();
                } catch (Exception e){
                    System.out.println("学生文件创建失败哦~");
                }
            }
    
            //将书籍信息加载到内存
            try {
                BufferedReader reader = new BufferedReader(new FileReader("./books"));
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] info = line.split(" ");
                    Book book = new Book();
                    book.setName(info[0]);
                    book.setNo(info[1]);
                    try{
                        book.setStudent_no(info[2]);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                   
                    books.add(book);
                    System.out.println(line);
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //将学生信息加载到内存
            try {
                BufferedReader reader = new BufferedReader(new FileReader("./students"));
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] info = line.split(" ");
                    Student student = new Student();
                    student.setName(info[0]);
                    student.setNo(info[1]);
                    try{
                        student.setBooks_no(info[2]);
                    } catch(Exception e){
                        System.out.println(e);
                    }
                    students.add(student);
                    System.out.println(line);
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //获取窗口大小
            Toolkit kit = Toolkit.getDefaultToolkit();
            Dimension screenSize = kit.getScreenSize();
            int screenw = screenSize.width;//屏幕的宽
            int screenh = screenSize.height;//屏幕的高
            //设置屏幕的宽和高
            int windowsWedth = 800;
            int windowsHeight = 450;
            //设置剧中
            //坐标x
            int x = (screenw - windowsWedth)/2;
            //坐标y
            int y = (screenh -  windowsHeight)/2;
    
            //Jpanel添加按钮
            options.add(borrow);
            options.add(back);
            options.add(append);
            options.add(query);
            options.add(list);
    
            //绑定借书功能
            borrow.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    String book_name = JOptionPane.showInputDialog("请输入书名:");
                    if(book_name == null){
                        return ;
                    }
                    boolean find = false;
                    Book book = new Book();
                    for(Book tmp : books){
                        if(tmp.getName().equals(book_name)){
                            find = true;
                            book = tmp;
                            break;
                        }
                    }
                    if(find){
                        if(book.getStudent_no() == null){
                            String student_no = JOptionPane.showInputDialog("请输入你的学号:");
                            book.setStudent_no(student_no);
                            JOptionPane.showMessageDialog(null, "借阅成功!");
                        }
                        else{
                            JOptionPane.showMessageDialog(null, "书籍已经被" + book.getStudent_no() + "借走了嗷~");
                        }
                    }
                    else{
                        JOptionPane.showMessageDialog(null, "未发现书籍");
                    }
                }
            });
    
            //还书
            back.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    String student_no = JOptionPane.showInputDialog("请输入学号: ");
                    if(student_no == null){
                        return ;
                    }
                    ArrayList borrowed_books = new ArrayList<>();
                    boolean find = false;
                    for(Book book : books){
                        if(book.getStudent_no() != null){
                            if(book.getStudent_no().equals(student_no)){
                                find = true;
                                borrowed_books.add(book);
                            }
                        }
                    }
                    if(!find){
                        JOptionPane.showMessageDialog(null, "未找到记录");
                    }
                    else{
                        JPanel list = new JPanel();
                        JButton sure_back = new JButton("确认归还");
                        JCheckBox[] checkBoxs = new JCheckBox[borrowed_books.size()];
                        for(int i=0; i";
                            }
                        }
                    }
                    if(!find){
                        JOptionPane.showMessageDialog(null, "未找到记录");
                    }
                    else{
                        JOptionPane.showMessageDialog(null, info);
                    }
                }
            });
    
            //展示书籍
            list.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    String info = "";
                    boolean find = false;
                    for(Book book : books){
                        find = true;
                        info += book.getName() + " " + book.getNo() + "
    "; } if(!find){ JOptionPane.showMessageDialog(null, "未找到记录"); } else{ JOptionPane.showMessageDialog(null, info); } } }); // 程序关闭时将内存写回文件 Thread shutdownHook = new Thread() { @Override public void run() { try{ FileWriter fw = new FileWriter("./books"); for(Book book : books){ fw.write(book.getName() + " " + book.getNo()); if(book.getStudent_no() != null){ fw.write(" " + book.getStudent_no() + "\n"); } else{ fw.write("\n"); } } fw.close(); } catch (Exception e) { System.out.println(e); } } }; // 注册关闭钩子 Runtime.getRuntime().addShutdownHook(shutdownHook); //整活 String makelive="

    CSDN关注

    嗯嗯你说的对"; Font font = new Font("宋体",Font.PLAIN,20); JLabel makeLive = new JLabel(makelive); makeLive.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { try { Desktop.getDesktop().browse(new java.net.URI("https://blog.csdn.net/weixin_61133168?type=blog")); } catch (Exception ex) { ex.printStackTrace(); } } }); makeLive.setFont(font); makeLive.setForeground(Color.RED); makeLive.setHorizontalAlignment(SwingConstants.CENTER); //基础设置 this.setTitle("图书管理"); //this.add(editorPane,BorderLayout.CENTER); this.add(makeLive,BorderLayout.CENTER); this.add(options,BorderLayout.SOUTH); this.setVisible(true); this.setBounds(x,y,windowsWedth,windowsHeight); this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); } } public class Main4{ public static void main(String [] args){ Scanner in = new Scanner(System.in); new frame(); in.close(); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412

    代码分析

    • 采用文件形式来保存信息,每次运行时加载到内存
    • 程序终止时将内存中的信息保存到文件
  • 相关阅读:
    分支对代码性能的影响和优化
    Hadoop-2.7.3完全分布式集群搭建(Centos7系统)
    java压缩pdf体积,图片体积
    创建vue3工程
    高德地图系列(三):vue项目利用高德地图实现地址搜索功能
    JAVA开发(关于写代码与数学)
    反欺诈模型常用开发工具
    读论文:Real-Time Encrypted Traffic Classification via Lightweight Neural Networks
    【Rust日报】2022-09-10 使用动态库加快 Rust 增量编译速度
    docker基础命令
  • 原文地址:https://blog.csdn.net/weixin_61133168/article/details/134475553