CS1003/1004 Homework #3
Due by Tuesday, March 23, at
11:00am
There is only one part to this homework assignment: a
programming portion, worth 25 points. Please be sure to
review the submission instructions
in advance.
This version is revised as of 3/6. Changes include a
-1 when indexing nAccounts in an array, a tip on
String equality, and more detail on how READMEs should be
written.
Programming assignment: bank account manager
In this assignment, you are going to write software that keeps
track of bank accounts by name and balance. We will use two
arrays -- one for people's names and one for their balances --
to keep track of this information. Note: This assignment
is the first "substantial" programming assignment you've
received. My test implementations run about 100 lines of code,
without comments. Most of it is very straightforward, but you
must make sure you don't start at the last minute! Also,
read this assignment carefully before you start anything,
as there are some fine points that need to be considered.
As I'll talk about in class, make sure to do this assignment one
piece at a time. You'll find it much easier to debug individual
pieces than the whole thing as a whole. Also, some parts of
this implementation will differ for C and Java; I'll go out of
my way to explain those where appropriate. If you want to test
your individual procedures without a UI, make direct calls with
hardcoded data to the appropriate procedures in the constructor
(Java) or the main function (C). You can also put debugging
statements in, but ultimately, only parts #4, #5, and #6 should
have any "real" print statements (the rest just do internal
computation).
Here's an example run of my version:
$ (command to start your program)
Welcome to Janak's bank account program
Enter one of the following commands:
--> d to deposit money in an account
--> w to withdraw money from an account
--> t to print out a total of all the assets in the bank
--> l to list all of the accounts
--> q to quit
> w
Enter the name of the person: janak
Enter the amount to withdraw: 100
Could not withdraw from account -- not found
> d
Enter the name of the person: janak
Enter the amount to deposit: 200
> d
Enter the name of the person: john
Enter the amount to deposit: 300
> w
Enter the name of the person: janak
Enter the amount to withdraw: 100
> l
janak 100.000000
john 300.000000
> t
Total amount is 400.000000
> q
$
Implement the following in a file called bank.c or
Bank.java, appropriately:
-
(4 points) Set up the data structures for this assignment.
You'll need three components: a list of names (array of
Strings), a list of balances (array of doubles), and an
integer (that I refer to as nAccounts) that keeps track
of the number of accounts. For simplicity's sake, you
can hardcode the maximum number of accounts (e.g., the length
of the name and balance arrays) to be 100. The number of
accounts, at startup, should be zero.
-
Java: Make these instance-level variables
(i.e., under the class declaration outside of methods, as
opposed to local variables inside methods). By doing so,
all methods in the class can access these variables
directly instead of having to pass them in as parameters.
-
C: Make these global variables. Doing so is
very simple: put them outside of any functions, at the top,
right below the include statements. By doing so, all
functions in the program can access them without having to
have them named in the function declaration.
Additionally, in order to store a set of strings, you must
declare a two-dimensional array; for example, char
names[100][100] declares 100 100-character strings,
which serves our purpose. Each individual string is then
referred to as names[i], and works perfectly with
C string functions.
-
(6 points) Write the deposit and withdraw
procedures. Both of them take two parameters: a String
corresponding to the name, and a double corresponding to the
size of the deposit/withdrawal.
The deposit procedure will perform a linear search through the
currently-defined accounts (e.g., names[0] through
names[nAccounts-1]) looking for this person. If it's
found, the corresponding balance (e.g., if names[i]
is the name of the person, balances[i] is the
corresponding balance) will be updated by the amount
deposited. If it's not found, a new record should be added at
the end (if there is space) with the name and the
deposit amount as the new initial balance. (Make sure to
update nAccounts when you do so!)
The withdraw procedure will do almost the same thing (in fact,
I recommend you start the withdraw procedure by copying the
deposit procedure). There are two major differences: first,
if you find the person, subtract the amount (incidentally,
negative balances are OK), instead of adding to it; and
second, if the person is not found, don't try to add them --
just fail. Note that withdraw will never actually try to
remove someone from the database -- that's a little trickier
and is beyond the scope of this assignment.
-
Java: Have these methods return a boolean.
Deposit will return true if an existing account was
updated or if a new account was added; a false would imply
the database is full. Withdraw will return true if an
existing account was updated, or false otherwise. Also,
when comparing strings, make sure to use the
.equals() method to test for lexicographical
equality (e.g., a.equals(b), not
==).
-
C: Have these functions return an int.
Deposit will return 1 if an existing account was updated
or if a new account was added; a 0 would imply the
database is full. Withdraw will return 1 if an existing
account was updated, or 0 otherwise. Also, when comparing
strings, make sure to use the strcmp() function
to test for lexicographical equality (e.g., !strcmp(a,
b), not ==).
- (3 points) Write the computeTotal procedure. This
procedure also takes no parameters, but returns a double --
the total sum of the balances in the bank.
- (3 points) Write the listAccounts procedure. This
procedure takes no parameters and returns no parameters, and
simply goes through the entire list of accounts and prints out
each name and balance, one-at-a-time.
-
(7 points) Write the user interface for this program.
We will employ an interactive user interface, i.e., no
command-line -- instead, all input will be done after
the program has been started. The user interface will prompt
the user to enter one of five commands: d (for
deposit), w (for withdrawal), l (for listing
all of the accounts), t (for displaying the total
value of the bank), and q (to quit). It should do
this repeatedly until the user wants to quit.
For deposit and withdrawal, the user interface must prompt the
user for the name and the amount of the deposit/withdrawal,
and then it should call the appropriate procedure. If the
procedure returns a 0 back (signaling an error), it should
also print out an error message.
See the above example to get an idea of how the user interface
might work.
-
Java: I recommend that you implement this user
interface in the object's constructor, not the main
method -- so that the instance variables as declared in
step #1 are accessible. Also, you should use the
BufferedReader mechanisms as described in lab #5 to
provide interactive user input. BufferedReader reads content
into Strings, which you can then test to determine what
commands the user entered.
-
C: I recommend that you implement this user
interface in the main() function (or, should you
prefer, in its own dedicated function; both are
requivalent). You should use fgets() to read in
the strings. Note that fgets keeps the newline
character, and you must remove it as per pages 68-69 in
Practical C Programming for your interface to work
perfectly.
- (2 points) Write the printInstructions procedure.
This procedure also takes and returns no parameters, and just
prints out the commands useable in the UI (a sample is shown in
the running above). Finally, modify the user interface as
described above to run this procedure when the program starts,
as well as whenever the user types in invalid input.
As previously mentioned, make sure to comment your code. Also,
you should include a README text file. This is a brief,
separate file that specifies the following: your name and the
homework #, a sentence or two outlining what your program does
and how to run it, known limitations or bugs, and anything else
you deem important. We'll review a README file in labs as well
right before break.