Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package librarysystem;
- /**
- *
- * @author Lowe
- */
- public class LibrarySystem {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Book b1 = new Book("Tures äventyr", 4500);
- Book b2 = new Book("Solskensdalen", 42332);
- Book b3 = new Book("Wikipedia vol 1", 42342);
- Library LibraryN =new Library();
- LibraryN.addBook(b1);
- LibraryN.addBook(b2);
- LibraryN.addBook(b3);
- System.out.println(LibraryN.findBook(1002).title());
- Gui gui=new Gui(LibraryN);
- }
- }
- package librarysystem;
- /**
- *
- * @author Lowe
- */
- import java.util.*;
- public class Library {
- private 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;
- }
- }
- package librarysystem;
- /**
- *
- * @author Lowe
- */
- public class Book {
- private int storage;
- private String isbnNo;
- private String title;
- private static int counter = 1000;
- public Book(String title, double initBalance){
- this.title = title;
- this.storage = initStorage;
- }
- public void rebound(double amount){
- this.storage += amount;
- }
- public int storage(){
- return storage;}
- public void withdraw( double amount){
- if(storage >= amount){
- this.storage -= amount;
- }
- }
- public String isbnNo(){
- return isbnNo;
- }
- public String title(){
- return title;
- }
- public String toString(){
- return "name: " + holder + "\n " + "accountNo:" + accountNo + "\n " + "balance: " + balance +"\n";
- }
- }
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Gui {
- private JFrame frame = new JFrame("Library");
- private JPanel background, centerPanel,southPanel;
- private JLabel l_isbnNo,l_title,l_rebound,l_lend,l_storage;
- private JTextField tf_isbnNo, tf_title, tf_rebound,tf_lend,tf_storage;
- private JButton b_find,b_rebound,b_lend,b_clear,b_addBook;
- private Book findBook;
- private Library libraryL;
- public Gui(Library l){
- LibraryL = l;
- background = new JPanel();
- background.setLayout(new BorderLayout());
- centerPanel = new JPanel();
- background.setBorder(BorderFactory.createLineBorder(Color.RED,9, false));
- centerPanel.setLayout((new GridLayout(5,2)));
- southPanel = new JPanel();
- southPanel.setLayout((new GridLayout(3,2)));
- l_isbnNo = new JLabel("accountNo: ");
- l_title = new JLabel("name:");
- l_rebound = new JLabel ("deposit:");
- l_lend = new JLabel("withdraw:");
- l_storage = new JLabel("balance:");
- b_find = new JButton("FIND");
- b_rebound = new JButton("Deposit");
- b_lend = new JButton("Witdraw");
- b_clear = new JButton("CLEAR");
- b_addBook = new JButton("CREATE");
- southPanel.add(b_rebound);
- 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_rebound = 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_rebound);
- centerPanel.add(tf_rebound);
- 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();
- FH fh = new FH();
- this.b_clear.addActionListener(cl);
- this.b_lend.addActionListener(new WH());
- this.b_rebound.addActionListener(new DH());
- this.b_find.addActionListener(fh);
- this.b_addBook.addActionListener(new Create());
- this.Library = libraryL;
- }
- private class ClearHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- tf_isbnNo.setText("");
- tf_title.setText("");
- tf_rebound.setText("");
- tf_lend.setText("");
- tf_storage.setText("");
- }
- }
- //private class DepositHandler implements ActionListener{
- //public void actionePerformed(ActionEvent ae){
- // double amount = Double.parseDouble(tf_deposit.getText());
- //foundAcc.deposit(amount);
- //tf_balance.setText(""+ foundAcc.balance)
- // System.out.println("deposit");
- //}
- //}
- private class WH implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("");
- String sWithdraw = tf_lend.getText();
- double amount = Double.parseDouble(sWithdraw);
- findBook.withdraw(amount);
- tf_storage.setText("" + findBook.storage());
- }
- }
- private class DH implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("");
- String sRebound = tf_rebound.getText();
- double amount = Double.parseDouble(sRebound);
- findBook.rebound(amount);
- tf_storage.setText("" + findBook.storage());
- System.out.println("Deposit" + amount);
- }
- // private class FL implements ActionListener{
- // public void actionPerformed(ActionEvent ae){
- // String accNo = tf_accountNo.getText();
- // foundAcc = banken.findAccount(Integer.parseInt(accNo));
- // String holder = founcAcc.holder();
- //double balance = foundAcc.holder();
- // tf_balance.setText(""+balance);
- //}
- }
- private class FH implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("");
- String sIsbnNo = tf_isbnNo.getText();
- int accNo = 0;
- try{
- accNo = Integer.parseInt(sIsbnNo);
- }catch (NumberFormatException nfe){
- JOptionPane.showMessageDialog(frame, "skriv in siffror som kontonummer");
- return;}
- Book findBook = libraryL.findBook(isbnNo);
- if(findBook!=null){
- tf_title.setText(findBook.title());
- tf_storage.setText(""+findBook.storage());
- }else JOptionPane.showMessageDialog(frame, "kontot finns inte! ");
- }
- }
- private class AddBook implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("Create");
- String title = tf_title.getText();
- double amount = Double.parseDouble(tf_rebound.getText());
- Book create = new Book(title, storage);
- tf_isbnNo.setText("" + create.isbnNo());
- libraryL.addBook(create);
- tf_rebound.setText("");
- tf_storage.setText("" + create.storage());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement