Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Book {
- private int storage;
- private String isbnNo;
- private String title;
- private static int counter = 1000;
- public Book(String title, int initStorage){
- this.title = title;
- this.isbnNo = isbnNo;
- this.storage = storage;
- }
- public void reclaim(int amount){
- this.storage += amount;
- }
- public void lend(int amount){
- if(storage > amount){
- this.storage -= amount;
- }
- }
- public String isbnNo(){
- return isbnNo;
- }
- public String title(){
- return title;
- }
- public int storage(){
- return storage;
- }
- public String toString(){
- return "title: " + title + "\n"+
- "isbnNo: " + isbnNo + "\n"+
- "storage: " + storage + "\n" +
- "-------------------";
- }
- }
- import java.util.*;
- public class Library2 {
- private final ArrayList books = new ArrayList();
- public void addBook(Book b){
- books.add(b);
- }
- public Book findBook(String isbnNo){
- for(int i=0 ; i<books.size() ; i++){
- Book temp = (Book)books.get(i);
- if(temp.isbnNo()==isbnNo){
- return temp;
- }
- }
- return null;
- }
- }
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Gui {
- private JFrame frame = new JFrame("Budget library");
- private JPanel background, centerPanel,southPanel,menuBar;
- private JLabel l_isbnNo,l_title,l_reclaim,l_lend,l_storage;
- private JTextField tf_isbnNo, tf_title, tf_reclaim,tf_lend,tf_storage;
- private JButton b_find,b_reclaim,b_lend,b_clear,b_addBook;
- private Library2 library;
- private JMenuBar baren;
- private JMenu menu;
- private JMenuItem i_item,i_isbnNo,i_reclaim,i_lend,i_clear,i_find;
- private Book findB;
- public Gui(Library2 l){
- library = l;
- background = new JPanel();
- background.setLayout(new BorderLayout());
- centerPanel = new JPanel();
- // background.setBorder(BorderFactory.addBookLineBorder(Color.RED,9, false));
- centerPanel.setLayout((new GridLayout(5,2)));
- southPanel = new JPanel();
- southPanel.setLayout((new GridLayout(3,2)));
- l_isbnNo = new JLabel("isbnNo: ");
- l_title = new JLabel("title:");
- l_reclaim = new JLabel ("reclaim:");
- l_lend = new JLabel("lend:");
- l_storage = new JLabel("storage:");
- b_find = new JButton("FIND");
- b_reclaim = new JButton("Return");
- b_lend = new JButton("Lend");
- b_clear = new JButton("Clear");
- b_addBook = new JButton("Add book");
- southPanel.add(b_reclaim);
- southPanel.add(b_find);
- southPanel.add(b_lend);
- southPanel.add(b_clear);
- southPanel.add(b_addBook);
- tf_isbnNo= 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_isbnNo);
- centerPanel.add(tf_isbnNo);
- centerPanel.add(l_title);
- centerPanel.add(tf_title);
- centerPanel.add(l_storage);
- centerPanel.add(tf_storage);
- centerPanel.add(l_reclaim);
- centerPanel.add(tf_reclaim);
- centerPanel.add(l_lend);
- centerPanel.add(tf_lend);
- background.add(centerPanel, BorderLayout.CENTER);
- background.add(southPanel, BorderLayout.SOUTH);
- frame.setContentPane(background);
- frame.pack();
- frame.setVisible(true);
- ClearHandler cl = new ClearHandler();
- FindHandler fh = new FindHandler();
- this.b_clear.addActionListener(cl);
- this.i_clear.addActionListener(cl);
- this.b_lend.addActionListener(new LD());
- this.b_reclaim.addActionListener(new RM());
- this.b_find.addActionListener(fh);
- this.i_find.addActionListener(fh);
- this.b_addBook.addActionListener(new AddBook());
- this.library = library;
- }
- private class ClearHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- tf_isbnNo.setText("");
- tf_title.setText("");
- tf_reclaim.setText("");
- tf_lend.setText("");
- tf_storage.setText("");
- }
- }
- private class LD implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- String sLend = tf_lend.getText();
- int amount = Int.parseInt(sLend);
- findB.lend(amount);
- tf_storage.setText("" + findB.storage());
- }
- }
- private class RM implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- String sReclaim = tf_reclaim.getText();
- int amount = Int.parseInt(sReclaim);
- findB.reclaim(amount);
- tf_storage.setText("" + findB.storage());
- System.out.println("Return" + amount);
- } }
- private class FindHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- String sIsbnNo = tf_isbnNo.getText();
- try{
- }catch (NumberFormatException nfe){
- JOptionPane.showMessageDialog(frame, "skriv in siffror som isbnNummer");
- return;}
- Book findB = library.findBook(sIsbnNo);
- if(findB!=null){
- tf_title.setText(findB.title());
- tf_storage.setText(""+findB.storage());
- }else JOptionPane.showMessageDialog(frame, "boken finns inte! ");
- }
- }
- private class AddBook implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("add Book");
- String title = tf_title.getText();
- String isbnNo = tf_isbnNo.getText();
- int storage = Int.parseInt(tf_reclaim.getText());
- Book addBook = new Book(title, amount);
- tf_isbnNo.setText("" + addBook.isbnNo());
- library.addBook(addBook);
- tf_reclaim.setText("");
- tf_storage.setText("" + addBook.storage());
- }
- }
- }
- public class LibrarySystem {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Book b1 = new Book ("Wikipedia Vol 1", 5);
- Book b2 = new Book ("Linas lina", 2);
- Book b3 = new Book ("Bortom vind och horisont", 1);
- Library2 library = new Library2();
- library.addBook(b1);
- library.addBook(b2);
- library.addBook(b3);
- System.out.println(library.findBook(1001));
- System.out.println(library.findBook(1002));
- System.out.println(library.findBook(1003));
- Gui gui = new Gui(library);
- } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement