Lab 5

weaverchurchΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 2 μήνες)

206 εμφανίσεις

Due: October 24
th
, 2011

CENG 310


Lab 5: GUI for a
Flat File
Database

1.

Create a JFrame form

(6

marks)


a.


In the JFrame display each of the fields of a
n

Address

record in separate JTextfields.

Use a checkbox for the
visited

field and a
comboBox
for the street type. Edit the
model

property of the combobox and fill in your list of street types.


b.

Underneath the fields add the following JButtons: First, Previous, Next, Last

c.

Underneath the fields add the f
ollowing buttons: New, Delete

d.

Add 2 mor
e JTextFields. The first will display the current record number, the 2
nd

will
display the
total number of records.

e.

Add JLabels next to your text fields

f.

Add a MenuBar to the JFrame. Then add a JMenu and to the JMenu add the following
MenuItems: Load, Sa
ve,Separator, Quit

g.

Re
name and re
label all your fields, menu items and buttons to make them developer
friendly.

h.

Use NetBeans to g
enerate
actionPerformed

methods for each of the buttons.


i.

Demonstrate your GUI in the lab.


The design might look like thi
s, but you don’t have to match the placement or text
exactly.





2.

Creating the underlying Model

(10 marks)


It is considered a

good idea to separate the GUI from the underlying data. That way we can
change each one independently of the

other.


Create a class Address
DB with a member variable
s:


Addres
[] db=new
Address
[600]
;

int currentRecord;

int nRecs; //Number of records stored in db


You may add other fields to this class if you feel that they are necessary.

Add member methods to do the follo
wing:


a.

first()
-

returns the zeroth

Address

in db and sets the currentRecord to 0.

b.

last()
-

returns the last
Address

in db and sets the currentRecord to the index of the last
record.

c.

previous() returns the previous record in db

d.

next()


returns the next

record in db

e.

add(
Address

p)
-

adds a

record to db at the current position

f.

delete()


removes the current record from db.

g.

Write predicate methods (methods that return true or false) hasNext() and
hasPrevious() that return true or false if there is a next
(or previous) record.

h.

load(fileName)
-

Load the contents of a specified file containing passwords into
db.

i.

save(fileName)


Save the contents of db into a specified file.


3.

Documentation

(4 marks)


a.

Using the special
/**

comment, Add JavaDoc comments to all your methods

in the
Address class

using the tags
@param
,
@return
. At the top of the note that NetBeans
has already added JavaDoc tags for @author.
Add the tag @version after @author.
Also add plain JavaDoc descr
iptions above each of the member variables.


b.

In the actionPerformed methods that you’ve generated, customize the
TODO

comments.


4.

Creating the underlying Test Data

(10

marks)


a.

Generate a CSV file

of 500 addresses
.

You’ll use this for final testing.

(1)


b.

Generate the framework for JUnit tests for each of the methods in the class
Address
DB.

To do this select
Tools

|

Create JUnit Tests



c.

C
reate
one
Address
DB as a member variable of the
Address
DBTest class.



Manually create 5
Address
s by using a full
constructor (one where you explicitly set each
field)
and
then
add them to the

setup
()

method inside

Address
DB
Test class
.

Use this
AddressDB instead of creating a new
instance

for each test. This will give you a fresh
copy for each test.


d.

Creating th
e test cases

for the each of the 10 methods in
Address
DB. Run your tests,
summarize the results and hand in a screen shot showing the output.


Keep in mind that you created the test data. Therefore if you start at the 0th
Address

in
your array,
next()

should return the 1
st

element,
hasNext()

should be true,
hasPrevious()
should be false
. You can try loading a file with a known number of
records


your test could be a check of the number of records loaded along with an
output of those records. You could then test the
save()
function by saving the file to
another name on the disk, the
n having your code compare the length of the original file
to the new file and checking that it exists.


None of the tests should require that much code. If you want to add additional separate
tests, copy one of the routines.



5.

Connecting the GUI with
the
Address
DB

(15

marks
/2 each except for h
)


In the class you created for your GUI:


a.

Initializing the database in the GUI



In the constructor create a new
Address
DB and initialize it with some records and
retrieve the 1
st

record in your constructor.

H
ow you do this is up to you, but it will be
easier if you create about 5 simple records that are easy to remember


this will make it
easier to debug any problems.

If all your
Address
DB routines are working you should be
able to use them. If not you can

reuse the code you used in the
setup
()

method in
Address
DBTest.


b.

Create a member method that takes a
n

Address

and copies the values into the display
fields.


You’ll reuse this method a lot.


c.

For each of the 4 navigation buttons, call the appropriate ro
utine and display the record.
number.

When a record is being viewed any of the editable fields may be changed.
When you move off a record, set the fields of the
Address

to whatever is currently in the
display.
(We will discuss when it is appropriate
to save changes in class.)


When you’ve reached the last record, disable the
Next
button. If you move to a button
that is not the last record, enable it. This prevents the user from navigating beyond the
last button. Implement similar logic for the
Previous

button.


d.

Implement the
Last
and
First
buttons.


e.

When the user presses the
New
button clear all the fields in the GUI display and create a
new
Address
. Set the Issue Date to the current date and time and display it.
The user
then has to fill
in the 3 remaining text fields and optionally check off the valid
Address

checkbox.


When you’ve filled in

the fields, click on the
Add

button. Update the # of records in the
display.


In lecture on the 17
th

we will discuss checking for empty fiel
ds. Our strategy will be to
disable the Add button until all fields contain some value.
Don’t check

for duplicates. (2)


f.

When the users presses the
Delete
button, the current record will be deleted. In the
special case of there being no records left,
all buttons except the
Add
button will be
disabled.


g.

In the lecture on the 17
th

we will discuss the JFileChooser class and the showOpenDialog
and showSaveDialog methods. (Section 34.9). Modify the actionPerformed methods of
the Load and Save menu item
s to use these to prompt the user for a file name and then
load or save the database in memory to a file.


h.

Implement the Quit menu item. Keep track if the database h
a
s been saved using a
“dirty” flag. If not then save it. (1 mark)


When this works w
ith your small 5 record database, test with your larger 500 record
sample.


6.


Final Report.

(5 marks)



a.

A Time Log in units no smaller than 15 minutes, no larger than half an hour. List the
total time spent at the bottom.

b.

A status report on each of
steps 1
-
5.

c.

A listing of your AddressDB class and your JUnit test class

d.

A listing of the member variables of your Address class. (I don’t need the entire class)

e.

A listing of
actionPerformed
or
keyTyped
methods that you generated and editted in
your GUI, a
long with a listing of the declaration of the member fields.


Keep in mind I will be reading your TODO comments and JavaDoc tags to verify that
you’ve used these features.


7.

Demo’s


a.

Demonstrate your GUI either in the week of Oct 10
th

or 17
th

b.

Demonstrate you

final program on the week of Oct 24
th
. Your program will be checked
against your status report.