Advertisement
Lowe576

Untitled

May 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. public class Book {
  2. private int storage;
  3. private String isbnNo;
  4. private String title;
  5. private static int counter = 1000;
  6.  
  7. public Book(String title, int initStorage){
  8. this.title = title;
  9. this.isbnNo = isbnNo;
  10. this.storage = storage;
  11.  
  12. }
  13. public void reclaim(int amount){
  14. this.storage += amount;
  15. }
  16. public void lend(int amount){
  17. if(storage > amount){
  18. this.storage -= amount;
  19. }
  20. }
  21.  
  22. public String isbnNo(){
  23. return isbnNo;
  24. }
  25. public String title(){
  26. return title;
  27.  
  28. }
  29. public int storage(){
  30. return storage;
  31. }
  32. public String toString(){
  33. return "title: " + title + "\n"+
  34. "isbnNo: " + isbnNo + "\n"+
  35. "storage: " + storage + "\n" +
  36. "-------------------";
  37. }
  38. }
  39.  
  40.  
  41.  
  42.  
  43. import java.util.*;
  44.  
  45. public class Library2 {
  46. private final ArrayList books = new ArrayList();
  47.  
  48. public void addBook(Book b){
  49. books.add(b);
  50. }
  51. public Book findBook(String isbnNo){
  52.  
  53. for(int i=0 ; i<books.size() ; i++){
  54. Book temp = (Book)books.get(i);
  55.  
  56. if(temp.isbnNo()==isbnNo){
  57. return temp;
  58. }
  59. }
  60. return null;
  61. }
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. import java.awt.*;
  70. import java.awt.event.*;
  71. import javax.swing.*;
  72. public class Gui {
  73. private JFrame frame = new JFrame("Budget library");
  74. private JPanel background, centerPanel,southPanel,menuBar;
  75. private JLabel l_isbnNo,l_title,l_reclaim,l_lend,l_storage;
  76. private JTextField tf_isbnNo, tf_title, tf_reclaim,tf_lend,tf_storage;
  77. private JButton b_find,b_reclaim,b_lend,b_clear,b_addBook;
  78. private Library2 library;
  79. private JMenuBar baren;
  80. private JMenu menu;
  81. private JMenuItem i_item,i_isbnNo,i_reclaim,i_lend,i_clear,i_find;
  82. private Book findB;
  83.  
  84.  
  85.  
  86.  
  87. public Gui(Library2 l){
  88. library = l;
  89.  
  90. background = new JPanel();
  91. background.setLayout(new BorderLayout());
  92. centerPanel = new JPanel();
  93. // background.setBorder(BorderFactory.addBookLineBorder(Color.RED,9, false));
  94.  
  95. centerPanel.setLayout((new GridLayout(5,2)));
  96. southPanel = new JPanel();
  97. southPanel.setLayout((new GridLayout(3,2)));
  98. l_isbnNo = new JLabel("isbnNo: ");
  99. l_title = new JLabel("title:");
  100. l_reclaim = new JLabel ("reclaim:");
  101. l_lend = new JLabel("lend:");
  102. l_storage = new JLabel("storage:");
  103. b_find = new JButton("FIND");
  104. b_reclaim = new JButton("Return");
  105. b_lend = new JButton("Lend");
  106. b_clear = new JButton("Clear");
  107. b_addBook = new JButton("Add book");
  108. southPanel.add(b_reclaim);
  109. southPanel.add(b_find);
  110. southPanel.add(b_lend);
  111. southPanel.add(b_clear);
  112. southPanel.add(b_addBook);
  113.  
  114.  
  115.  
  116. tf_isbnNo= new JTextField(12);
  117. tf_title = new JTextField(12);
  118. tf_reclaim= new JTextField(12);
  119. tf_lend= new JTextField(12);
  120. tf_storage= new JTextField(12);
  121. centerPanel.add(l_isbnNo);
  122. centerPanel.add(tf_isbnNo);
  123. centerPanel.add(l_title);
  124. centerPanel.add(tf_title);
  125. centerPanel.add(l_storage);
  126. centerPanel.add(tf_storage);
  127. centerPanel.add(l_reclaim);
  128. centerPanel.add(tf_reclaim);
  129. centerPanel.add(l_lend);
  130. centerPanel.add(tf_lend);
  131. background.add(centerPanel, BorderLayout.CENTER);
  132. background.add(southPanel, BorderLayout.SOUTH);
  133. frame.setContentPane(background);
  134. frame.pack();
  135. frame.setVisible(true);
  136. ClearHandler cl = new ClearHandler();
  137. FindHandler fh = new FindHandler();
  138.  
  139. this.b_clear.addActionListener(cl);
  140. this.i_clear.addActionListener(cl);
  141. this.b_lend.addActionListener(new LD());
  142. this.b_reclaim.addActionListener(new RM());
  143. this.b_find.addActionListener(fh);
  144. this.i_find.addActionListener(fh);
  145. this.b_addBook.addActionListener(new AddBook());
  146. this.library = library;
  147.  
  148. }
  149. private class ClearHandler implements ActionListener{
  150. public void actionPerformed(ActionEvent ae){
  151. tf_isbnNo.setText("");
  152. tf_title.setText("");
  153. tf_reclaim.setText("");
  154. tf_lend.setText("");
  155. tf_storage.setText("");
  156.  
  157. }
  158. }
  159.  
  160. private class LD implements ActionListener{
  161. public void actionPerformed(ActionEvent ae){
  162. String sLend = tf_lend.getText();
  163. int amount = Int.parseInt(sLend);
  164. findB.lend(amount);
  165. tf_storage.setText("" + findB.storage());
  166.  
  167. }
  168. }
  169. private class RM implements ActionListener{
  170. public void actionPerformed(ActionEvent ae){
  171. String sReclaim = tf_reclaim.getText();
  172. int amount = Int.parseInt(sReclaim);
  173. findB.reclaim(amount);
  174. tf_storage.setText("" + findB.storage());
  175. System.out.println("Return" + amount);
  176. } }
  177. private class FindHandler implements ActionListener{
  178. public void actionPerformed(ActionEvent ae){
  179. String sIsbnNo = tf_isbnNo.getText();
  180. try{
  181.  
  182. }catch (NumberFormatException nfe){
  183. JOptionPane.showMessageDialog(frame, "skriv in siffror som isbnNummer");
  184.  
  185. return;}
  186. Book findB = library.findBook(sIsbnNo);
  187. if(findB!=null){
  188. tf_title.setText(findB.title());
  189. tf_storage.setText(""+findB.storage());
  190. }else JOptionPane.showMessageDialog(frame, "boken finns inte! ");
  191. }
  192. }
  193. private class AddBook implements ActionListener{
  194. public void actionPerformed(ActionEvent ae){
  195. System.out.println("add Book");
  196. String title = tf_title.getText();
  197. String isbnNo = tf_isbnNo.getText();
  198. int storage = Int.parseInt(tf_reclaim.getText());
  199. Book addBook = new Book(title, amount);
  200. tf_isbnNo.setText("" + addBook.isbnNo());
  201. library.addBook(addBook);
  202. tf_reclaim.setText("");
  203. tf_storage.setText("" + addBook.storage());
  204.  
  205.  
  206. }
  207. }
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214. public class LibrarySystem {
  215.  
  216. /**
  217. * @param args the command line arguments
  218. */
  219. public static void main(String[] args) {
  220.  
  221. // TODO code application logic here
  222. Book b1 = new Book ("Wikipedia Vol 1", 5);
  223. Book b2 = new Book ("Linas lina", 2);
  224. Book b3 = new Book ("Bortom vind och horisont", 1);
  225.  
  226. Library2 library = new Library2();
  227.  
  228.  
  229. library.addBook(b1);
  230. library.addBook(b2);
  231. library.addBook(b3);
  232. System.out.println(library.findBook(1001));
  233. System.out.println(library.findBook(1002));
  234. System.out.println(library.findBook(1003));
  235.  
  236.  
  237. Gui gui = new Gui(library);
  238.  
  239. } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement