الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

تجربة و حبيت اشارك فيها

مغلق
بدأه صلاح الثبيتي في 10 يوليو 2003 · 3 رد · 512 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم...

انا اليوم كنت مشغول بكتابة كود،، لكن فجأه اكتشفت ان الكود صار طويل جداً و مزحوم بشكل كبير،، فانصدمت اني ما املك اي مقدرة برمجية احترافية :( ... فكنت اقول لنفسي هل الفكرة هي الأساس ؟؟ او طريقة كتابة الكود هي الأساس ؟؟ أو الكل هي الأساس ..

فقلت المفروض على الواحد انه يحاول يحسن من كتابة الكود و يلخصه باقل عدد من السطور،، فقلت خليني اكتب الكود حقي من جديد و بطريقة تختلف عن الطريقة القديمة .. ففكرت و فكرت و فكرت و بديت اكتب الكود من جديد لكن بطريقة مختصرة ،،و باسرع وقت ممكن كتبت كود جديد و باقل من الكود السابق بحوالي 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();
	}
}
#2

يا صلاح حاول تتعلم ان تفصل بين التعامل مع الداتابيس والواجهة فلو افترضنا انك سوف تعمل 20:30 dialog فهل في كل مرة تفتح ربط مع الداتا بيس .

بل اعمل class مع الداتابيس منفصلة ثم ارسل اليها بارامتر واحصل على النتائج من هذه الكلاس لمساعدتك توجد هذه الكلاس في الأرشيف

فرضا انك تريد تحويل برنامجك بحيث يعتمد على Client server structur ففيهذه الحالة لن يعمل برنامجك لأن النت قد لا يسمح بهذا الربط

الكلاس التي ترتبط مع الداتا بيس يجب ان تكون في السيرفر والواجهة تعمل في الكلينت

اللهم انا نسألك علما نافعا ونعوذ بك من حماقة الجهلاء
#3

قصدك مثل كذا ؟؟؟

الملف الاول بس واجهه

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

public class searchBooks extends JInternalFrame {
	private JPanel northPanel = new JPanel();
	private JLabel title = new JLabel("Search");

	private JPanel westPanel = new JPanel();
	private JLabel search = new JLabel("  Search by:");
	private JComboBox searchType;
	private String[] types = {"Title", "Authers", "Publisher", "ISBN"};
	private JLabel key = new JLabel("  Write the KeyWord:");
	private JTextField keyTextField = new JTextField();

	private JPanel southPanel = new JPanel();
	private JPanel searchButtonPanel = new JPanel();
	private JPanel cancelButtonPanel = new JPanel();
	private JButton searchButton;
	private JButton cancelButton = new JButton("Cancel");

	private listSearchBooks listSearching;

	public String getSearchType() {
		return searchType.getSelectedItem().toString();
	}
	public String getKeyTextField() {
		return "'%" + keyTextField.getText() + "%'";
	}
	public searchBooks() {
		super("Search Books", false, true, false, true);

		Container cp = getContentPane();

		northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		title.setFont(new Font("Tahoma", Font.PLAIN, 16));
		northPanel.add(title);
		cp.add("North", northPanel);

		westPanel.setLayout(new GridLayout(2,2,1,1));
		westPanel.add(search);
		westPanel.add(searchType = new JComboBox(types));
		westPanel.add(key);
		westPanel.add(keyTextField);
		cp.add("West", westPanel);

		southPanel.setLayout(new BorderLayout());
		searchButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		searchButtonPanel.add(searchButton = new JButton("Search"));
		cancelButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		cancelButtonPanel.add(cancelButton);
		southPanel.add("North", searchButtonPanel);
		southPanel.add("Center",cancelButtonPanel);
		cp.add("South", southPanel);

		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				listSearching = new listSearchBooks("SELECT * FROM Books WHERE "+getSearchType()+" LIKE "+getKeyTextField());
				getParent().add(listSearching);
				try {
					listSearching.setSelected(true);
				}
				catch(java.beans.PropertyVetoException e) {}
				dispose();
			}
		});
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				dispose();
			}
		});
		setVisible(true);
		pack();
	}
}

الملف الثاني قاعدة البيانات

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

public class listSearchBooks extends JInternalFrame {
	private Connection connection = null;
	private Statement statement = null;
	private ResultSet resultset = null;

	private Vector data = new Vector();
	private Vector head = new Vector();
	private Vector oneRow;

	private JPanel panel = new JPanel();
	private JLabel label;
	private JTable table;
	private TableColumn column = null;
	private JScrollPane scrollPane;

	public listSearchBooks(String query) {
		super("List the Searching Books", false, true, false, true);
		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:Library");
			statement = connection.createStatement();
			resultset = statement.executeQuery(query);
			while(resultset.next()) {
				oneRow = new Vector();
				oneRow.add(resultset.getString("title"));
				oneRow.add(resultset.getString("authers"));
				oneRow.add(resultset.getString("publisher"));
				oneRow.add(resultset.getString("editionNumber"));
				oneRow.add(resultset.getString("copyRightYear"));
				oneRow.add(resultset.getString("ISBN"));
				data.add(oneRow);
			}
			resultset.close();
			statement.close();
			connection.close();
		}
		catch(SQLException sqle) {
			System.out.println(sqle.toString());
		}
		head.add("Title");
		head.add("Authers");
		head.add("Publisher");
		head.add("Edition");
		head.add("Copyright");
		head.add("ISBN");
		table = new JTable(data, head);
		table.setPreferredScrollableViewportSize(new Dimension(600, 100));
		table.setFont(new Font("Tahoma", Font.PLAIN, 12));
		scrollPane = new JScrollPane(table);

		for(int i = 0; i < 6; i++) {
			column = table.getColumnModel().getColumn(i);
			if(i == 0)
				column.setPreferredWidth(200);
			if(i == 1)
				column.setPreferredWidth(100);
			if(i == 2)
				column.setPreferredWidth(100);
			if(i == 3)
				column.setPreferredWidth(100);
			if(i == 4)
				column.setPreferredWidth(100);
			if(i == 5)
				column.setPreferredWidth(120);
		}
		label = new JLabel(" * The List For All Books In The Library:");
		label.setFont(new Font("Tahoma", Font.PLAIN, 15));
		getContentPane().add("Center", panel);
		getContentPane().add("North", label);
		panel.setLayout(new BorderLayout());
		panel.add(scrollPane, BorderLayout.CENTER);

		setVisible(true);
		pack();
	}
}

هل هذا هو قصدك ؟؟

#4

I guess it is not so bad to have a file with so much code, if it was readable and extensible. Furthermore, you can subdivide it into smaller files.

Readability is one of the most important carachtarestics a good code of a good programmer must have.

هذا الموضوع مغلق.

مواضيع مشابهة