Advertisement
Lowe576

Untitled

May 30th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. 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.
  2.  
  3. BIG Thanks in advance for anyone taking the time to help
  4.  
  5. public class Book {
  6. int storage;
  7. private String isbn;
  8. private String title;
  9. //private static int counter = 1000;
  10.  
  11. public Book( String title, String isbn, int initStorage){
  12. this.title = title;
  13. this.isbn= isbn;
  14. this.storage = storage;
  15. }
  16.  
  17. public void returnB(int amount){
  18. this.storage += amount;
  19. }
  20. public void lend(int amount){
  21. if(storage > amount){
  22. this.storage -= amount;
  23. }
  24. }
  25.  
  26. public int storage(){
  27. return storage;
  28. }
  29.  
  30. public String isbn(){
  31. return isbn;
  32. }
  33.  
  34. public String title(){
  35. return title;
  36. }
  37. }
  38.  
  39. Library: Here is the location of the array
  40.  
  41. import java.util.*;
  42.  
  43. public class Library {
  44. private final ArrayList books = new ArrayList();
  45.  
  46. /**
  47. * @param args the command line arguments
  48. */
  49. public void addBook(Book b){
  50. books.add(b);
  51. }
  52.  
  53.  
  54. public Book findBook(String isbn){
  55.  
  56. for(int i=0; i<books.size() ; i++){
  57. Book temp = (Book)books.get(i);
  58.  
  59. if(temp.isbn()==isbn){
  60. return temp;
  61. }
  62. }
  63. return null;
  64. }
  65.  
  66. }
  67.  
  68. LibrarySystem
  69.  
  70. public class LibrarySystem {
  71.  
  72. public static void main(String[] args){
  73.  
  74. Book b1 = new Book ("Basic book","12-445-2346-23",5);
  75.  
  76. libraryN.addBook(b1);
  77.  
  78. Library libraryN = new Library();
  79.  
  80. Gui gui = new Gui(libraryN);
  81.  
  82. }
  83. }
  84.  
  85. Gui line 113 down here is FindHandler
  86.  
  87. import java.awt.*;
  88. import java.awt.event.*;
  89. import javax.swing.*;
  90. import java.io.*;
  91.  
  92. public class Gui {
  93.  
  94. private JFrame frame = new JFrame("Bibblan");
  95. private JPanel background, centerPanel, southPanel;
  96. private JLabel l_isbn,l_title, l_reclaim,l_lend,l_storage;
  97. private JTextField tf_isbn,tf_title, tf_reclaim,tf_lend,tf_storage;
  98. private JButton b_find,b_reclaim,b_lend,b_clear,b_newBook,b_save,b_load;
  99. private Book findB;
  100. private Library libraryN;
  101.  
  102.  
  103. public Gui(Library l){
  104. libraryN = l;
  105. background = new JPanel();
  106. background.setLayout((new BorderLayout()));
  107. centerPanel = new JPanel();
  108. background.setBorder(BorderFactory.createLineBorder(Color.BLUE,9,false));
  109.  
  110. centerPanel.setLayout((new GridLayout(5,2)));
  111. southPanel = new JPanel();
  112. southPanel.setLayout((new GridLayout(3,2)));
  113.  
  114. b_find = new JButton("find");
  115. b_reclaim = new JButton("return");
  116. b_lend = new JButton("lend");
  117. b_clear = new JButton("clear");
  118. b_newBook = new JButton("Add");
  119. b_save = new JButton("save");
  120. b_load = new JButton("load");
  121.  
  122. southPanel.add(b_find);
  123. southPanel.add(b_reclaim);
  124. southPanel.add(b_lend);
  125. southPanel.add(b_clear);
  126. southPanel.add(b_newBook);
  127. southPanel.add(b_save);
  128. southPanel.add(b_load);
  129.  
  130. l_isbn = new JLabel("isbn:");
  131. l_title = new JLabel("title:");
  132. l_reclaim = new JLabel("return:");
  133. l_lend = new JLabel("lend:");
  134. l_storage = new JLabel("storage:");
  135.  
  136. tf_isbn = new JTextField(12);
  137. tf_title = new JTextField(12);
  138. tf_reclaim = new JTextField(12);
  139. tf_lend = new JTextField(12);
  140. tf_storage = new JTextField(12);
  141.  
  142. centerPanel.add(l_isbn);
  143. centerPanel.add(tf_isbn);
  144. centerPanel.add(l_title);
  145. centerPanel.add(tf_title);
  146. centerPanel.add(l_reclaim);
  147. centerPanel.add(tf_reclaim);
  148. centerPanel.add(l_lend);
  149. centerPanel.add(tf_lend);
  150. centerPanel.add(l_storage);
  151. centerPanel.add(tf_storage);
  152.  
  153. frame.setContentPane(background);
  154. frame.pack();
  155. frame.setVisible(true);
  156.  
  157. background.add(centerPanel,BorderLayout.CENTER);
  158. background.add(southPanel,BorderLayout.SOUTH);
  159.  
  160. SaveHandler SL = new SaveHandler();
  161. ClearHandler cl = new ClearHandler();
  162. FindHandler fh = new FindHandler();
  163. LendHandler LE = new LendHandler();
  164. ReclaimHandler RE = new ReclaimHandler();
  165. AddHandler AH = new AddHandler();
  166. LoadHandler LO = new LoadHandler();
  167.  
  168. this.b_save.addActionListener(SL);
  169. this.b_clear.addActionListener(cl);
  170. this.b_lend.addActionListener(LE);
  171. this.b_reclaim.addActionListener(RE);
  172. this.b_find.addActionListener(fh);
  173. this.b_newBook.addActionListener(AH);
  174. this.b_load.addActionListener(LO);
  175. this.libraryN = libraryN;
  176.  
  177. }
  178. private class ClearHandler implements ActionListener{
  179. public void actionPerformed(ActionEvent ae){
  180. System.out.println("clear");
  181. tf_isbn.setText("");
  182. tf_title.setText("");
  183. tf_storage.setText("");
  184. tf_lend.setText("");
  185. tf_reclaim.setText("");
  186. }
  187. }
  188. private class FindHandler implements ActionListener{
  189. public void actionPerformed(ActionEvent ae){
  190. System.out.println("find");
  191. String isbn = tf_isbn.getText();
  192. }
  193. }
  194. private class LendHandler implements ActionListener{
  195. public void actionPerformed(ActionEvent ae){
  196. System.out.println("lend");
  197.  
  198. }
  199. }
  200. private class ReclaimHandler implements ActionListener{
  201. public void actionPerformed(ActionEvent ae){
  202. System.out.println("reclaim");
  203. }
  204. }
  205. private class AddHandler implements ActionListener{
  206. public void actionPerformed(ActionEvent ae){
  207. System.out.println("add");
  208. String title = tf_title.getText();
  209. String isbn = tf_isbn.getText();
  210. int amount = Integer.valueOf(tf_storage.getText());
  211. Book newBook = new Book(title,isbn, amount);
  212. libraryN.addBook(newBook);
  213. tf_reclaim.setText("");
  214. tf_storage.setText("" + newBook.storage);
  215. }
  216. }
  217. private class LoadHandler implements ActionListener{
  218. public void actionPerformed(ActionEvent ae){
  219. System.out.println("load");
  220. try{
  221. ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("library.srz")));
  222. libraryN=(Library)in.readObject();
  223.  
  224. in.close();
  225. }catch(Exception e){
  226. e.printStackTrace();
  227. }
  228. }
  229. }
  230. private class SaveHandler implements ActionListener{
  231. public void actionPerformed(ActionEvent ae){
  232. System.out.println("save");
  233. try{
  234. ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("library.srz")));
  235. out.writeObject(libraryN);
  236. out.close();
  237.  
  238. }catch(Exception e){
  239. e.printStackTrace();
  240. }
  241. }
  242.  
  243. }
  244.  
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement