Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.List;
- public class BookSystem extends JFrame {
- //Swing elements to for user interface
- JPanel panel;
- JButton btnAdd;
- JButton btnDelete;
- JButton btnSearch;
- JTextField txtTitle;
- JTextField txtAuthor;
- JTextField txtIsbn;
- JTextField txtPrice;
- JLabel lblTitle;
- JLabel lblAuthor;
- JLabel lblIsbn;
- JLabel lblPrice;
- //List to hold all stored instances of book class by BookSystem
- List<Book> storedBooks = new ArrayList<Book>();
- public BookSystem() {
- Book emptyBook = new Book();
- Book davinciCode = new Book("Davinci's Code","Leonardo Davinci","AAA31G",10);
- System.out.println("Empty book :");
- emptyBook.displayToConsole();
- System.out.println("Populated book : ");
- davinciCode.displayToConsole();
- initUI();
- }
- private void initUI() {
- //set up frame
- setSize(new Dimension(350,200));
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //setup panels for buttons and textfields
- JPanel labelAndTextPanel = new JPanel();
- labelAndTextPanel.setLayout(new GridLayout(2,4));
- JPanel buttonPanel = new JPanel();
- buttonPanel.setLayout(new GridLayout(1,3));
- panel = new JPanel();
- panel.setLayout(new GridLayout(2,1));
- //setup all buttons, labels, textfields
- txtAuthor = new JTextField();
- txtIsbn = new JTextField();
- txtPrice = new JTextField();
- txtTitle = new JTextField();
- lblAuthor = new JLabel("Author");
- lblIsbn = new JLabel("ISBN Code");
- lblPrice = new JLabel("Price");
- lblTitle = new JLabel("Title");
- btnAdd = new JButton("Add");
- btnDelete = new JButton("Delete");
- btnSearch = new JButton("Search");
- //Action listeners to activate when buttons are clicked
- btnAdd.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- addBook();
- }
- });
- btnSearch.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- searchBook();
- }
- });
- btnDelete.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- deleteBook();
- }
- });
- //add elements to the panels
- buttonPanel.add(btnAdd);
- buttonPanel.add(btnDelete);
- buttonPanel.add(btnSearch);
- labelAndTextPanel.add(lblTitle);
- labelAndTextPanel.add(lblAuthor);
- labelAndTextPanel.add(lblIsbn);
- labelAndTextPanel.add(lblPrice);
- labelAndTextPanel.add(txtTitle);
- labelAndTextPanel.add(txtAuthor);
- labelAndTextPanel.add(txtIsbn);
- labelAndTextPanel.add(txtPrice);
- panel.add(labelAndTextPanel);
- panel.add(buttonPanel);
- setContentPane(panel);
- pack();
- setVisible(true);
- }
- private void addBook() {
- //create the book using information from text fields
- Book book = new Book();
- book.setAuthor(txtAuthor.getText());
- book.setIsbn(txtIsbn.getText());
- book.setTitle(txtTitle.getText());
- double price = Double.valueOf(txtPrice.getText());
- book.setPrice(price);
- //add the book to list of stored books
- storedBooks.add(book);
- //display the added book to console for debugging
- System.out.println("Book Added :");
- book.displayToConsole();
- }
- private void deleteBook() {
- double price = Double.valueOf(txtPrice.getText());
- String title = txtTitle.getText();
- String author = txtAuthor.getText();
- String isbn = txtIsbn.getText();
- //create an empty book to store the book to be removed
- Book bookToRemove = null;
- //look through all stored books to match with above information
- for(Book b : storedBooks) {
- if(b.getAuthor().equals(author) && b.getIsbn().equals(isbn) && b.getTitle().equals(title) && b.getPrice() == price) {
- bookToRemove = b;// new Book(b);
- break; //break out of array as we've found the book
- }
- }
- //remove the book if its found
- if(bookToRemove != null) {
- storedBooks.remove(bookToRemove);
- //display the removed book to console for debugging
- System.out.println("Book Deleted :");
- bookToRemove.displayToConsole();
- bookToRemove = null;
- }
- }
- private void searchBook() {
- // double price = Double.valueOf(txtPrice.getText());
- // String title = txtTitle.getText();
- // String author = txtAuthor.getText();
- // String isbn = txtAuthor.getText();
- System.out.println("Books Found :");
- for(Book b : storedBooks) {
- b.displayToConsole();
- }
- }
- private class Book {
- private String title = "";
- private String author = "";
- private String isbn = "";
- private double price = 0;
- //CONSTRUCTOR WITH EMPTY PARAMETERS
- public Book() {
- }
- //CONSTRUCTOR WITH PARAMETERS
- public Book(String title, String author, String isbn, double price) {
- //Assign passed values to values of this class
- this.title = title;
- this.author = author;
- this.isbn = isbn;
- this.price = price;
- }
- //CONSTRUCTOR FROM ANOTHER BOOK
- public Book(Book that) {
- this.title = that.getTitle();
- this.author = that.getAuthor();
- this.isbn = that.getIsbn();
- this.price = that.getPrice();
- }
- //GETTERS AND SETTERS
- public String getTitle() {
- return title;
- }
- public String getAuthor() {
- return author;
- }
- public String getIsbn() {
- return isbn;
- }
- public double getPrice() {
- return price;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public void setIsbn(String isbn) {
- this.isbn = isbn;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- //function to display book to console window
- public void displayToConsole() {
- System.out.println();
- System.out.println("Title : " + title);
- System.out.println("Author : " + author);
- System.out.println("ISBN : " + isbn);
- System.out.println("Price : " + price);
- }
- }
- public static void main(String[] args) {
- BookSystem system = new BookSystem();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement