需求

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

代码

/***说明书籍信息采用空格隔开,请勿在书名、书号之中添加空格**运行代码首先会在当前目录下检测是否存在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();//Jpanelprivate 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;//设置剧中//坐标xint x = (screenw - windowsWedth)/2;//坐标yint 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<borrowed_books.size(); i++){checkBoxs[i] = new JCheckBox(borrowed_books.get(i).getName());list.add(checkBoxs[i]);}JFrame listBooks = new JFrame("还书");//确认归还sure_back.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){boolean find = false;for(int i=0; i<borrowed_books.size(); i++){if(checkBoxs[i].isSelected()){find = true;for(Book tmp : books){if(tmp.getNo().equals(borrowed_books.get(i).getNo())){tmp.setStudent_no(null);}}}}if(find){JOptionPane.showMessageDialog(null,"还书完成");listBooks.dispose();}else{JOptionPane.showMessageDialog(null,"请选择要归还的图书");}}});//JFrame listBooks = new JFrame("还书");listBooks.add(list);listBooks.add(sure_back,BorderLayout.SOUTH);listBooks.setVisible(true);listBooks.setBounds(x+200,y+50,300,400);//JOptionPane.showMessageDialog(null, info);}}});//添加书籍append.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String book_name = JOptionPane.showInputDialog("请输入书名:");if(book_name != null){if(book_name.equals("")){JOptionPane.showMessageDialog(null, "请输入书名!");return ;}else{String book_no = JOptionPane.showInputDialog("请输入书ID: ");if(book_no != null){if(book_no.equals("")){JOptionPane.showMessageDialog(null, "请输入书ID!");return ;}Book book = new Book();book.setNo(book_no);book.setName(book_name);books.add(book);JOptionPane.showMessageDialog(null, "添加成功");}}} }});//借阅查询query.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String student_no = JOptionPane.showInputDialog("请输入学号: ");String info = "";boolean find = false;for(Book book : books){if(book.getStudent_no() != null){if(book.getStudent_no().equals(student_no)){find = true;info += book.getName() + " " + book.getNo() + "
";}}}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() {@Overridepublic 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() {@Overridepublic 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();}}

代码分析

  • 采用文件形式来保存信息,每次运行时加载到内存
  • 程序终止时将内存中的信息保存到文件