Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I have been searching around for hours now trying to find a way to enter isbn number in String format such as "97-123-43564-43-3" and find book title and storage amount. I dont understand how to search the array with String. The FindHandler is in the Gui line 113. Array name is books. Super sorry for long code.
- BIG Thanks in advance for anyone taking the time to help
- 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: Here is the location of the array
- 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 down here is 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();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement