CMSC 206 (Data Structures)

Assignment#2

Part 1 (100 pts) due:   September 22, 2013 11:59:59pm
Part 2 (100 pts) due:   September 29, 2013 11:59:59pm

The goal of this assignment is to develop your ability to use control flow statements to make a user menu and do object-oriented programming, focusing specifically on inheritance and code reuse.  

Introduction

In this assignment, you will write a program that allows a user to repeatedly create and manage express card accounts. 

Each express card is like a debit card, except that it can only be used to purchase meals. A new express card account starts at a balance of $0.  The user can (1) deposit money to the express card, increasing the balance (by depositing more money), (2) purchase meals at a fixed rate, thus decreasing the balance, and (3) have meals ( each swipe of the card reduces the remaining number of meals by one).  
There are two types of express card accounts: a student express account or a faculty express account. All express account have fields accountNumber, accountBalance, numberOfMeals, pricePerMeal, baseAmtForBonus and accountTypeName. The field baseAmtForBonus is used to record the bonus cashback for an account when making a deposit. This is a base amount one-time deposit (baseAmtForBonus) to receipt bonus which is the minimum amount of money for each deposit. For example, if the baseAmtForBonus for a student is $500 and deposit $499 for a student account will not receive any bonus. The criteria of receiving bonus for these two types of express accounts are different:

  • The baseAmtForBonus is set to $500.0 for a student express account and to $0.0 for a faculty express account in the corresponding constructor.
  • A student express account has extra two variables for bonus: rewardLevel and rewardAmt. For every deposit that is greater than the baseAmtForBonus, for each rewardLevel, a student account receives rewardAmt amount of money. For example, if rewardLevel is set to 200.0 and rewardAmt is set to 2.0, then a deposit of $500 to a $0 balance student express account will result $4.0 bonus and a new balance of $504.0. However, a deposit of $499 to a $100 balance student express account will not obtain any bonus and final balance is $599.0.
    The variable rewardLevel should be set to $200.0 and rewardAmt to $2.0. While we won't change them in this assignment, they must be variables.
  • A faculty express account has only one extra variable for bonus: rewardPct, the percentage of deposit that will become the bonus. In this assignment, it is set to 0.01. For example, since the baseAmtForBonus is $0.0 for a faculty, a $50 deposit to a $0 balance faculty express account will result $0.5 bonus and a new balance of $50.5.
Moreover, pricePerMeal for a faculty account is reduced to $8.0 rather than $10.0 (which is for a student).

To begin, download CS206Assignment2.jar. This (jar) file is a completed version of the assignment that you can study. 
To run this completed program, use the command java -jar CS206Assignment2.jar

Here's an example trace of the program running once the assignment is complete.

Welcome to CS206 Fall'13 Express Account Company

MAIN MENU
1.) Create a new account
2.) Log into an existing account
3.) Exit the banking system
Please enter your selection: 1

CHOOSE THE TYPE FOR THE NEW ACCOUNT
1.) Student express account
2.) Faculty express account
Please enter your selection: 1
Created new Student Express account #0, balance: $0.0, number of meals: 0

STUDENT EXPRESS ACCOUNT #0, BALANCE: $0.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 1
Enter deposit amount: 600
Received bonus of $6.0
Deposit $600.0 New balance $606.0

STUDENT EXPRESS ACCOUNT #0, BALANCE: $606.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 2
Enter the number of meals you want to purchase: 1
Purchased 1 meals with $10.0 per meal New balance $596.0

STUDENT EXPRESS ACCOUNT #0, BALANCE: $596.0, NUMBER OF MEALS: 1
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 3

STUDENT EXPRESS ACCOUNT #0, BALANCE: $596.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 3
No meals left on your account. Please purchase meals first.

STUDENT EXPRESS ACCOUNT #0, BALANCE: $596.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 1
Enter deposit amount: 0
The deposit must be a positive amount.

STUDENT EXPRESS ACCOUNT #0, BALANCE: $596.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 2
Enter the number of meals you want to purchase: 60
Not enough balance for 60 meals
Purchased 59 meals, New balance $6.0

STUDENT EXPRESS ACCOUNT #0, BALANCE: $6.0, NUMBER OF MEALS: 59
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 4
Goodbye!

MAIN MENU
1.) Create a new account
2.) Log into an existing account
3.) Exit the system
Please enter your selection: 1

