Course web page: Introduction to Computer Science for non-majors II James Tam Return to the course web page

CPSC 219: Assignment 4

Due April 6 at 4 PM

New concepts to be applied for this assignment:

 

  • Implementing a graphical user interface
  • File input (using FileReader and BufferedReader) and output (using FileWriter and PrintWriter)
  • Exceptions

  • Description:

    Implement a simple graphical user interface to simulate a shopping program. When the program is run a login dialog will be presented to the user (use the JPasswordField class as the input component). The password will be stored in an 'encrypted' format in a file called "password.txt". (The original password in the [link to sample file in the 219 course directory] is the string "password", the encryption employs the Caesar cipher which shifted each character backward one place in the alphabet ('s' becomes 'r', 'p' becomes 'o' and 'a' wraps around to 'z'). The encrypted version thus becomes "ozrrvnqc". Your program must be able to use any arbitrary password file. However you can assume that the file will only consist of lower case alphabetic characters.

    Your program must read the encrypted password and convert it to the original form ("ozrrvnqc" read from file becomes "password"). It will then compare the unencrypted string vs. the current value entered in the password field when either: (1) the user hits the login button or (2) the cursor focus is in the password field (as shown in Figure 1A) and the user hits enter. If there is no match then the program should display a suitable error message as well as the number of attempts and the maximum number allowed. If three unsuccessful attempts have been made then a suitable error message should be provided (e.g., Figure 1B for an example) and the program will end. Note that the program will need to 'pause' so the user actually has time to see the error message before it shuts down.

    The container for the login dialog need not be a JFrame, indeed in such a case something like a JDialog is probably more appropriate. (If you use the latter control then you may need to end the program with the call to the 'exit()' method via System.exit(<int>). Clicking on the close window of the login dialog (or the main shopping window (shown in Figure 2) will also end the program although without an error message).

    Figure 1A: Login dialog when incorrect credentials entered
     
    Figure 1B: Max attempts reached

    If the password has been correctly entered then the login window is banished and the main shopping screen comes up. You have some flexibility in the exact content and layout, an example is shown in Figure 2.

    Figure 2: Main shopping screen

    At a minimum this window should consist of the following: a JFrame container, a JTextField and JTextArea for the name and address input areas (respectively), Jlabels to describe the input fields, two JButtons to handle most of the user interaction. The full version of the program will employ the GridBagLayout (and GridBagConstraints) for the main shopping window (it's optional for the login dialog). Regardless of whether a layout 'manager class' is used the layout should be organized and logical so the user can quickly understand the interface at a glance. If you desire you can use other sub-containers with other layout manager classes. ImageIcons will be used to adorn the buttons (your choice of image but it should complement the text descriptions).  Finally you should include another image somewhere in the container with an appropriate company logo ("TAMCO Enterprises Inc." in the example) contained with a Java GUI Component (a JLabel is a good choice). There should be an appropriate descriptive title in the window. If you use external data - something that you didn't create yourself - unless it's largely in the public domain (e.g., clipart that is made universally available for free) make sure that you cite the source. The program should be written so that the data files (password file and images) will reside in the same directory as your program (so your marker doesn't have to waste time determining the structure assumed...those few minutes adds up over the course of marking all the assignments in a tutorial).

    How the interactive widgets work

    1. Save button: When pressed this button will take the current information in the name and address and save it to a plain text file (unencrypted) called 'order.txt' which should be saved in the same directory as your program. The name information should sit by itself on the first line of the file while the second line (and successive lines - if appropriate) will consist of the address. Each line of the address from the JTextArea will reside on a separate line in the order file (the example shown in Figure 2 will create a order file with 4 files: 1 for the name and 3 for the address). If you are still unsure then you should look at the sample order file 'order.txt' in the A4 directory. It's the result of saving the order shown in Figure 2. The program does not have to read this information from the file. If the file already exists then the previous contents can be overwritten. During the save process the title bar of the main window should provide an appropriate status message (and the program should temporarily pause itself to provide the user with enough time to read it).

    2. Clear button: When pressed the two input fields (name and address should be cleared of text).

    As previously indicated clicking on the close window control will just shut the application down. Because the address field can consist of multiple lines (i.e., the user can hit enter to separate lines) hitting enter in the name field should have no effect (unlike the password field).

    Data files

    In the assignment directory (/home/219/assignments/assignment4/data) there will be sample images that you can use in your program, a sample 'order.txt' file and two versions of the password file (one is encrypted the other is in plaintext format). You may also find there a short video illustrating how my sample solution works.

     
    Video: Running my sample solution (".wmv" format)

    Using pre-written Java code

    Unlike the other assignments you will make extensive use of the pre-created Java libraries developed by Sun/Oracle for graphical components (e.g., java.awt., javax.swing), event handling (e.g., java.awt.event) and the four classes for file input and output.

    Submitting your work:

    1. Assignments (source code/'dot-java' files and the class diagram) must be electronically submitted according to [the assignment submission requirements] using [D2L]. Since you can submit multiple times in D2L it may be very confusing for your TA if you submit things piecemeal via multiple submissions i.e., the marker may mark your work with the wrong version of a file if he or she doesn't note latest submission date. Consequently each time that you submit your work you should submit every relevant file (it may make it easier if you bundle them all using the 'zip' compression utility before you upload them to D2L.
    2. As a reminder, you are not allowed to work in groups for this class. Copying the work of another student will be regarded as academic misconduct (cheating).  For additional details about what is and is not okay for this class please refer to the following [link].
    3. Before you submit your assignment to help make sure that you haven't missed anything here is a [checklist] of items to be used in marking