Advertisement
Lowe576

libraryDone

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