Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package library;
- /**
- *
- * @author Lowe
- */
- public class Book implements java.io.Serializable{
- int storage;
- private String isbn;
- private String title;
- public Book( String title, String isbn, int storage){
- this.title = title;
- this.isbn= isbn;
- this.storage = storage;
- }
- public void returnB(int amount){
- this.storage += amount;
- }
- public void lend(int amount){
- if(storage > amount){
- this.storage -= amount;
- }
- }
- public int storage(){
- return storage;
- }
- public String isbn(){
- return isbn;
- }
- public String title(){
- return title;
- }
- public String toString(){
- return "title: " + title + "\n"+
- "isbn: " + isbn + "\n"+
- "storage: " + storage + "\n" +
- "-------------------";
- }
- }
- package library;
- import java.util.*;
- /**
- *
- * @author Lowe
- */
- public class Library implements java.io.Serializable{
- private final ArrayList books = new ArrayList();
- /**
- * @param args the command line arguments
- */
- public void addBook(Book b){
- books.add(b);
- }
- public Book findBook(String isbn){
- for(int i=0; i<books.size() ; i++){
- Book temp = (Book)books.get(i);
- if(temp.isbn().equals(isbn)){
- return temp;
- }
- }
- return null;
- }
- }
- package library;
- /**
- *
- * @author Lowe
- */
- public class LibrarySystem {
- public static void main(String[] args){
- Book a1 = new Book ("Wikipedia vol 1","976-1-2-25756-34-1",4);
- Book a2 = new Book ("Bortom vind och horisont","978-1-23-45678-12",7);
- Book a3 = new Book ("Stinas Γventyr","978-21-30-26358-8",2);
- Book a4 = new Book ("Programmering 1 Java", "978-91-40-67710-5", 30);
- Library libraryN = new Library();
- libraryN.addBook(a1);
- libraryN.addBook(a2);
- libraryN.addBook(a3);
- libraryN.addBook(a4);
- Gui gui = new Gui(libraryN);
- }
- }
- package library;
- /**
- *
- * @author Lowe
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.io.*;
- public class Gui {
- private JFrame frame = new JFrame("Bibblan");
- private JPanel background, centerPanel, southPanel;
- private JLabel l_isbn,l_title, l_reclaim,l_lend,l_storage;
- private JTextField tf_isbn,tf_title, tf_reclaim,tf_lend,tf_storage;
- private JButton b_find,b_reclaim,b_lend,b_clear,b_newBook,b_save,b_load;
- private Book findB;
- private Library libraryN;
- public Gui(Library l){
- libraryN = l;
- background = new JPanel();
- background.setLayout((new BorderLayout()));
- centerPanel = new JPanel();
- background.setBorder(BorderFactory.createLineBorder(Color.BLUE,9,false));
- centerPanel.setLayout((new GridLayout(5,2)));
- southPanel = new JPanel();
- southPanel.setLayout((new GridLayout(3,2)));
- b_find = new JButton("find");
- b_reclaim = new JButton("return");
- b_lend = new JButton("lend");
- b_clear = new JButton("clear");
- b_newBook = new JButton("Add");
- b_save = new JButton("save");
- b_load = new JButton("load");
- southPanel.add(b_find);
- southPanel.add(b_reclaim);
- southPanel.add(b_lend);
- southPanel.add(b_clear);
- southPanel.add(b_newBook);
- southPanel.add(b_save);
- southPanel.add(b_load);
- l_isbn = new JLabel("isbn:");
- l_title = new JLabel("title:");
- l_reclaim = new JLabel("return:");
- l_lend = new JLabel("lend:");
- l_storage = new JLabel("storage:");
- tf_isbn = new JTextField(12);
- tf_title = new JTextField(12);
- tf_reclaim = new JTextField(12);
- tf_lend = new JTextField(12);
- tf_storage = new JTextField(12);
- centerPanel.add(l_isbn);
- centerPanel.add(tf_isbn);
- centerPanel.add(l_title);
- centerPanel.add(tf_title);
- centerPanel.add(l_reclaim);
- centerPanel.add(tf_reclaim);
- centerPanel.add(l_lend);
- centerPanel.add(tf_lend);
- centerPanel.add(l_storage);
- centerPanel.add(tf_storage);
- frame.setContentPane(background);
- frame.pack();
- frame.setVisible(true);
- background.add(centerPanel,BorderLayout.CENTER);
- background.add(southPanel,BorderLayout.SOUTH);
- SaveHandler SL = new SaveHandler();
- ClearHandler cl = new ClearHandler();
- FindHandler fh = new FindHandler();
- LendHandler LE = new LendHandler();
- ReclaimHandler RE = new ReclaimHandler();
- AddHandler AH = new AddHandler();
- LoadHandler LO = new LoadHandler();
- this.b_save.addActionListener(SL);
- this.b_clear.addActionListener(cl);
- this.b_lend.addActionListener(LE);
- this.b_reclaim.addActionListener(RE);
- this.b_find.addActionListener(fh);
- this.b_newBook.addActionListener(AH);
- this.b_load.addActionListener(LO);
- this.libraryN = libraryN;
- }
- private class ClearHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("clear");
- tf_isbn.setText("");
- tf_title.setText("");
- tf_storage.setText("");
- tf_lend.setText("");
- tf_reclaim.setText("");
- }
- }
- private class FindHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("find");
- String sBNo= tf_isbn.getText();
- Book findBn = libraryN.findBook(sBNo);
- if(findBn!=null){
- tf_title.setText(findBn.title());
- tf_storage.setText(""+findBn.storage());
- }else JOptionPane.showMessageDialog(frame, "Doesn't exists");
- }
- }
- private class LendHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("lend");
- }
- }
- private class ReclaimHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("reclaim");
- }
- }
- private class AddHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("add");
- String title = tf_title.getText();
- String isbn = tf_isbn.getText();
- int amount = Integer.valueOf(tf_storage.getText());
- Book newBook = new Book(title,isbn, amount);
- libraryN.addBook(newBook);
- tf_reclaim.setText("");
- tf_storage.setText("" + newBook.storage);
- }
- }
- private class LoadHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("load");
- try{
- ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("library.srz")));
- libraryN=(Library)in.readObject();
- in.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- private class SaveHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("save");
- try{
- ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("library.srz")));
- out.writeObject(libraryN);
- out.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement