Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class BookSystem extends JFrame {
  9.  
  10. //Swing elements to for user interface
  11. JPanel panel;
  12. JButton btnAdd;
  13. JButton btnDelete;
  14. JButton btnSearch;
  15. JTextField txtTitle;
  16. JTextField txtAuthor;
  17. JTextField txtIsbn;
  18. JTextField txtPrice;
  19. JLabel lblTitle;
  20. JLabel lblAuthor;
  21. JLabel lblIsbn;
  22. JLabel lblPrice;
  23.  
  24.  
  25. //List to hold all stored instances of book class by BookSystem
  26. List<Book> storedBooks = new ArrayList<Book>();
  27.  
  28. public BookSystem() {
  29. Book emptyBook = new Book();
  30. Book davinciCode = new Book("Davinci's Code","Leonardo Davinci","AAA31G",10);
  31. System.out.println("Empty book :");
  32. emptyBook.displayToConsole();
  33. System.out.println("Populated book : ");
  34. davinciCode.displayToConsole();
  35.  
  36. initUI();
  37. }
  38.  
  39. private void initUI() {
  40. //set up frame
  41. setSize(new Dimension(350,200));
  42. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.  
  44. //setup panels for buttons and textfields
  45. JPanel labelAndTextPanel = new JPanel();
  46. labelAndTextPanel.setLayout(new GridLayout(2,4));
  47. JPanel buttonPanel = new JPanel();
  48. buttonPanel.setLayout(new GridLayout(1,3));
  49. panel = new JPanel();
  50. panel.setLayout(new GridLayout(2,1));
  51.  
  52. //setup all buttons, labels, textfields
  53. txtAuthor = new JTextField();
  54. txtIsbn = new JTextField();
  55. txtPrice = new JTextField();
  56. txtTitle = new JTextField();
  57.  
  58. lblAuthor = new JLabel("Author");
  59. lblIsbn = new JLabel("ISBN Code");
  60. lblPrice = new JLabel("Price");
  61. lblTitle = new JLabel("Title");
  62.  
  63. btnAdd = new JButton("Add");
  64. btnDelete = new JButton("Delete");
  65. btnSearch = new JButton("Search");
  66.  
  67. //Action listeners to activate when buttons are clicked
  68. btnAdd.addActionListener(new ActionListener() {
  69. @Override
  70. public void actionPerformed(ActionEvent e) {
  71. addBook();
  72. }
  73. });
  74.  
  75. btnSearch.addActionListener(new ActionListener() {
  76. @Override
  77. public void actionPerformed(ActionEvent e) {
  78. searchBook();
  79. }
  80. });
  81.  
  82. btnDelete.addActionListener(new ActionListener() {
  83. @Override
  84. public void actionPerformed(ActionEvent e) {
  85. deleteBook();
  86. }
  87. });
  88.  
  89. //add elements to the panels
  90. buttonPanel.add(btnAdd);
  91. buttonPanel.add(btnDelete);
  92. buttonPanel.add(btnSearch);
  93. labelAndTextPanel.add(lblTitle);
  94. labelAndTextPanel.add(lblAuthor);
  95. labelAndTextPanel.add(lblIsbn);
  96. labelAndTextPanel.add(lblPrice);
  97. labelAndTextPanel.add(txtTitle);
  98. labelAndTextPanel.add(txtAuthor);
  99. labelAndTextPanel.add(txtIsbn);
  100. labelAndTextPanel.add(txtPrice);
  101. panel.add(labelAndTextPanel);
  102. panel.add(buttonPanel);
  103.  
  104. setContentPane(panel);
  105. pack();
  106. setVisible(true);
  107. }
  108.  
  109. private void addBook() {
  110. //create the book using information from text fields
  111. Book book = new Book();
  112. book.setAuthor(txtAuthor.getText());
  113. book.setIsbn(txtIsbn.getText());
  114. book.setTitle(txtTitle.getText());
  115. double price = Double.valueOf(txtPrice.getText());
  116. book.setPrice(price);
  117.  
  118. //add the book to list of stored books
  119. storedBooks.add(book);
  120.  
  121. //display the added book to console for debugging
  122. System.out.println("Book Added :");
  123. book.displayToConsole();
  124. }
  125.  
  126. private void deleteBook() {
  127. double price = Double.valueOf(txtPrice.getText());
  128. String title = txtTitle.getText();
  129. String author = txtAuthor.getText();
  130. String isbn = txtIsbn.getText();
  131.  
  132. //create an empty book to store the book to be removed
  133. Book bookToRemove = null;
  134.  
  135. //look through all stored books to match with above information
  136. for(Book b : storedBooks) {
  137. if(b.getAuthor().equals(author) && b.getIsbn().equals(isbn) && b.getTitle().equals(title) && b.getPrice() == price) {
  138. bookToRemove = b;// new Book(b);
  139. break; //break out of array as we've found the book
  140. }
  141. }
  142.  
  143. //remove the book if its found
  144. if(bookToRemove != null) {
  145. storedBooks.remove(bookToRemove);
  146. //display the removed book to console for debugging
  147. System.out.println("Book Deleted :");
  148. bookToRemove.displayToConsole();
  149. bookToRemove = null;
  150. }
  151. }
  152.  
  153. private void searchBook() {
  154. // double price = Double.valueOf(txtPrice.getText());
  155. // String title = txtTitle.getText();
  156. // String author = txtAuthor.getText();
  157. // String isbn = txtAuthor.getText();
  158. System.out.println("Books Found :");
  159. for(Book b : storedBooks) {
  160. b.displayToConsole();
  161. }
  162. }
  163.  
  164. private class Book {
  165.  
  166. private String title = "";
  167. private String author = "";
  168. private String isbn = "";
  169. private double price = 0;
  170.  
  171. //CONSTRUCTOR WITH EMPTY PARAMETERS
  172. public Book() {
  173.  
  174. }
  175.  
  176. //CONSTRUCTOR WITH PARAMETERS
  177. public Book(String title, String author, String isbn, double price) {
  178. //Assign passed values to values of this class
  179. this.title = title;
  180. this.author = author;
  181. this.isbn = isbn;
  182. this.price = price;
  183. }
  184.  
  185. //CONSTRUCTOR FROM ANOTHER BOOK
  186. public Book(Book that) {
  187. this.title = that.getTitle();
  188. this.author = that.getAuthor();
  189. this.isbn = that.getIsbn();
  190. this.price = that.getPrice();
  191. }
  192.  
  193. //GETTERS AND SETTERS
  194. public String getTitle() {
  195. return title;
  196. }
  197.  
  198. public String getAuthor() {
  199. return author;
  200. }
  201.  
  202. public String getIsbn() {
  203. return isbn;
  204. }
  205.  
  206. public double getPrice() {
  207. return price;
  208. }
  209.  
  210. public void setTitle(String title) {
  211. this.title = title;
  212. }
  213.  
  214. public void setAuthor(String author) {
  215. this.author = author;
  216. }
  217.  
  218. public void setIsbn(String isbn) {
  219. this.isbn = isbn;
  220. }
  221.  
  222. public void setPrice(double price) {
  223. this.price = price;
  224. }
  225.  
  226. //function to display book to console window
  227. public void displayToConsole() {
  228. System.out.println();
  229. System.out.println("Title : " + title);
  230. System.out.println("Author : " + author);
  231. System.out.println("ISBN : " + isbn);
  232. System.out.println("Price : " + price);
  233. }
  234. }
  235.  
  236. public static void main(String[] args) {
  237. BookSystem system = new BookSystem();
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement