Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This project keeps track of courses offered in FAST. A user can add, edit, and delete course records. They can also view courses by category

This project keeps track of courses offered in FAST. A user can add, edit, and delete course records. They can also view courses by category (e.g. Programming, Networking) and they can search for courses by course code or with a keyword in the title.File Description The course file has the following structure: File: courses Description Data Type Comments course code String uniquely identifies a course (Primary Key) course title String the title/name of the course credit value double the credit value for this course category int the category number ** ** note for category field: In my case, I used an int to store the category number. Each number corresponds to a category, for example 1 is Business, 2 is Database, etc. The category info could be stored in a second file like this: File: categories Description Data Type Comments category id int uniquely identifies a category (Primary Key) category name String the name of the category.

Course Class

The constants CODE_SIZE, TITLE_SIZE, and RECORD_SIZE are constants needed for a random access file. You wouldn't need these if you were using a sequential file.

The second toString() method with the boolean parameter is a trick used when you want to have two different toString() methods (you'll see in the screen shots later that Course objects in the main screen's list only show the course code, whereas Course objects in the Search Matches list on the Search screen show the course code and the course title. In my second toString(), I use the boolean as a "include course title" flag - when it's true, concatenate the course title to the course code, otherwise, just return the course code like the default toString() does.

The compareTo() method is used to test and see if a second course is greater than, less than, or equal to the current course object. This is an override of the compareTo() method in the Comparable interface and it's used when I wanted to sort my CourseList course objects (this would be an extra feature to add for bonus marks or for working with a partner). If you didn't learn about the Comparable interface, you can find it in your textbook (it's pretty easy to figure out, but let me know if you need help!).

validCourseCode() is a private helper method I added to make sure the course code was valid (4 characters followed by 5 digits). This is something specific to this sample project but if you had a similar field, you'd probably want to make one or more validation methods, too.

CourseList Class

There are quite a few constructors here - one default, one that takes a variable-length argument list of Course objects (which will also accept an array of Course objects e.g. CourseList list = new CourseList(new Course(..), new Course(..), new Course(..), ...); or Course[] courses = new Course[10]; // ... fill courses with course objects blah blah blah // ... CourseList list = new CourseList(courses); The last two constructors are for loading the list from a file - one takes a File object and the other takes a String for the file name/location. The String constructor simply constructs a File object and then calls CourseList(File).

Methods like get(), set(), add(), insert(), remove(), clear(), indexOf(), and size() mostly just call the ArrayList methods by the same name.

loadFromFile() loads the CourseList with Course objects that are constructed from records in a specific file.

writeToFile() takes each Course in the CourseList, creates a record string, and writes the string to the file. This overwrites the original file contents.

findCoursesByTitle() will return a list of courses (as a CourseList) that match a specific title keyword. This is a case-insensitive comparison and does partial matches (e.g. if the search key is "ca5" then it should match "Concatenation", "Meercat", and "Cat Tails".

findCoursesByCode() will return a list of courses (as a CourseList) that match a specific course code. As with the previous, this is case-insensitive and can be a partial match.

The sortCourses() method is an extra that is not required but could get you some bonus marks. This sorts the Course objects in the CourseList. I made use of the Collections framework here, so if you'd like to give this a try, let me know and I'll give you some info on how it works.

MainUI - the main screen

The main UI contains a private CourseList data member that contains the list of courses in the data file.

In my program (see also functionality, below), I use an Open menu item in the File menu that allows the user to choose a file (via JFileChooser). Once a valid file is chosen, the CourseList data member is loaded with Course objects that are created from the records in the file. The file is then closed and not touched again until the user decides to close and save their changes or exit.

Course Class

The constants CODE_SIZE, TITLE_SIZE, and RECORD_SIZE are constants needed for a random access file. You wouldn't need these if you were using a sequential file.

The second toString() method with the boolean parameter is a trick used when you want to have two different toString() methods (you'll see in the screen shots later that Course objects in the main screen's list only show the course code, whereas Course objects in the Search Matches list on the Search screen show the course code and the course title. In my second toString(), I use the boolean as a "include course title" flag - when it's true, concatenate the course title to the course code, otherwise, just return the course code like the default toString() does.

The compareTo() method is used to test and see if a second course is greater than, less than, or equal to the current course object. This is an override of the compareTo() method in the Comparable interface and it's used when I wanted to sort my CourseList course objects (this would be an extra feature to add for bonus marks or for working with a partner). If you didn't learn about the Comparable interface, you can find it in your textbook (it's pretty easy to figure out, but let me know if you need help!).

validCourseCode() is a private helper method I added to make sure the course code was valid (4 characters followed by 5 digits). This is something specific to this sample project but if you had a similar field, you'd probably want to make one or more validation methods, too.

CourseList Class

There are quite a few constructors here - one default, one that takes a variable-length argument list of Course objects (which will also accept an array of Course objects e.g. CourseList list = new CourseList(new Course(..), new Course(..), new Course(..), ...); or Course[] courses = new Course[10]; // ... fill courses with course objects blah blah blah // ... CourseList list = new CourseList(courses); The last two constructors are for loading the list from a file - one takes a File object and the other takes a String for the file name/location. The String constructor simply constructs a File object and then calls CourseList(File).

Methods like get(), set(), add(), insert(), remove(), clear(), indexOf(), and size() mostly just call the ArrayList methods by the same name.

loadFromFile() loads the CourseList with Course objects that are constructed from records in a specific file.

writeToFile() takes each Course in the CourseList, creates a record string, and writes the string to the file. This overwrites the original file contents.

findCoursesByTitle() will return a list of courses (as a CourseList) that match a specific title keyword. This is a case-insensitive comparison and does partial matches (e.g. if the search key is "ca5" then it should match "Concatenation", "Meercat", and "Cat Tails".

findCoursesByCode() will return a list of courses (as a CourseList) that match a specific course code. As with the previous, this is case-insensitive and can be a partial match.

The sortCourses() method is an extra that is not required but could get you some bonus marks. This sorts the Course objects in the CourseList. I made use of the Collections framework here, so if you'd like to give this a try, let me know and I'll give you some info on how it works.

MainUI - the main screen

The main UI contains a private CourseList data member that contains the list of courses in the data file.

In my program (see also functionality, below), I use an Open menu item in the File menu that allows the user to choose a file (via JFileChooser). Once a valid file is chosen, the CourseList data member is loaded with Course objects that are created from the records in the file. The file is then closed and not touched again until the user decides to close and save their changes or exit.

Course Class

The constants CODE_SIZE, TITLE_SIZE, and RECORD_SIZE are constants needed for a random access file. You wouldn't need these if you were using a sequential file.

The second toString() method with the boolean parameter is a trick used when you want to have two different toString() methods (you'll see in the screen shots later that Course objects in the main screen's list only show the course code, whereas Course objects in the Search Matches list on the Search screen show the course code and the course title. In my second toString(), I use the boolean as a "include course title" flag - when it's true, concatenate the course title to the course code, otherwise, just return the course code like the default toString() does.

The compareTo() method is used to test and see if a second course is greater than, less than, or equal to the current course object. This is an override of the compareTo() method in the Comparable interface and it's used when I wanted to sort my CourseList course objects (this would be an extra feature to add for bonus marks or for working with a partner). If you didn't learn about the Comparable interface, you can find it in your textbook (it's pretty easy to figure out, but let me know if you need help!).

validCourseCode() is a private helper method I added to make sure the course code was valid (4 characters followed by 5 digits). This is something specific to this sample project but if you had a similar field, you'd probably want to make one or more validation methods, too.

CourseList Class

There are quite a few constructors here - one default, one that takes a variable-length argument list of Course objects (which will also accept an array of Course objects e.g. CourseList list = new CourseList(new Course(..), new Course(..), new Course(..), ...); or Course[] courses = new Course[10]; // ... fill courses with course objects blah blah blah // ... CourseList list = new CourseList(courses); The last two constructors are for loading the list from a file - one takes a File object and the other takes a String for the file name/location. The String constructor simply constructs a File object and then calls CourseList(File).

Methods like get(), set(), add(), insert(), remove(), clear(), indexOf(), and size() mostly just call the ArrayList methods by the same name.

loadFromFile() loads the CourseList with Course objects that are constructed from records in a specific file.

writeToFile() takes each Course in the CourseList, creates a record string, and writes the string to the file. This overwrites the original file contents.

findCoursesByTitle() will return a list of courses (as a CourseList) that match a specific title keyword. This is a case-insensitive comparison and does partial matches (e.g. if the search key is "ca5" then it should match "Concatenation", "Meercat", and "Cat Tails".

findCoursesByCode() will return a list of courses (as a CourseList) that match a specific course code. As with the previous, this is case-insensitive and can be a partial match.

The sortCourses() method is an extra that is not required but could get you some bonus marks. This sorts the Course objects in the CourseList. I made use of the Collections framework here, so if you'd like to give this a try, let me know and I'll give you some info on how it works.

MainUI - the main screen

The main UI contains a private CourseList data member that contains the list of courses in the data file.

In my program (see also functionality, below), I use an Open menu item in the File menu that allows the user to choose a file (via JFileChooser). Once a valid file is chosen, the CourseList data member is loaded with Course objects that are created from the records in the file. The file is then closed and not touched again until the user decides to close and save their changes or exit.When the user chooses File/Close or exits the program, the Course objects in the CourseList are saved as records to the same data file the user chose when they opened the file.

If the user chooses an invalid file or the user changes their mind and clicks Cancel in the file dialog, the screen goes back to its default state (see Startup, above). If the user chooses a valid file, the CourseList object is loaded with Course objects (each Course object models one record in the courses.dat file), the appropriate buttons/menu items are enabled/disabled (e.g. enable add/edit/delete buttons and menu items, disable open menu item, enable close menu item, enable Search, etc), the "-=Category=-" item is selected in the combo box (represents "all categories"), the first item is selected in the list box, and the corresponding record displayed in the fields. I also make sure that if the user chose a file that existed but was empty (no records), the edit/delete/search menu items and buttons are disabled, since all they can do is ADD new records to the empty file.

When the user chooses to close the file (perhaps they wish to close the current one and work with a different one), they use the File/Close menu item. This prompts the user to see if they want to save any changes they made to the list of Courses. If they say yes, the CourseList object's Courses are saved to the file, one Course object at a time (each Course in the CourseList is converted to a delimted string and written to the file). The file is overwritten, since the old data in the file is no longer up-to-date. Whether the user chooses to save or not, the screen is then set back to its default state (see Startup, above).

Having the Open/Close option for the file is not necessary in all applications. If you're pretty sure your user will only be using one master file in your application, you can load the file into the list on startup, and then overwrite the file on exit, without asking the user anything about the file name/location.

Clicking ADD simply enables the text fields and clears the them of any data (I set the category input drop-down to the same value as the main category drop-down), disables/enables controls so that the only thing the user can do is SAVE, CANCEL, or EXIT. I also show the hidden course code field: The course code is this file's primary key field. Therefore, the course code field is only shown and editable when a new record is being added (one generally doesn't edit a primary key). I also moved the focus to the course code field since this is where the user will want to start typing.

Clicking EDIT enables the fields but doesn't clear them since the user wants to change the existing values. I do set the focus to the Title field and have all the text selected by default, as a way to make the user's job easier. As with ADD, screen elements are enabled/disabled so that all the user can do now is SAVE, CANCEL, or EXIT.

The SAVE button works for both new and modified records. How do you know when the user is in the middle of an add or edit? One easy way is to create a private data member for the "edit mode" - it can be a boolean or an int, whatever suits you. The variable stores the current mode the user is in. I like using an int because then I can use -1 for "no mode", 0 for ADD, and 1 for EDIT. When the user clicks ADD, I set editMode to 0 and when they click EDIT I set it to 1. Then you can just test editMode in your SAVE button and perform whichever operation is appropriate. Remember to set the editMode variable back to its "no mode" or default state in the SAVE and CANCEL buttons.

When the SAVE is clicked for a new record, just add the new Course object to the end of the CourseList and the list model. When it's clicked for a modified record, replace the old Course object with the edited Course object in the CourseList and list model. (In mine, I actually just added/replaced the Course in the CourseList, and called a method that I wrote to load up the list model). Then the screen is put back into the proper default state for viewing/maintaining records.

Clicking CANCEL puts everything back the way it was before the user clicked ADD or EDIT: buttons/menu items are enabled/disabled as appropriate and the record that was being viewed before add/edit was clicked is displayed.

Always confirm deletion with the user, in case they clicked the DELETE button/menu by accident. If the user confirms, simply remove the Course object from the CourseList (and from the list model, or simply reload the list model, whichever works best).

A list of all matching records are displayed in the list box. The user can then select one of the records and click the SELECT button to take that Course object back to the main screen. They can also decide to perform a new/different search if they wish.

If there are no matches, a dialog is displayed. After the user clears the dialog, they can exit back to the main screen or perform another search.

Clicking CANCEL on the search screen simply gets rid of the Search screen and goes back to the main screen.

The lesson on using Mulitple JFrames will show you how to pass data back and forth between the forms. There are a few choices you can make here (I chose in this example to pass the entire CourseList to the search window so that the search could be performed on it; I passed the selected Course object back to the main screen when SELECT is clicked).

When the user takes a selected Course object back to the main screen, it's entry is also selected in the list box!

user if they wish to exit and, if they say yes, it then prompts the user to see if they want to save the changes they made to the file/CourseList or not. If the user confirms that they want to save their changes, the CourseList is written to the file (see Closing A File, above).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

More Books

Students also viewed these Databases questions