Java library management (visualization, file)

catalogue

requirement

design sketch

Brief explanation of function realization

Some methods

code

requirement

At the end of the year, I suddenly remembered that I hadn't published an article this semester. Think about sending something. I saw the demand for a java applet in a group yesterday. I have nothing to do today. I also wrote a little. Achieved something I felt necessary.

● write a Java application simulating Bookstore inventory management, which is required to realize the following functions:

1. Prepare Book profile management procedures to manage book name, book number, publishing house, unit price, inventory and other information:

2. Compile book information input method

3. Write the method of deleting book information

I won't write the following requirements (in fact, I'm too lazy to realize it, which makes no sense)

design sketch

 

 

 

Brief explanation of function realization

  1. The first is the information input method during entry and deletion. I have the subjective idea of inputting low directly from the terminal console (maybe my idea is childish). What I use is to generate a new small dialog form. Then we come to the first point we want to say, that is, what we want is to close the dialogue window after entering the information and clicking OK. Then continue the display operation on the JFrame. Note that exit cannot be used to close the window_ ON_ Close, it will close together with the main window.
  2. This is the deletion operation. The entry operation is relatively simple. You only need to write directly. But deleting by number is not so easy. My idea is very simple and violent, that is to use a new text file to save the information that has not been deleted. Then use the file to overwrite the file that stores the book information. This simple and crude file saving method has been used by me from c to java. Next, let's talk about writing the temporary file (the new text file mentioned above). When we read the input number. We will search the documents of book information. It is recommended to use BufferedReader, which can read line by line. Because the format of the information we store is fixed (book title + "" + number). Therefore, it is natural to use StringTokenizer. After splitting the information into strings, you can get the number of each book. Use a List collection to save the book information, and then write it to the temporary file. To judge whether the number is the same as the input, you can't directly use = =, and use the equals method of String class (I didn't pay attention to it when I wrote it, but I found it after several mistakes)
  3. In JTextArea, you can use "\ r\n" to actively wrap lines; File remember to close the input and output streams
  4. Well, because I'm not too neat. Therefore, classification is basically unrealistic for me... As a result, I often implement all functions in one or several methods of a class...

Some methods

setViewportView();  //JScrollPane method for adding components and interfaces (not add!!!)
FileWriter out = new FileWriter(filename,boolean); //For the following parameters, true means append and false means no append.
List<String>name = new ArrayList<String>(); //String collection, common method, name Size() collection size, name Get (index) gets the ordinal index element of the collection.

code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;


public class BookSystem extends JFrame{

    private JPanel ShowArea;
    private JPanel MenuArea;


    public void MyFrame(){
        JFrame jf = new JFrame("Library management system");
        jf.setBounds(200,200,400,400);
        jf.setLayout(new BorderLayout());
        JPanel ShowArea = new JPanel();
        JPanel MenuArea = new JPanel();



        //Display area
        JTextArea area = new JTextArea();
        area.setLineWrap(true);
        area.setEnabled(true);
        area.setSize(350,200);
        area.setRows(15);
        ShowArea.add(area);


        JScrollPane js = new JScrollPane();
        js.setBounds(20,20,400,200);
        js.setViewportView(ShowArea);
        jf.add(js,BorderLayout.NORTH);

        //Functional area
        JButton jb1 = new JButton("Input");
        JButton jb2 = new JButton("delete");
        JButton jb3 = new JButton("Refresh");
        //monitor
        //Input
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame Mess = new JFrame("Input information");
                Mess.setBounds(250,250,300,150);
                Mess.setLayout(new BorderLayout());
                JPanel jp1 = new JPanel();
                JPanel jp2 = new JPanel();
                JPanel jp3 = new JPanel();
                jp1.add(new JLabel("title"));
                jp2.add(new JLabel("number"));
                JTextField name = new JTextField();
                name.setColumns(15);
                jp1.add(name);
                JTextField no = new JTextField();
                no.setColumns(15);
                jp2.add(no);
                JButton jb = new JButton("determine");
                jp3.add(jb);
                //Generate information window
                jb.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String name1 = name.getText();
                        String no1 = no.getText();
                        try {
                            FileWriter out = new FileWriter("E:\\Library management\\src\\book.txt",true);
                            BufferedWriter bufferedWriter = new BufferedWriter(out);
                            bufferedWriter.write(name1+" "+no1);
                            bufferedWriter.newLine();
                            bufferedWriter.flush();
                            bufferedWriter.close();
                            Mess.dispose();
                        }
                        catch (IOException a){
                            System.out.println(a.toString());
                        }
                    }
                });

                Mess.add(jp1,BorderLayout.NORTH);
                Mess.add(jp2,BorderLayout.CENTER);
                Mess.add(jp3,BorderLayout.SOUTH);
                Mess.setVisible(true);
                Mess.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            }
        });
        //Delete operation
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame jf = new JFrame("delete");
                jf.setBounds(250,250,300,150);
                jf.setLayout(new BorderLayout());
                JPanel jp = new JPanel();
                JPanel jp2 = new JPanel();
                JTextField no = new JTextField();
                no.setColumns(15);
                JButton jb = new JButton("determine");
                jp.add(new JLabel("number"));
                jp.add(no);
                jp2.add(jb);

                //determine
                jb.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String num = no.getText();
                        try{
                            FileReader in = new FileReader("E:\\Library management\\src\\book.txt");
                            FileWriter out = new FileWriter("E:\\Library management\\src\\temp.txt",false);
                            BufferedReader bufferedReader = new BufferedReader(in);
                            BufferedWriter bufferedWriter = new BufferedWriter(out);
                            String str;
                            List<String>name = new ArrayList<String>();
                            while((str=bufferedReader.readLine())!=null){
                                name.add(str);
                            }
                            for(int i=0;i<name.size();i++){
                                StringTokenizer fx = new StringTokenizer(name.get(i)," ");
                                String temp_num="";
                                while(fx.hasMoreTokens()){
                                    temp_num = fx.nextToken();
                                }
                                if(!num.equals(temp_num)){
                                    bufferedWriter.write(name.get(i));
                                    bufferedWriter.newLine();
                                }
                            }
                            bufferedReader.close();
                            bufferedWriter.flush();
                            bufferedWriter.close();

                            //Save deleted data
                            FileReader in_ = new FileReader("E:\\Library management\\src\\temp.txt");
                            FileWriter out_ = new FileWriter("E:\\Library management\\src\\book.txt",false);
                            BufferedReader bufferedReader_ = new BufferedReader(in_);
                            BufferedWriter bufferedWriter_ = new BufferedWriter(out_);
                            String str_;
                            while((str_=bufferedReader_.readLine())!=null){
                                bufferedWriter_.write(str_);
                                bufferedWriter_.newLine();
                            }
                            bufferedReader_.close();
                            bufferedWriter_.flush();
                            bufferedWriter_.close();

                        }catch (IOException a){
                            System.out.println(e.toString());
                        }
                        jf.dispose();
                    }
                });

                jf.add(jp,BorderLayout.NORTH);
                jf.add(jp2,BorderLayout.SOUTH);
                jf.setVisible(true);
                jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            }
        });
        //Refresh
        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    FileReader in = new FileReader("E:\\Library management\\src\\book.txt");
                    BufferedReader bufferedReader = new BufferedReader(in);
                    String str,str2="";
                    while((str=bufferedReader.readLine())!=null){
                        str2 += str + "\r\n";
                    }
                    area.setText(str2);
                    in.close();
                }
                catch (IOException a){
                    System.out.println(a.toString());
                }
            }
        });
        MenuArea.setLayout(new FlowLayout());
        MenuArea.add(jb1);
        MenuArea.add(jb2);
        MenuArea.add(jb3);
        jf.add(MenuArea,BorderLayout.SOUTH);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        BookSystem book = new BookSystem();
        book.MyFrame();
    }
}

Keywords: Java Back-end

Added by stephfox on Sun, 02 Jan 2022 15:11:25 +0200