CSE 11 Programming assignment #3


Here's a description of the third programming assignment for CSE 11. In this assignment, you will get more experience with object-oriented programming by designing, implementing, and testing Java classes.

>>> Due deadline: ...

>>> Required solution files: Person.java Account.java ElevenDollar.java

This means that the files that contain your solution to the assignment must be turned in by the due deadline. Use the bundleP3 script to turn in your assignment.

It's a good idea to plan to do the assignment well before the deadline; terminals and tutors can get very busy at the last minute.

In doing this assignment, it's your responsibility to understand the course rules for integrity of scholarship.

1: Background

Make sure you've read in the textbook up through chapter 5. In addition, have a look at this link before continuing: http://uazu.net/money/lietaer.html.

Money is not a neutral concept--it can be both positive, as in Japan's fureai kippu, and negative, as some think the dollar has become. In this project we will create a currency completely online with a program, and attempt to make use of it.

Currency, as is noted in the above link, is based on trust. When you pay someone dollars for a gallon of milk ($3.95, the last time I bought one), they trust the dollar. They believe that someone else will give them goods and services for that same dollar. They place faith in the dollar, and without this faith the currency is useless. No amount of counterfeit-proofing or security measures can force someone to believe in the dollar--they must do so on their own to make it valuable.

The CSE 11 class will be creating a currency called the 11-dollar, similar to the time-dollar. We will set the value of this currency by telling you what to expect from it, and how much faith you can put into it. An 11-dollar is worth a favor from a fellow CS11 student. It is worth making someone a peanut-butter and jelly sandwich, or xeroxing a copy of lecture notes. It is worth an hour of essay proofreading, or baking a batch of brownies for someone who's heading to the labs. It is approximately worth the same as an hour of your time.

You will not be forced to use this currency--if someone wants your time from you and wants to pay you in 11-dollars, you are not forced to accept. But if you do, someone else may wish to reciprocate in the future, when they have some time to spare.

We hope that the nature of this currency will promote its use, and that an 11-dollar may one day be preferred over an ordinary dollar. This is because when a dollar is spent, it has traveled through a thousand anonymous hands. But when an 11-dollar is spent, it is only through favors to other students. For those of you that have seen the movie "Pay It Forward," this is a method to promote favors without making it appear like charity. Instead it has more similar to an equal trade.

No money will be printed--it's quite difficult to create physical currency that could avoid fraud. Instead a database will hold the accounts of all CS11 students. In this project you will write a program to access this database, and keep records of your 11-dollars.

The class will provide a server on the sunpal machines to handle accounts of each student. Accounts will be seeded with ten 11-dollars each, to represent the ten hours of spare time we expect you to have after this class is through with you. ;) Staff will have accounts as well, but may not charge for duties they would ordinarily perform. They are already being paid for their efforts in ordinary dollars.

A few caveats before we get into the project itself:

  1. Don't do anything silly. That is, please use your own judgment when using 11-dollars. Don't drive to Tijuana for any number of 11-dollars. Don't buy copious amounts of alcohol for 11-dollars. Don't lend your brand-new Mercedes to someone for 11-dollars. Instead, start small. I suggest everyone's first expenditure/earning should be edible. Offer to make cookies for your friends for an 11-dollar. Give things that involve your time or things you can make--the goal is to have 11-dollars fill the niche of things you can't buy with ordinary dollars.
  2. This doesn't change the integrity of scholarship rules. Don't use the 11-dollar to have someone write your code. And likewise, don't perform a favor for someone that involves writing their code for them.
  3. At the same time, have some fun with this. This currency is to build community, not competition. It's about encouraging people to make use of their time, by assigning it a value. If you want a makefile tutorial, for instance, but don't have time for to read up about it, post on the webboard that you're willing to pay 11-dollars if someone could write you a handy something-or-other. If you need someone to review concepts with you after class, feel free to ask someone. Draw a picture for 11-dollars. Write a story. Sub for someone in a intervarsity game. Make a few greeting cards. Host an anime-watching night. Teach someone how to DDR. Or Lindy-hop. Or break-dance. Or make cheesecakes.
  4. Don't be shy in both offering and taking people up on their offers. You have to try it at least to test your code. And who knows what might become of this in the future.

2: The Project

The usual details apply. Create a P3 directory in your home directory where you'll keep all your files related to this project. When finished use bundleP3 to submit.

This assignment, like the previous two, require your classes and methods to be implemented according to given specifications. Make sure your method signatures match those given.

When finished, your program will function as this sample run describes. User input is in bold italics ; program output is in plain teletype font.

[cs11w2@sunpal] java ElevenDollar
Welcome to the 11-dollar banking system
A)Retrieve personal information
B)Update your personal information
C)Check your balance
D)Transfer money to another account
E)Check your transaction history
X)Exit
A
Which user?(Leave blank to retrieve your own)
cs11w
Nickname:The Professor
Secret Password:*SECRET*
Welcome to the 11-dollar banking system
A)Retrieve personal information
B)Update your personal information
C)Check your balance
D)Transfer money to another account
E)Check your transaction history
X)Exit
A
Which user?(Leave blank to retrieve your own)

