COMP274
Week 6 Programming Assignment
The Calendar Program
The purpose of this lab is to give you a chance to use some of the data stream tools we have been discussing in a simple application. The assignment is to write a calendar application which allows the user to select a date, and either retrieve a previously stored calendar entry, or save a calendar entry.
Your program should present a GUI interface which allows the user to specify the month, day, and year of the calendar entry. The GUI should also have a text area for displaying and editing a particular entry. It will also need two buttons, one for saving an entry, and the other for retrieving an entry.
Required program elements:
1. Your user interface must allow the user to enter the month, day, and year. You can do this using text fields for input, or you can use ComboBoxes if you feel adventurous.
2. The only GUI components which create events that your program needs to handle are the save and retrieve buttons.
3. Don’t go overboard making your GUI beautiful! We are just looking for basic functionality here!
4. You must have a separate class which manages the calendar data. You will have a minimum of three classes in your application, a user interface class, the calendar manager class, and a calendar test class. The user interface class creates an instance of the calendar manager in its constructor and stores it in a member variable.
5. The calendar manager must provide methods which support saving a specific calendar entry and retrieving a specific calendar entry. The interfaces must be defined to only pass a single day’s calendar entry across the interface.
6. The calendar manager must store calendar data into files according to month+year. That is, the calendar entries for December 2011 must all be stored in the same file.
7. The calendar manager must use ObjectInput/OutputStreams to read/write data from/to files. The calendar manager will use an array to store String objects. The position of a String in this array corresponds to the calendar entry for a specific day.
8. The save method of the calendar manager will need to determine if a file exists for the requested month and year. If so, the object from that file must be read into the calendar manager. Otherwise, the calendar manager must create an empty String array. The new entry must be saved to the appropriate day’s location in the array. The modified array must be saved to the appropriate file.
9. The retrieve method of the calendar manager will need to determine if a file exists for the requested month and year. If not, return an error string indicating that there is no such entry. If the file exists, read the String array from the file and locate the requested day’s entry. If this entry is null, return an error string indicating that there is no such entry, otherwise return the entry.
Take screen shots of the output of your program. Paste the screen shots and your source code for your programs into a Word document. Submit your Word document.
The Calendar Program
Pseudo Code for the User Interface
public class UserInterface extends JFrame implements ActionListener
{
Declare member variables for:
CalendarManager
JComboBoxes or JLabels/JTextFields for month and day
JLabel / JTextField for year
JTextArea / JScrollPane for entry content
JButtons for get and save entry
public UserInterface()
{
Set the layout manager for the JFrame
Create the CalendarManager object
Create the day, month, year GUI components
Create the entry content area
Create the get and save buttons
Add your event handler object to the get and save buttons
Add the GUI components to the JFrame
}
public void actionPerformed(ActionEvent ev)
{
Declare string variables for the month, day, year, and entry content
Get the user input from the month, day, year, and entry content GUI
components into these local string variables
if the get button was clicked, call the calendar manager’s getEntry method
passing in the month, day, and year strings and get back a result
else the save button was pressed, so call the calendar manager’s saveEntry
method passing in the month, day, year, and entry content and get
back a result
Set the entry content GUI component to the result from the method call
}
}
Pseudo Code for the CalendarManager
public class CalendarManager
{
Declare member variables for the base directory and an array of 31 strings
public CalendarManager( )
{
Set the base directory where the calendar manager’s data files will reside
Create a file object for the base directory and make sure that the directory
exists, and if it doesn’t, use the mkdir method on the file object to create it.
}
private void getFile(String month, String year)
{
This method trys to get the string array from the specified file
Formulate the filename from the base directory, month, and year – make sure you
use double backslashes after the base directory
Create a file object using the filename
If the file does not exist, create a new array of 31 strings for the member
variable
else create a FileInputStream and ObjectInputStream on the file and use the
readObject method of the object input stream to get an array of
strings from the file – remember to cast the value returned by the
readObject method to a String [ ] and assign it to the string array
member variable. Close the streams.
If an exception occurs, create a new array of 31 strings for the
member variable
}
public String getEntry(String month, String day, String year)
{
Call the getFile method to try to get the string array from the file
specified by the month and year.
Convert the day string to an integer
Remember, days go from 1 to 31, while the string array contains entries 0
thru 30
Get the entry from the string array member variable
If it is not null, return that string
Else return an error string indicating that the entry does not exist
}
public String saveEntry(String month, String day, String year, String entry)
{
Call the getFile method to try to get the string array from the file
specified by the month and year.
Convert the day string to an integer
Remember, days go from 1 to 31, while the string array contains entries 0
thru 30
Save the entry to the string array member variable
Create a FileOutputStream object using the base directory, month, and year.
Create an ObjectOutputStream object on top of the file output stream.
Use the writeObject method to write the string array member variable to the
file.
Close the object stream and the file stream object.
If everything goes well, return a string indicating success.
If there was an exception, return a string indicating an error occurred.
}
}
Pseudo Code for the Calendar Test
public class CalendarTest
{
public static void main(String[] args)
{
Create a UserInterface object
Set the size of the GUI, and make it visible.
Set the default close operation
}
}