Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Library {
- private final ArrayList books = new ArrayList();
- /** Metoden addAcconut tar emot ett konto och lägger in det i sist i samlingen
- *
- * @param b
- */
- public void addBook(Book b){
- books.add(b);
- }
- /**Metoden findAccount söker efter ett konto m.a.p kontonummer
- * metoden returnerar första kontot med korrekt kontonumer
- * finnns dubletter. Metoden returnerar null om inget konto finns
- * @param isbnNo
- * @return
- */
- 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;
- }
- }
- 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", "234234234",0001);
- Book b2 = new Book("Solskensdalen", "12313123",0002);
- Book b3 = new Book("Wikipedia vol 1", "123123123",0003);
- Library libraryN =new Library();
- libraryN.addBook(b1);
- libraryN.addBook(b2);
- libraryN.addBook(b3);
- System.out.println(libraryN.findBook(0001));
- System.out.println(libraryN.findBook(0002));
- System.out.println(libraryN.findBook(0003));
- Gui gui=new Gui(libraryN);
- }
- }
- public class Book {
- private int storage;
- private String isbnNo;
- private String title;
- private static int counter = 0001;
- public Book(String holder, String initStorage){
- this.title = title;
- this.storage = initStorage;
- isbnNo = ++counter;
- }
- public void deposit(double amount){
- this.storage += amount;
- }
- public void lend(double amount){
- if(storage > amount){
- this.storage -= amount;
- }
- }
- public int isbnNo(){
- return isbnNo;
- }
- public String title(){
- return title;
- }
- public double storage(){
- return storage;
- }
- public String toString(){
- return "title: " + title + "\n"+
- "isbnNo: " + isbnNo + "\n"+
- "balance: " + storage + "\n" +
- "-------------------";
- }
- }
- 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_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 Library libraryL;
- private JMenuBar baren;
- private JMenu menu;
- private JMenuItem i_item,i_isbnNo,i_rebound,i_lend,i_clear,i_find;
- private Book findBook;
- public Gui(Library L){
- libraryL = L;
- //this.banken = banken;
- baren = new JMenuBar();
- menu = new JMenu("file");
- i_item = new JMenuItem("close");
- i_clear = new JMenuItem("clear");
- i_find = new JMenuItem("find");
- menu.add(i_clear);
- baren.add(menu);
- menu.add(i_item);
- menu.add(i_find);
- this.frame.setJMenuBar(baren);
- 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("WITHDRAW");
- 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();
- FindHandler fh = new FindHandler();
- this.b_clear.addActionListener(cl);
- this.i_clear.addActionListener(cl);
- this.b_lend.addActionListener(new WH());
- this.b_rebound.addActionListener(new DH());
- this.b_find.addActionListener(fh);
- this.i_find.addActionListener(fh);
- this.b_addBook.addActionListener(new addBook());
- this.libraryL = 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 WH implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("hej");
- String sLend = tf_lend.getText();
- double amount = Double.parseDouble(sLend);
- findB.lend(amount);
- tf_storage.setText("" + findB.storage());
- }
- }
- private class DH implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("hej");
- String sRebound = tf_rebound.getText();
- double amount = Double.parseDouble(sRebound);
- findB.deposit(amount);
- tf_storage.setText("" + findB.storage());
- System.out.println("Deposit" + amount);
- // double amount = Double.parseDouble (tf_deposit.getText());
- // findAcc.deposit(23);
- // tf_balance.setText("" + findAcc.balance());
- } }
- private class FindHandler implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("hej");
- String sIsNo = tf_isbnNo.getText();
- int isNo = 0;
- try{
- isNo = Integer.parseInt(sIsNo);
- }catch (NumberFormatException nfe){
- JOptionPane.showMessageDialog(frame, "skriv in siffror som kontonummer");
- return;}
- Book findB = libraryL.findBook(isNo);
- if(findB!=null){
- tf_title.setText(findB.title());
- tf_storage.setText(""+findB.storage());
- }else JOptionPane.showMessageDialog(frame, "kontot finns ej! ");
- }
- }
- private class addBook implements ActionListener{
- public void actionPerformed(ActionEvent ae){
- System.out.println("CREATE");
- String holder = tf_title.getText();
- double amount = Double.parseDouble(tf_rebound.getText());
- Book addBook = new Book(title, amount);
- tf_isbnNo.setText("" + addBook.isbnNo());
- library.addAccount(addBook);
- tf_rebound.setText("");
- tf_storage.setText("" + addBook.storage());
- }
- } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement