vendredi 1 janvier 2016

Unit testing in java

I have to add tests for this java program. But, i don't understand for what could i use tests in such a program. I don't really see any benefits of using tests because it is not a program like finding if a number is or isn't prime.

package utcn;

import javax.swing.*;
import java.sql.*;
import java.awt.event.*;

public class BorrowBook extends JFrame implements ActionListener {
    /**
     * title will be the label for "Enter a book title" message
     */
    JLabel title;
    /**
     * ttitle will be the field for introducing the title
     */
    JTextField ttitle;
    /**
     * btn_borrow is the button for borrowing a book
     */
    JButton btn_borrow;

    /**
     * This method will create a window for borrowing a book.
     */
    public BorrowBook() {
        super("BorrowBook");
        title = new JLabel("Enter a book title:");
        title.setBounds(20, 20, 200, 15);
        ttitle = new JTextField(20);
        ttitle.setBounds(130, 20, 220, 30);
        btn_borrow = new JButton("BorrowBook");
        btn_borrow.setBounds(220, 65, 100, 40);
        btn_borrow.addActionListener(this);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setSize(500, 150);

        setLayout(null);
        add(btn_borrow);

        add(title);
        add(ttitle);
    }

    /**
     * This method will be called when the button is pressed. The application
     * require for an book title. If the introduced title can be find in the
     * database, it will be displayed a success message, otherwise an error
     * message. Also, in the database, the nr_exempare will be decreased and the
     * nr_imprumuturi will be increased.
     */
    @Override
    public void actionPerformed(ActionEvent ex) {
        Connection conn = null;
        PreparedStatement pst = null;
        PreparedStatement pst1 = null;
        ResultSet rs = null;
        String title = ttitle.getText();
        conn = MySqlConnect.ConnectDB();
        try {
            pst = conn.prepareStatement("update carti set nr_exemplare=nr_exemplare-1 where nume_carte=? ");
            pst1 = conn.prepareStatement("update carti set nr_imprumuturi=nr_imprumuturi+1 where nume_carte=? ");
            pst.setString(1, ttitle.getText());
            pst1.setString(1, ttitle.getText());
            int i = pst.executeUpdate();
            int i1 = pst1.executeUpdate();
            if ((i > 0) && (i1 > 0)) {
                dispose();
                JOptionPane.showMessageDialog(null, "Your book has been borrowed!");
            } else {
                JOptionPane.showMessageDialog(null, "Invalid book title.", "Accse Denied", JOptionPane.ERROR_MESSAGE);

            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

    }
}

Aucun commentaire:

Enregistrer un commentaire