Advertisement
Lowe576

Library

May 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. package librarysystem;
  2.  
  3. /**
  4. *
  5. * @author Lowe
  6. */
  7. public class LibrarySystem {
  8.  
  9. /**
  10. * @param args the command line arguments
  11. */
  12. public static void main(String[] args) {
  13. // TODO code application logic here
  14. Book b1 = new Book("Tures äventyr", 4500);
  15. Book b2 = new Book("Solskensdalen", 42332);
  16. Book b3 = new Book("Wikipedia vol 1", 42342);
  17.  
  18. Library LibraryN =new Library();
  19.  
  20. LibraryN.addBook(b1);
  21. LibraryN.addBook(b2);
  22. LibraryN.addBook(b3);
  23. System.out.println(LibraryN.findBook(1002).title());
  24.  
  25.  
  26.  
  27. Gui gui=new Gui(LibraryN);
  28. }
  29.  
  30. }
  31.  
  32.  
  33.  
  34. package librarysystem;
  35.  
  36. /**
  37. *
  38. * @author Lowe
  39. */
  40. import java.util.*;
  41. public class Library {
  42. private ArrayList books = new ArrayList();
  43.  
  44. public void addBook (Book b){
  45. books.add(b);
  46. }
  47. public Book findBook(String isbnNo){
  48. for(int i = 0; i < Books.size(); i++){
  49. Book temp = (Book)books.get(i);
  50. if(temp.isbnNo()== isbnNo){
  51. return temp;
  52. }
  53. }
  54. return null;
  55. }
  56.  
  57. }
  58.  
  59.  
  60.  
  61.  
  62. package librarysystem;
  63.  
  64. /**
  65. *
  66. * @author Lowe
  67. */
  68. public class Book {
  69. private int storage;
  70. private String isbnNo;
  71. private String title;
  72. private static int counter = 1000;
  73.  
  74. public Book(String title, double initBalance){
  75. this.title = title;
  76. this.storage = initStorage;
  77. }
  78.  
  79. public void rebound(double amount){
  80. this.storage += amount;
  81. }
  82.  
  83. public int storage(){
  84. return storage;}
  85. public void withdraw( double amount){
  86. if(storage >= amount){
  87. this.storage -= amount;
  88. }
  89. }
  90. public String isbnNo(){
  91. return isbnNo;
  92. }
  93. public String title(){
  94. return title;
  95. }
  96. public String toString(){
  97. return "name: " + holder + "\n " + "accountNo:" + accountNo + "\n " + "balance: " + balance +"\n";
  98. }
  99.  
  100. }
  101.  
  102.  
  103.  
  104. import java.awt.*;
  105. import java.awt.event.*;
  106. import javax.swing.*;
  107. public class Gui {
  108. private JFrame frame = new JFrame("Library");
  109. private JPanel background, centerPanel,southPanel;
  110. private JLabel l_isbnNo,l_title,l_rebound,l_lend,l_storage;
  111. private JTextField tf_isbnNo, tf_title, tf_rebound,tf_lend,tf_storage;
  112. private JButton b_find,b_rebound,b_lend,b_clear,b_addBook;
  113. private Book findBook;
  114. private Library libraryL;
  115.  
  116.  
  117. public Gui(Library l){
  118. LibraryL = l;
  119. background = new JPanel();
  120. background.setLayout(new BorderLayout());
  121. centerPanel = new JPanel();
  122. background.setBorder(BorderFactory.createLineBorder(Color.RED,9, false));
  123.  
  124. centerPanel.setLayout((new GridLayout(5,2)));
  125. southPanel = new JPanel();
  126. southPanel.setLayout((new GridLayout(3,2)));
  127. l_isbnNo = new JLabel("accountNo: ");
  128. l_title = new JLabel("name:");
  129. l_rebound = new JLabel ("deposit:");
  130. l_lend = new JLabel("withdraw:");
  131. l_storage = new JLabel("balance:");
  132. b_find = new JButton("FIND");
  133. b_rebound = new JButton("Deposit");
  134. b_lend = new JButton("Witdraw");
  135. b_clear = new JButton("CLEAR");
  136. b_addBook = new JButton("CREATE");
  137. southPanel.add(b_rebound);
  138. southPanel.add(b_find);
  139. southPanel.add(b_lend);
  140. southPanel.add(b_clear);
  141. southPanel.add(b_addBook);
  142.  
  143.  
  144. tf_isbnNo= new JTextField(12);
  145. tf_title = new JTextField(12);
  146. tf_rebound = new JTextField(12);
  147. tf_lend = new JTextField(12);
  148. tf_storage = new JTextField(12);
  149. centerPanel.add(l_isbnNo);
  150. centerPanel.add(tf_isbnNo);
  151. centerPanel.add(l_title);
  152. centerPanel.add(tf_title);
  153. centerPanel.add(l_storage);
  154. centerPanel.add(tf_storage);
  155. centerPanel.add(l_rebound);
  156. centerPanel.add(tf_rebound);
  157. centerPanel.add(l_lend);
  158. centerPanel.add(tf_lend);
  159. background.add(centerPanel, BorderLayout.CENTER);
  160. background.add(southPanel, BorderLayout.SOUTH);
  161. frame.setContentPane(background);
  162. frame.pack();
  163. frame.setVisible(true);
  164. ClearHandler cl = new ClearHandler();
  165. FH fh = new FH();
  166.  
  167. this.b_clear.addActionListener(cl);
  168. this.b_lend.addActionListener(new WH());
  169. this.b_rebound.addActionListener(new DH());
  170. this.b_find.addActionListener(fh);
  171. this.b_addBook.addActionListener(new Create());
  172. this.Library = libraryL;
  173.  
  174. }
  175. private class ClearHandler implements ActionListener{
  176. public void actionPerformed(ActionEvent ae){
  177. tf_isbnNo.setText("");
  178. tf_title.setText("");
  179. tf_rebound.setText("");
  180. tf_lend.setText("");
  181. tf_storage.setText("");
  182.  
  183. }
  184. }
  185. //private class DepositHandler implements ActionListener{
  186. //public void actionePerformed(ActionEvent ae){
  187. // double amount = Double.parseDouble(tf_deposit.getText());
  188. //foundAcc.deposit(amount);
  189. //tf_balance.setText(""+ foundAcc.balance)
  190. // System.out.println("deposit");
  191. //}
  192. //}
  193.  
  194. private class WH implements ActionListener{
  195. public void actionPerformed(ActionEvent ae){
  196. System.out.println("");
  197. String sWithdraw = tf_lend.getText();
  198. double amount = Double.parseDouble(sWithdraw);
  199. findBook.withdraw(amount);
  200. tf_storage.setText("" + findBook.storage());
  201.  
  202. }
  203. }
  204. private class DH implements ActionListener{
  205. public void actionPerformed(ActionEvent ae){
  206. System.out.println("");
  207. String sRebound = tf_rebound.getText();
  208. double amount = Double.parseDouble(sRebound);
  209. findBook.rebound(amount);
  210. tf_storage.setText("" + findBook.storage());
  211. System.out.println("Deposit" + amount);
  212. }
  213. // private class FL implements ActionListener{
  214. // public void actionPerformed(ActionEvent ae){
  215. // String accNo = tf_accountNo.getText();
  216. // foundAcc = banken.findAccount(Integer.parseInt(accNo));
  217. // String holder = founcAcc.holder();
  218. //double balance = foundAcc.holder();
  219. // tf_balance.setText(""+balance);
  220.  
  221. //}
  222. }
  223. private class FH implements ActionListener{
  224. public void actionPerformed(ActionEvent ae){
  225. System.out.println("");
  226. String sIsbnNo = tf_isbnNo.getText();
  227. int accNo = 0;
  228. try{
  229. accNo = Integer.parseInt(sIsbnNo);
  230.  
  231. }catch (NumberFormatException nfe){
  232. JOptionPane.showMessageDialog(frame, "skriv in siffror som kontonummer");
  233.  
  234. return;}
  235. Book findBook = libraryL.findBook(isbnNo);
  236. if(findBook!=null){
  237. tf_title.setText(findBook.title());
  238. tf_storage.setText(""+findBook.storage());
  239. }else JOptionPane.showMessageDialog(frame, "kontot finns inte! ");
  240. }
  241. }
  242. private class AddBook implements ActionListener{
  243. public void actionPerformed(ActionEvent ae){
  244. System.out.println("Create");
  245. String title = tf_title.getText();
  246. double amount = Double.parseDouble(tf_rebound.getText());
  247. Book create = new Book(title, storage);
  248. tf_isbnNo.setText("" + create.isbnNo());
  249. libraryL.addBook(create);
  250. tf_rebound.setText("");
  251. tf_storage.setText("" + create.storage());
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement