من يعرف لبرمجة الجافا وكتابة الكود فلا يبخل
البرنامج مشروح تفصيليا ولكن اعاني من كتابة الكود حيث لم أدرس الجافا إلأ لمدة شهرين فلست بتلك المعرفة
أتمنى أن أجد الفائدة والمساعدة
بأسرع وقت ممكن
In this exercise you will write two applications, CreateTextFile (to save invoice
data) and ReadTextFile (to display invoice data saved in the file).
CreateTextFile displays a frame with four labels and four text fields for
entering invoice data: part number, description, quantity, price per item (grid
layout for this panel is ok). At the bottom of the frame there are three buttons,
"Save To File," "Cancel Item," "Exit." When the first button is clicked, the
values of invoice data are validated, written to file "invoices.txt" and the text
fields are cleared to accept a next set of data. When the second button is
clicked, the text fields are simply cleared. A click on the third button displays a
confirmation dialog that asks the user "Do you really want to exit?" If the user
clicks the No button, the dialog is removed and the user continues entering
invoice data. If the user clicks the Yes button, the application terminates.
When the user clicks the closing icon at the right-top corner of the frame, the
application ends without asking questions.
To support this functionality, CreateTextFile program includes three other
classes GUIframe, SaveHandler, and OutputFile. The frame constructor puts
the labels and text fields for entering invoice data on the top panel, and the
control buttons on the bottom panel. The labels should use a 14 pts bold
Helvetica font. Make sure that the frame cannot be resized, and experiment
with frame dimensions so that it looks nicely. Concentrate everything related
to the GUI appearance in the frame constructor, not in main. Other GUIframe
methods are setUpHandlers, clearTextFields, getTextFields.
Method setUpHandlers creates a SaveHandler object and makes it a listener
to the four text fields and to the Save button. Also, setUpHandlers adds an
anonymous listener to the Cancel button (it calls clearTextFields) and another
anonymous listener to the Exit button (it calls method closeFile of class
OutputFile). Method clearTextFields sets all four text fields to empty strings.
Method getTextFields gets (and trims) the text from four text fields and
combines it into an array of strings for further validation.
Method getTextFields is called from class SaveHandler. If any of the text
fields is empty, SaveHandler shows a message dialog saying "Please enter
data into each field" and returns so that the user can correct error(s). If the
quantity integer is invalid, it shows a message dialog that echoes the user
input and says, e.g., "Illegal integer 1.0. Try again." If quantity is not positive,
in shows a message dialog saying "Integer should be positive. Try again." If
the price per item is invalid, it shows a message dialog that says, e.g., "Illegal
value 25.O. Try again." If the price per item is not positive, SaveHandler
shows a message dialog that says "Value should be positive. Try again."
If user input passes these validation tests, a confirmation dialog displays the
invoice data for inspection and says "Please confirm writing to file." If the user
clicks the Yes button, SaveHandler calls saveToFile (it writes data to file) and
the text fields are cleared. If the answer is No, the user can further edit invoice
values in the text fields. To support these operations, SaveHandler should
receive the GUIframe and OutputFile references in its constructor (it is trivial
for GUIframe; for OutputFile, pass it to the GUIframe constructor and from
that constructor to setUpHandlers and then to SaveHandler).
Class OutputFile has a constructor and methods saveToFile and closeFile.
The constructor creates a Formatter object for the file name passed as an
argument. If the text file cannot be opened, the constructor displays a
message dialog that says, e.g., "Error creating the file invoices.txt" and after
that calls exit to end the program. Method saveToFile calls method format to
save strings (5 and 20 positions) and numeric values (8 and 12 positions) in
the text file. If the operation fails, it shows a message dialog that says "Cannot
write to file" and aborts. Method closeFile shows a confirmation dialog asking
"Do you really want to exit?" If the answer is No, only the dialog is removed
and the user can continue input. If the user clicks the Yes button, the
Formatter is closed and the exit method is called to end the program.
The main method simply creates an OutputFile and passes it to a GUIframe
object.
The second application, ReadTextFile, displays a frame with a text area (with
vertical scrollbars) at the top of the frame and the Exit button at the bottom.
When started, it opens the text file "invoices.txt" (if the operation fails, it shows
a message dialog and calls exit) and displays a header and the file data (one
invoice per line) in the text area.
To support this functionality, ReadTextFile cooperates with classes
GUIframe1 and InputFile. The GUIframe1 constructor puts visual components
on the frame and sets an anonymous listener to the Exit button to call the
closeFile method of class InputFile. Similar to CreateTextFile, make sure that
all visual elements and handled by the GUIframe1 constructor so that main
only creates the frame and the file objects. This application also should not
allow the user to change the size of the frame.
The InputFile constructor opens the text file by creating a Scanner object for
file name passed to is as at argument (make sure you use a proper Scanner
constructor). If this fails, show the message dialog that says, e.g., "Error
creating the file invoices.txt" and abort the program. The InputFile method
readFile adds to the text area the column headers for part number,
description, etc (for output, use a 14 pt bold Monospace font). In a loop,
readFile checks whether the next file record is available, reads four values
from the file (two strings, an integer, and a double) and adds them to the text
area. If anything goes wrong, a message dialog is displayed that says, e.g.,
"File invoices.txt is corrupted", the file is closed and the program ends.
Method closeFile shows a confirmation dialog that asks "Do you really want to
exit?" If the answer is No, the dialog window is simply removed. If the user
clicks on the Yes button, the Scanner is closed and the exit method is called
to terminate the program. The main method simply creates a file and a frame
object and calls readFile.
You see that class GUIframe1 needs to know about InputFile (to call
closeFile), and class InputFile needs to know about GUIframe1 (to write to the
text area). One way to make it happen is to (a) add a method getTextArea to
GUIframe1, (B) pass the InputFile object to the GUIframe1 constructor, and
© pass the GUIframe1 object to method readFile. Another way is to add a
method displayLine to GUIframe1.
Use the same naming, formatting, commenting and coding conventions as
before. Make static, final, local and private everything that can be made static,
final, local or private. Define variables to minimize their visibility. Minimize the
length of code segments in try blocks, include only statements that can throw
an exception. Consult Java documentation (and tutorials) all the time. Test
both applications.
Turn in the source code and the results of execution (crop the windows if you
use the Print Screen command). In the memo, discuss the following issues:
a) Explain how your GUI changes when you change the resolution of your
monitor.
B) Compare data validation techniques for traditional interactive input/output
and for graphical user interface. When is data validation easier to program?
When is it easier to make more complete and effective?
c) Discuss whether anonymous classes are convenient for these programs
and why.
d) Describe techniques that you used for communications between the frame
and file classes for each application.
e) Why do you think that your testing is complete?
أتمنى أن أجد الماسعدة بأسرع وقت
اردت وضع صورة ولكن لم اعرف للطريقة فحاولت ان ارسلها كملف مرفق ولا اعلم ان تظهر او لا دلوني ع الطريقة لصحيحة
دمتم