用于在数组中搜索字符串的java方法

时间:2018-05-30 18:39:00

标签: java arrays string arraylist methods

我一直在搜索几个小时,试图找到一种方法以字符串格式输入isbn数字,例如“97-123-43564-43-3”并找到书名和存储金额。我不明白如何使用String搜索数组。 FindHandler位于Gui第113行。数组名称是书籍。非常抱歉长代码。

非常感谢任何花时间提供帮助的人

public class Book {
    int storage;
    private String isbn;
    private String title;
    //private static int counter = 1000;

    public Book( String title, String isbn, int initStorage){
        this.title = title;
        this.isbn= isbn;
        this.storage = storage;
    }

    public void returnB(int amount){
        this.storage += amount;
    }

    public void lend(int amount){
        if(storage > amount){
            this.storage -= amount;
        }
    }

    public int storage(){
        return storage;
    }

    public String isbn(){
        return isbn;
    }

    public String title(){
        return title;
    }
}

Library:这是数组的位置

import java.util.*;

public class Library {
    private final ArrayList books = new ArrayList();

    /**
     * @param args the command line arguments
     */
    public void addBook(Book b){
    books.add(b);
    }


    public Book findBook(String isbn){

        for(int i=0; i<books.size() ; i++){
        Book temp = (Book)books.get(i);

        if(temp.isbn()==isbn){
        return temp;
            }
        }
        return null;
    }

}

LibrarySystem

public class LibrarySystem {

    public static void main(String[] args){

        Book b1 = new Book ("Basic book","12-445-2346-23",5);

        libraryN.addBook(b1);

        Library libraryN = new Library();

        Gui gui = new Gui(libraryN);

    }
}

Gui line 113这里是FindHandler

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Gui {

private JFrame frame = new JFrame("Bibblan");
private JPanel background, centerPanel, southPanel;
private JLabel l_isbn,l_title, l_reclaim,l_lend,l_storage;
private JTextField tf_isbn,tf_title, tf_reclaim,tf_lend,tf_storage;
private JButton b_find,b_reclaim,b_lend,b_clear,b_newBook,b_save,b_load;
private Book findB;
private Library libraryN;


public Gui(Library l){
libraryN = l;
background = new JPanel();
background.setLayout((new BorderLayout()));
centerPanel = new JPanel();
background.setBorder(BorderFactory.createLineBorder(Color.BLUE,9,false));

centerPanel.setLayout((new GridLayout(5,2)));
southPanel = new JPanel();
southPanel.setLayout((new GridLayout(3,2)));

b_find = new JButton("find");
b_reclaim = new JButton("return");
b_lend = new JButton("lend");
b_clear = new JButton("clear");
b_newBook = new JButton("Add");
b_save = new JButton("save");
b_load = new JButton("load");

southPanel.add(b_find);
southPanel.add(b_reclaim);
southPanel.add(b_lend);
southPanel.add(b_clear);
southPanel.add(b_newBook);
southPanel.add(b_save);
southPanel.add(b_load);

l_isbn = new JLabel("isbn:");
l_title = new JLabel("title:");
l_reclaim = new JLabel("return:");
l_lend = new JLabel("lend:");
l_storage = new JLabel("storage:");

tf_isbn = new JTextField(12);
tf_title = new JTextField(12);
tf_reclaim = new JTextField(12);
tf_lend = new JTextField(12);
tf_storage = new JTextField(12);

centerPanel.add(l_isbn);
centerPanel.add(tf_isbn);
centerPanel.add(l_title);
centerPanel.add(tf_title);
centerPanel.add(l_reclaim);
centerPanel.add(tf_reclaim);
centerPanel.add(l_lend);
centerPanel.add(tf_lend);
centerPanel.add(l_storage);
centerPanel.add(tf_storage);

frame.setContentPane(background);
frame.pack();
frame.setVisible(true);

background.add(centerPanel,BorderLayout.CENTER);
background.add(southPanel,BorderLayout.SOUTH);

SaveHandler SL = new SaveHandler();
ClearHandler cl = new ClearHandler();
FindHandler fh = new FindHandler();
LendHandler LE = new LendHandler();
ReclaimHandler RE = new ReclaimHandler();
AddHandler AH = new AddHandler();
LoadHandler LO = new LoadHandler();

this.b_save.addActionListener(SL);
this.b_clear.addActionListener(cl);
this.b_lend.addActionListener(LE);
this.b_reclaim.addActionListener(RE);
this.b_find.addActionListener(fh);
this.b_newBook.addActionListener(AH);
this.b_load.addActionListener(LO);
this.libraryN = libraryN;

    }
private class ClearHandler implements ActionListener{
    public void actionPerformed(ActionEvent ae){
        System.out.println("clear");
        tf_isbn.setText("");
        tf_title.setText("");
        tf_storage.setText("");
        tf_lend.setText("");
        tf_reclaim.setText("");
    }
}
private class FindHandler implements ActionListener{
    public void actionPerformed(ActionEvent ae){
       System.out.println("find");
       String isbn = tf_isbn.getText();
    }
}
private class LendHandler implements ActionListener{
    public void actionPerformed(ActionEvent ae){
        System.out.println("lend");

    }
}
private class ReclaimHandler implements ActionListener{
    public void actionPerformed(ActionEvent ae){
        System.out.println("reclaim");
    }
}
private class AddHandler implements ActionListener{
    public void actionPerformed(ActionEvent ae){
        System.out.println("add");
        String title = tf_title.getText();
        String isbn = tf_isbn.getText();
        int amount = Integer.valueOf(tf_storage.getText());
        Book newBook = new Book(title,isbn, amount);
        libraryN.addBook(newBook);
        tf_reclaim.setText("");
        tf_storage.setText("" + newBook.storage);
    }
}
private class LoadHandler implements ActionListener{
    public void actionPerformed(ActionEvent ae){
        System.out.println("load");
        try{
        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("library.srz")));
        libraryN=(Library)in.readObject();

        in.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
private class SaveHandler implements ActionListener{
    public void actionPerformed(ActionEvent ae){
        System.out.println("save");
        try{
            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("library.srz")));
            out.writeObject(libraryN);
            out.close();

        }catch(Exception e){
            e.printStackTrace();
        }
    }

}


}

0 个答案:

没有答案