Assignment #4, CS 49J, Section 1

due October 4, 2007
70 points

 

In this assignment you are to modify the BankAccount and BankAccountTester classes from the class web site, as described below. In particular, you are to arrange that instances of the BankAccount class are password protected. This class is essentially the class of Chapter 3 of the text.

Specifically, each instance of your BankAccount class is to have a password associated with it. This password is to be represented as a String. The class is to have public methods deposit, withdraw, and getBalance that work as in the given class, except that they take as an extra parameter a password String from the user, and perform the requested operation if and only if the password that the user submits matches the account's password. In the case of mismatch, the first two methods are not to change the balance, and the third is to return 0. All three methods are to print an informative error message if the operation is not performed. The withdraw method should also print an error message in case the amount to be withdrawn exceeds the account balance. In this case it is to set the balance to zero (that is, it is to treat the request as a request to withdraw the entire balance).

Your class is to have two constructors that work the same as the given constructors, except that in both cases they initialize the password to the String "CS 49C", print a message informing the user that this is the initial password, request the user to change this password immediately, and print a message describing what a legal password looks like (as described in the next paragraph).

Your are two have two additional public methods for this class. The first is a method printPasswordDescription() that prints a description of what a legal password looks like (and returns nothing). The second is a boolean-valued method changePassword that takes Strings representing the old password and a proposed new password, and changes the password if the first argument matches the current password and the second argument is a legal password String. If either of these conditions fails, an informative error message is to be printed and the value false returned. Otherwise the value true is to be returned. A legal password is a String with at least 6 characters, at least one of which is an upper-case letter, at least one of which is a digit, and at least one of which is neither a letter nor a digit. Recall that the String and Character classes provide methods for checking such properties of strings and characters.

You are strongly encouraged to use a private method to check whether a proposed password is legal. In fact, to simplify the definition of this method, it might even be wise to have private auxiliary methods for this method. You may define as many additional private methods as you find useful.

Don't forget to modify the documentation of the old methods and constructors of this class so that it accurately reflects their new behavior.

The BankAccountTester class is to have two static methods newAccountDialog and oldAccountDialog. The first of these methods is to construct and return a new account (with an initial balance obtained interactively from the user) and then change the account password (with the old password and new password obtained interactively from the user). Note that this method needs to be prepared for incorrect values to be entered by the user -- perhaps repeatedly.

The oldAccountDialog method is to take a BankAccount as argument, and allow the user to interact with the account, using its public methods. More precisely, the user is to be allowed to select the deposit method by typing d, the withdraw method by typing w, the getBalance method by typing g, and the the changePassword method by typing p. The user is also allowed to quit the dialog by typing q. Any other choice is to be ignored.

Regardless of whether the user makes a legal or illegal choice, the user is to be continued to be prompted for choices until the q option is selected. In case of an illegal choice, an appropriate error message is to be printed. In the case of a legal choice other than q, the arguments for the corresponding messages (i.e., methods) are to be obtained interactively from the user.

All input to be obtained from a user in these two BankAccountTester methods should be prompted for by appropriate prompt strings. Observe once again that whenever a user is prompted for a password, it's good to remind the user what a legal password looks like.

As with the BankAccount class, you may define any additional private methods that you find useful. You should still use javadoc for your documentation of both private and public methods.

There are a number of tests that would be made in a realistic bank account simulator that you needn't make for this assignment. You needn't check for nonnumeric or negative user input. You may assume that a user's choice of methods uses lower case (so you may treat capital letter input as an error). You may ignore the possibility of null references passed as arguments to the BankAccount methods. Also in a real application, new passwords would likely be requested twice and errors would not be handled simply by printing error messages; you may ignore these issues.

Even with these simplifications, this assignment will be considerably more time consuming than previous assignments, so please budget your time accordingly.

For this assignment, you are to turn in hard and soft copies of your class definitions, as well as hard copies of the results of your testing (of the BankAccountTester class). The values you should use while testing are given in three files, inputData1.data, inputData2.data, and inputData3.data, all available on the class web site. You may perform the corresponding tests interactively by typing in this data, or by using input redirection as described on page 279 of the text. You may turn in your two class definitions as separate text attachments to an email message.