السلام عليكم...
انا اليوم كنت مشغول بكتابة كود،، لكن فجأه اكتشفت ان الكود صار طويل جداً و مزحوم بشكل كبير،، فانصدمت اني ما املك اي مقدرة برمجية احترافية :( ... فكنت اقول لنفسي هل الفكرة هي الأساس ؟؟ او طريقة كتابة الكود هي الأساس ؟؟ أو الكل هي الأساس ..
فقلت المفروض على الواحد انه يحاول يحسن من كتابة الكود و يلخصه باقل عدد من السطور،، فقلت خليني اكتب الكود حقي من جديد و بطريقة تختلف عن الطريقة القديمة .. ففكرت و فكرت و فكرت و بديت اكتب الكود من جديد لكن بطريقة مختصرة ،،و باسرع وقت ممكن كتبت كود جديد و باقل من الكود السابق بحوالي 60 سطر كامل :) :)
فحبيت اشاركم التجربة،،
الكود القديم:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class addBooks extends JInternalFrame {
private Connection connection = null;
private Statement statement = null;
private ResultSet resultset = null;
private JPanel northPanel = new JPanel();
private JLabel northLabel = new JLabel("Add the information for the book...");
private JPanel centerPanel = new JPanel();
private JPanel informationPanel = new JPanel();
private JLabel subjectLabel = new JLabel(" Write the Book Subject:");
private JTextField subjectTextField = new JTextField();
private String subject = null;
private JLabel titleLabel = new JLabel(" Write the title:");
private JTextField titleTextField = new JTextField();
private String title = null;
private JLabel authersLabel = new JLabel(" Write the name of the auther:");
private JTextField authersTextField = new JTextField();
private String authers = null;
private JLabel publisherLabel = new JLabel(" Write the name of the publisher:");
private JTextField publisherTextField = new JTextField();
private String publisher = null;
private JLabel copyrightLabel = new JLabel(" Write the copyright for the book:");
private JTextField copyrightTextField = new JTextField();
private String copyrightYear = null;
private JLabel editionNumberLabel = new JLabel(" Write the Edition Number:");
private JTextField editionNumberTextField = new JTextField();
private String editionNumber = null;
private JLabel pagesLabel = new JLabel(" Write the number of pages:");
private JTextField pagesTextField = new JTextField();
private String pages = null;
private JLabel ISBNLabel = new JLabel(" Write ISBN for the book:");
private JTextField ISBNTextField = new JTextField();
private String ISBN = null;
private boolean availble = true;
private JPanel insertInformationButtonPanel = new JPanel();
private JButton insertInformationButton = new JButton("Insert the Information");
private JPanel southPanel = new JPanel();
private JButton OKButton = new JButton("Exit");
public addBooks() {
super("Add Book", false, true, false, true);
Container cp = getContentPane();
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
northLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
northPanel.add(northLabel);
cp.add("North", northPanel);
subjectLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
titleLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
authersLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
publisherLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
copyrightLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
editionNumberLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
pagesLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
ISBNLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
centerPanel.setLayout(new BorderLayout());
centerPanel.setBorder(BorderFactory.createTitledBorder("Add a new book:"));
informationPanel.setLayout(new GridLayout(8,8,1,1));
informationPanel.add(subjectLabel);
informationPanel.add(subjectTextField);
informationPanel.add(titleLabel);
informationPanel.add(titleTextField);
informationPanel.add(authersLabel);
informationPanel.add(authersTextField);
informationPanel.add(publisherLabel);
informationPanel.add(publisherTextField);
informationPanel.add(copyrightLabel);
informationPanel.add(copyrightTextField);
informationPanel.add(editionNumberLabel);
informationPanel.add(editionNumberTextField);
informationPanel.add(pagesLabel);
informationPanel.add(pagesTextField);
informationPanel.add(ISBNLabel);
informationPanel.add(ISBNTextField);
centerPanel.add("Center", informationPanel);
insertInformationButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
insertInformationButton.setFont(new Font("Tahoma", Font.BOLD, 11));
insertInformationButtonPanel.add(insertInformationButton);
centerPanel.add("South", insertInformationButtonPanel);
cp.add("Center", centerPanel);
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
OKButton.setFont(new Font("Tahoma", Font.BOLD, 11));
southPanel.add(OKButton);
southPanel.setBorder(BorderFactory.createEtchedBorder());
cp.add("South", southPanel);
insertInformationButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
subject = subjectTextField.getText();
title = titleTextField.getText();
authers = authersTextField.getText();
publisher = publisherTextField.getText();
copyrightYear = copyrightTextField.getText();
editionNumber = editionNumberTextField.getText();
pages = pagesTextField.getText();
ISBN = ISBNTextField.getText();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ea) {
System.out.println(ea.toString());
}
catch(Exception e) {
System.out.println(e.toString());
}
try {
connection = DriverManager.getConnection("jdbc: odbc:JLibrary");
statement = connection.createStatement();
statement.executeUpdate("INSERT INTO Books (Subject,Title,Authers,"+
"Publisher,CopyrightYear,EditionNumber,Pages,ISBN,Availble) VALUES ('"+
subject + "','" + title + "','" + authers + "','" + publisher + "',"+
Integer.parseInt(copyrightYear) + "," + Integer.parseInt(editionNumber)+
"," + Integer.parseInt(pages) + ",'" + ISBN + "'," + availble + ")");
statement.close();
connection.close();
}
catch(SQLException SQLe) {
System.out.println(SQLe.toString());
}
subjectTextField.setText("");
titleTextField.setText("");
authersTextField.setText("");
publisherTextField.setText("");
editionNumberTextField.setText("");
copyrightTextField.setText("");
pagesTextField.setText("");
ISBNTextField.setText("");
}
});
OKButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dispose();
}
});
setVisible(true);
pack();
}
}و هذا هو الكود الجديد:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class addBooks extends JInternalFrame {
private Connection connection = null;
private Statement statement = null;
private ResultSet resultset = null;
private JPanel northPanel = new JPanel();
private JLabel northLabel = new JLabel("Add the information for the book...");
private JPanel centerPanel = new JPanel();
private JPanel informationPanel = new JPanel();
private JLabel[] informationLabel = new JLabel[8];
private String[] informaionString = {
" Write the Book Subject:"," Write the title:",
" Write the name of the auther:"," Write the name of the publisher:",
" Write the copyright for the book:"," Write the Edition Number:",
" Write the number of pages:"," Write ISBN for the book:"
};
private JTextField[] informationTextField = new JTextField[8];
private boolean availble = true;
private JPanel insertInformationButtonPanel = new JPanel();
private JButton insertInformationButton = new JButton("Insert the Information");
private JPanel southPanel = new JPanel();
private JButton OKButton = new JButton("Exit");
public addBooks() {
super("Add Book", false, true, false, true);
Container cp = getContentPane();
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
northLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
northPanel.add(northLabel);
cp.add("North", northPanel);
centerPanel.setLayout(new BorderLayout());
centerPanel.setBorder(BorderFactory.createTitledBorder("Add a new book:"));
informationPanel.setLayout(new GridLayout(8,8,1,1));
for(int i = 0; i < informationLabel.length; i++) {
informationPanel.add(informationLabel = new JLabel(informaionString));
informationLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
informationPanel.add(informationTextField = new JTextField());
}
centerPanel.add("Center", informationPanel);
insertInformationButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
insertInformationButton.setFont(new Font("Tahoma", Font.BOLD, 11));
insertInformationButtonPanel.add(insertInformationButton);
centerPanel.add("South", insertInformationButtonPanel);
cp.add("Center", centerPanel);
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
OKButton.setFont(new Font("Tahoma", Font.BOLD, 11));
southPanel.add(OKButton);
southPanel.setBorder(BorderFactory.createEtchedBorder());
cp.add("South", southPanel);
insertInformationButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String[] types = new String[8];
for(int i = 0; i < informationLabel.length; i++) {
types = informationTextField.getText();
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ea) {
System.out.println(ea.toString());
}
catch(Exception e) {
System.out.println(e.toString());
}
try {
connection = DriverManager.getConnection("jdbc: odbc:JLibrary");
statement = connection.createStatement();
statement.executeUpdate("INSERT INTO Books (Subject,Title,Authers,"+
"Publisher,CopyrightYear,EditionNumber,Pages,ISBN,Availble) VALUES ('"+
types[0] + "','" + types[1] + "','" + types[2] + "','" + types[3]+
"'," + Integer.parseInt(types[4]) + "," + Integer.parseInt(types[5])+
"," + Integer.parseInt(types[6]) + ",'" + types[7] + "'," + availble + ")");
statement.close();
connection.close();
}
catch(SQLException SQLe) {
System.out.println(SQLe.toString());
}
for(int i = 0; i < informationTextField.length; i++) {
informationTextField.setText(null);
}
}
});
OKButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dispose();
}
});
setVisible(true);
pack();
}
}