Nickname:SomeStudent
Secret Password:11takes_time
Welcome to the 11-dollar banking system
A)Retrieve personal information
B)Update your personal information
C)Check your balance
D)Transfer money to another account
E)Check your transaction history
X)Exit
B
New Nickname?(default:SomeStudent)
SupercoolStudent
New Password?(default:11takes_time)
11isgr8
UPDATE CONFIRMED
Welcome to the 11-dollar banking system
A)Retrieve personal information
B)Update your personal information
C)Check your balance
D)Transfer money to another account
E)Check your transaction history
X)Exit
C
You have $10.00
Welcome to the 11-dollar banking system
A)Retrieve personal information
B)Update your personal information
C)Check your balance
D)Transfer money to another account
E)Check your transaction history
X)Exit
D
To which account will you transfer?
cs11w
How much money?
.20
Optional comment?
Professor Tax
TRANSACTION CONFIRMED
Welcome to the 11-dollar banking system
A)Retrieve personal information
B)Update your personal information
C)Check your balance
D)Transfer money to another account
E)Check your transaction history
X)Exit
C
You have $9.80
Welcome to the 11-dollar banking system
A)Retrieve personal information
B)Update your personal information
C)Check your balance
D)Transfer money to another account
E)Check your transaction history
X)Exit
E
Sun Jan 18 22:43:59 PST 2004    cs11w2  cs11w 20      Professor Tax

Welcome to the 11-dollar banking system
A)Retrieve personal information
B)Update your personal information
C)Check your balance
D)Transfer money to another account
E)Check your transaction history
X)Exit
X

If everything was implemented correctly, the owner of account cs11w should be .20 11-dollars (that is, 20 11-cents) richer.

For each menu choice, your program will communicate to the central server, sending and receiving information about your account. Communication will performed through Strings, which are sent and received through a single method in a class ElevenClient. This class will be given to you--through the magic of abstraction you will need no knowledge of the details of how the server or the network is implemented.

3: ElevenClient

ElevenClient.class is in the public/P3 directory, and is written for you. It contains one method, which has the following signature:
public static String sendMessage(String msg)
Call this method with a String you wish to communicate to the server. It will return the server's response as another String. The format for the argument is: a command, followed by zero or more arguments separated by a tab character ('\t'). The following is a table of Strings the server will understand, and their expected replies.

Command Format Description Server Reply
BALANCE requests your balance BALANCE\t[amount]
TRANSFER\t[username]\t[amount]\t[comment] give money to another user TRANSACTION CONFIRMED
GETINFO get your personal information USERINFO\t[nickname]\t[password]
GETINFO\t[username] get another user's info. Note that because this user isn't you, you don't get their password. "USERINFO\t[nickname]\t*SECRET*"
UPDATEINFO\t[nickname]\t[password] update your stored information "UPDATE CONFIRMED"
GET_HISTORY Retrieve your transaction history. [timestamp]\t[from]\t[to]\t[amount]\t[comment]

Some notes:

4: Person.java

We'll be putting the concept of objects to use here. You'll be representing yourself as an object, and keep your personal information in this class. This will be a convenient way to keep multiple pieces of information together. This is also a flexible way of organizing state--should you need to know more or less about a Person, you can modify this class.

For this class you will be defining two constructors:

public Person(String nickname, String password)
Stores the nickname and password in instance variables.

public Person(String netString)
The netString expected is of the format "[username]\t[password]". Parses this string and stores the username and password in instance variables.

There will be the usual complement of accessor and mutator methods to retrieve and modify the values:
public String getNickname()

Returns the nickname

public String getPassword()
Returns the password

public void setNickname(String nickname)
Sets the nickname to the String passed in

public void setPassword(String password)
Sets the password to the String passed in

There will also be an equals method to compare with other objects:
public boolean equals (Object other)
Checks to see if the object passed in is a Person, and has the same nickname and password as the Person this was called on. We won't be using the equals method, but it is good practice to implement one for data types in case you need to compare them in the future.

There will be two methods to convert Persons into Strings. The first is called toString(), which is a special method name in Java--whenever any object is printed with System.out.print() or System.out.println(), its toString() method is called, and the String it returns is printed. In this way objects can be printed like other ordinary data types.
public String toString()
Returns a string of "Nickname:" followed by the nickname, a newline, and then "Password:" followed by the password. (No added spaces!) Suitable for printing to the screen.

The second is a netString() method, which prints the Person data in a format similar to messages you will be sending over the network.
public String netString()
Returns a concatenation of the nickname followed by a tab and then the password. Useful for certain network commands, particularly UPDATEINFO.

Dividing the problem up in this manner has additional benefits besides flexibility--you can test your Person object by itself, and should you add or modify methods later, it will be very easy to find and fix your bugs. It's a good idea to add a main function and perform a simple test of your Person class before moving on.

