Advertisement
Lowe576

library

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