CHOOSE THE TYPE FOR THE NEW ACCOUNT
1.) Student express account
2.) Faculty express account
Please enter your selection: 2
Created new Faculty Express account #1, balance: $0.0, number of meals: 0

FACULTY EXPRESS ACCOUNT #1, BALANCE: $0.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 1
Enter deposit amount: 80
Received bonus of $0.8
Deposit $80.0 New balance $80.8

FACULTY EXPRESS ACCOUNT #1, BALANCE: $80.8, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 2
Enter the number of meals you want to purchase: 3
Purchased 3 meals with $8.0 per meal New balance $56.8

FACULTY EXPRESS ACCOUNT #1, BALANCE: $56.8, NUMBER OF MEALS: 3
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 4
Goodbye!

MAIN MENU
1.) Create a new account
2.) Log into an existing account
3.) Exit the system
Please enter your selection: 3
Exiting the system


Part 1 (100 pts) Implementing a user menu and a class (ExpressAccount)

In this part, you are going to use control flow statements to make a user menu with only one general express account type. The following is the program requirements for this simplified version of the general express account class.

  • Implement a class ExpressAccount that includes the following attributes and methods at a minimum (you can add others of your own design):
    • Private Variables:  accountNumber, accountBalance, numberOfMeals, pricePerMeal.
      • The pricePerMeal should be set to $10.0.  While we won't change them in this assignment, they must be variables.
    • A constructor that takes in one integer for the accountNumber, and initializes the accountNumber to the given value.
    • Accessors (i.e., getters) for all private variables needed outside the class with appropriate public (or protected) access levels.
    • A toString() function that outputs a textual description of the account that includes the account number, balance and number of meals left.
    • The following three methods:
      • void deposit(double amount) which takes in an amount to deposit the account.  The method should output a simple status message to System.out, such as "Deposit $40 to Account #101, new balance $40".
      • void purchaseMeal(int numOfMeals) which purchases meals according to the number of meals given from the argument. This method checks whether the account has enough balance for purchasing this number of meals based on the pricePerMeal. If there is not enough balance, it computes the maximum number of meals that allows the current balance to purchase and leaves the rest amount as new balance. For example, if I want to purchase 20 meals, the balance is $90.2 and the price per meal is $10, only 9 meals will be purchased, leaving the account balance $0.2. The method should output a simple status message to System.out, such as "Not enough balance for 20 meals. Purchased 9 meals with $10.0 per meal, New balance $0.2".
      • void haveMeal() which reduces the number of meals by one and throws an exception if the number of meals is 0 before reduction. This exception should be properly handled in the calling code.   The method output a simple status message to System.out, such as "8 meals left".
  • Be sure to do error checking on the inputs.  Negative deposit and number of meals to purchase are not allowed.
  • Since we're working with money, all decimals need to be rounded off to two decimal places.  You can achieve this by multiplying a decimal by 100, rounding using Math.round(), and then dividing the result by 100.0.  I suggest making a function to do this, since you will need to do it a lot.
  • Write a main() method within the CreditAccount ExpressAccount class to demonstrate its functionality.  The main() method should perform the following tasks (in-order):

In this part, you are going to use control flow statements to make a user menu with only one general express account type. This means that the menu is pretty much the same as the final version showed above, except that there is only one type of express accounts. Here's an example trace of the program.

Welcome to CS206 Fall'13 Express Account Company

MAIN MENU
1.) Create a new account
2.) Log into an existing account
3.) Exit the banking system
Please enter your selection: 1

CHOOSE THE TYPE FOR THE NEW ACCOUNT
1.) Express account
Please enter your selection: 1
Created new Express account #0, balance: $0.0, number of meals: 0

EXPRESS ACCOUNT #0, BALANCE: $0.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 1
Enter deposit amount: 600
Deposit $600.0 New balance $600.0

EXPRESS ACCOUNT #0, BALANCE: $600.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 2
Enter the number of meals you want to purchase: 1
Purchased 1 meals with $10.0 per meal New balance $590.0

EXPRESS ACCOUNT #0, BALANCE: $590.0, NUMBER OF MEALS: 1
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 3

EXPRESS ACCOUNT #0, BALANCE: $590.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 3
No meals left on your account. Please purchase meals first.

EXPRESS ACCOUNT #0, BALANCE: $590.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 1
Enter deposit amount: 0
The deposit must be a positive amount.

EXPRESS ACCOUNT #0, BALANCE: $590.0, NUMBER OF MEALS: 0
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 2
Enter the number of meals you want to purchase: 60
Not enough balance for 60 meals
Purchased 59 meals, New balance $0.0