5: Account.java

An account represents a user's bank account on the server. All communication with the server occurs in this class.

An account needs information stored in a Person to function, so it makes sense to store a Person inside an Account. As for methods, there are three things you can do to an account--namely, update the user information, check its balance, and transfer money to another account.

Make sure this Person has protected visibility. This means in the future, if a subclass needs to access this Person, it may do so. Name this variable "myIdentity", as follows.

private Person myIdentity;

Your Account class will hold one Person object in it, one constructor, one accessor, and five methods.

Constructor:

public Account()
Create an new account. Initialize the stored person to null.

public Person getPerson()
Return the stored Person. ElevenDollar will use this to modify portions of the stored Person before calling updateInformation(). Methods:
public Person getInformation()
Sends a GETINFO request to the server. Parses the reply and creates a new Person object with the name and password initialized. Returns this new Person object.

public Person getInformation(String username)
Sends a GETINFO request to the server with one argument. Parses the reply and returns a Person object. Note that the password replied by the server will be "*SECRET*"

public String updateInformation()
Sends an UPDATEINFO request to the server, using the stored Person for information to be transmitted.

public double checkBalance()
Sends a BALANCE request to the server. It returns the balance, modified to have two decimal places. If the server returns ERROR, then checkBalance()'s results are undefined. That is, your program can do whatever it wants in that case.

public String transferToAcct(String user, double amount, String comment)
Sends a TRANSFER request to the server, requesting amount funds to be transferred to the user. The comment part of the request will be stored in the server's database. The server's reply is returned. Remember that you must convert amount to a format that ElevenClient will understand--that is, an amount of 4.92 should be converted to 492 before placed in the message string.

Again, when finished with this class, attempt to test out its features with a main method. I suggest you print out the Strings at first, instead of sending them to ElevenClient. This way you can make sure they match the format suggested, without throwing random things at the server.

6: ElevenDollar.java

Now that you have these two pieces, a third class will provide a simple user interface to them. The algorithm will be simple--Create an Account object, and then display a menu and use a switch construct very similar to FiWizard in P1. See the sample run for an example of what it should do. A template will be provided in the public/P3 directory to save some typing, and provide some hints as to what structure it should have.

7: Hints

  1. Start early. Or at least, try not to start late.
  2. Read the README twice. Often times questions are answered in the README, and reading it twice may reduce any misconceptions you might have about the assignment.
  3. Test your program by transferring money to a classmate, and asking them to transfer it back. You may also transfer money to yourself, although your balance won't change. Remember you must be logged in to a sunpal machine, either physically or through SSH for ElevenClient to send messages appropriately.
  4. Much of your work will go into String construction and modification. If you've never seen the online documentation for Strings, take a look--it has a lot of useful features. The java documentation can be found at http://java.sun.com/j2se/1.4.2/docs/api/. Look for the String class in the java.lang package.
  5. Helper functions will save you a lot of hassle. Consider creating a getNth(int n) method that, given any string with tabs in it, returns the pieces in between the nth and n+1th tab. Also consider creating a trimToken command that can remove the first word from a server's reply String. When a simple method like this is debugged, you can use it without hesitation to make other methods easier.
  6. Note that Account.java handles all communication with the server and ElevenDollar.java handles all input from the user. Ensure that this is so! If you're putting a sendMessage in ElevenDollar or a readLine in Account, it's in the wrong spot.
  7. This is in some respects a good design. If, for instance, we wanted to run a batch of transactions from a file, we could simply replace ElevenDollar. If on the other hand, we wanted to run on a different server, we could replace Account.java. Modularity like this improves both readability and reusability.

8:FAQ

What's the password good for? It doesn't seem to be checked anywhere.
As some of you may have heard, P3 and P4 are linked projects--that is, code from P3 will be used in P4. The password will become useful for something in P4, although its importance will remain limited.

I transferred all my money to someone on accident! What do I do now?
Be careful with the numbers you're sending around--1000 in the amount field is all ten 11-dollars you're seeded with. If you make a mistake, ask them (politely) if they can give it back, or ask a TA/tutor to loan you some money to test your program with.

What does readLine() return if I just hit enter?
readLine() will return the empty string, "", which you can compare to other strings with the .equals method.

Should I use my social security number, credit card number, PIN number, or anything important for my password?
No. I actually suggest making it whimsical or funny as opposed to secure, since you can always retrieve it and you can always change it, and it doesn't protect all that much.

What if a program generates an Account and calls updateInformation before getInformation?
You will almost certainly get a NullPointerException, which is okay for this assignment. Error handling will also be covered in P4.

Grading of assignment #3

There are 20 possible points for this assignment, broken down as follows:
a.   4 points for your Person class

b. 12 points for your Account class

c. 4 points for your ElevenDollar class


In each case, doing a part of the assignment means that your code compiles, runs correctly, satisfies the specifications, is well-written and commented.