EXPRESS ACCOUNT #0, BALANCE: $0.0, NUMBER OF MEALS: 59
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 4
Goodbye!

MAIN MENU
1.) Create a new account
2.) Log into an existing account
3.) Exit the system
Please enter your selection: 2

EXPRESS ACCOUNT #0, BALANCE: $0.0, NUMBER OF MEALS: 59
1.) Make a deposit
2.) Purchase meals
3.) Have meal
4.) Log out
Please enter your selection: 4
Goodbye!

MAIN MENU
1.) Create a new account
2.) Log into an existing account
3.) Exit the system
Please enter your selection: 3
Exiting the system


Implementation Hints
  • Start by writing the ExpressAccount class with only the accountBalance field, a toString() method that outputs the balance, and a main() method that instantiates a new instance of the ExpressAccount class and outputs the result of calling the toString() method.  Compile and test to make sure that this simple version works.
  • Gradually build up the functionality of the ExpressAccount class, adding each field and method in turn, testing thoroughly that your class works at each step.  Do this very gradually, breaking the development of the ExpressAccount class into small steps.
  • It is often a handy trick to write a main() method for every class that tests the class thoroughly, as we've done in this assignment.  This is very useful for diagnostic purposes, and will be essential when we start developing larger programs.
  • For the menu part, one option (for now) of storing all accounts is by using an array. Array is always created with a fixed length. Initially, you have an array of size 1. When you create another account, use a temporary array (that is one size bigger) and System.arraycopy method to increase the size the the original array.
  • To accept input from the console, you can use java.util.Scanner to directly accept input from System.in or use java.io.BufferedReader to read text from a character-input stream, buffering characters so as to provide for the efficient reading. Check Java Doc according to your JDK version for the usage of these classes.
  • Notice that you have two levels of menu: main menu and sub-menus. For each option in the main menu, there is a sub-menu for this option. However, some sub-menus share the same functionalities. For example, after you create an account, the display is the same as after you choose to log into an existing account. Instead of writing same code several times, you should try to make your program as modularly as possible by writing small methods. One good practice would be separating the display the menu from the logic of making options on the menu by passing parameters and getting return values wisely. With this in mind, you will notice that we have the following functionalities in the menu part: display the main menu, create a new account of type ExpressAccount, store an account into the array, retrieve an account information from the array, display a user menu (for making a deposit, purchasing meals, etc). For each of these functionalities, it is recommended to have a method implementing it.

    Question: What about making a deposit, purchasing meals, having meals? Where should these functionalities go?

Part 2 (100 pts) Completing the whole assignment
In this part, you are going to finish the whole assignment.
  • Complete the sub-menu functions to properly display the options for interacting with a single account (student or faculty), following the menu format given below in the example trace. 
  • Notice that you may need to modify some methods in the ExpressAccount class you created in Part 1.
  • Implement classes for the student and faculty express accounts that extend the ExpressAccount and implement the appropriate behavior for each type of account.  These subclasses should reuse as much as possible from the base ExpressAccount class.  If functionality is shared between both account types, then it should be included in the ExpressAccount class (i.e., don't implement the same thing three times in the subclasses, implement it once in the ExpressAccount class).  You may add constructors and protected setters to your ExpressAccount class; however, you should be able to reuse the methods purchaseMeal and haveMeal implemented in your ExpressAccount class.
  • Be sure to do error checking on the inputs.  Negative deposit and number of meals are not allowed.
  • As in the previous assignment, all decimals need to be rounded off to two decimal places.
  • While you are free to be creative with some of the messages, you program must follow the same menu structure, the ordering of the options, and the effects of each button press.  In other words, if I close my eyes at the main menu and then choose option 2, enter account number 0, then option 1, and then type 300, your program should do the exact same thing as the sample program.  You can improve the look of the menu system, the wording of the messages, the formatting of the amounts (for example, make all dollar amounts be outputted to two decimal places), and the output details; just don't change the program functionality.  If you do wish to add new features, you can add additional items to the end of any menu.
Submitting the Assignment

Be certain that all files contains proper header comments that include your name.

Place all .java and compiled .class files into a directory entitled LastnameFirstname-Assignment2-PartX
Make certain that your assignment will run when the command java Assignment2 is called from the terminal.

Compress the LastnameFirstname-Assignment2-PartX folder as a zip or tar file, and submit it via Dropbox, as detailed in the assignment submission instructions.