Practice


Classwork


Create a project, and in it a class with a main.

 Part A

Add a class Account, which has an instance variable for a balance

Add a toString that returns either something like

$4583.02

At this point, go into your main and create an Account object.  Set its balance  and make sure that when you print it you see the right result.

To Account, add a method deposit which takes a double and adds it to the balance. 

Remember that "takes" always means a method has a parameter.  This method is changing the value of the balance, but we don't have to pass the balance as a parameter since it is an instance variable.  Since I didn't say what type it returns, it is probably void.

In main, try calling your deposit method for the Account and then printing the Account again to see that its balance changes correctly.

In Account, add a method withdraw which takes a double and subtracts it from the balance.  If the amount is larger than the balance, set the balance to 0.  Either way, return however much money we were actually able to withdraw.

Since this method returns something, it will not be void.  It needs to return a value in every case.

Again, go into main, and this time try withdrawing.  Make sure you print what gets returned from the method as well as the value of the balance after withdrawing.  Did you try both withdrawing less and withdrawing more than the balance?

Go back to these methods and change them so that if you are given a negative number, you print a complaint and do nothing.  Think about what code needs to be inside the if, and how to write the condition of the if so that it is true when you do want to do the code.  If a method returns, it needs to return no matter what happens.  Make sure withdraw is returning a reasonable number even if the method is "doing nothing."

Throughout this assignment, if you are depositing to or withdrawing from the account, remember to use these methods!!!


Part B

Create a new class Bank, with an instance variable currentAccount, of type Account.  Remember that if you have a class Account, then Account is a perfectly good variable type, and is used just like String or int.

Also give Bank an instance variable fee.

In the following methods, whenever we need to talk to the user, create a local Scanner variable in each method.  (Remember that the info on the Scanner class is linked from the class page)

In Bank create a method makeAccount, which asks the user for a starting balance.  Return a new Account with the balance set to the value given. 

Remember that asking the user for a value is different from taking a parameter. 

In Bank, create a method starter, which sets up currentAccount using makeAccount, and sets fee to .10

At this point, go to your main and create a Bank object.  Call its starter method and print its currentAccount.  Try depositing and withdrawing to the bank's currentAccount.

In Bank, to provide a menu, you will create methods, mainMenu, depositMenu, and withdrawMenu. 

In mainMenu,

In a loop, print a list of options for the user

1. Show Balance
2. Deposit Menu
3. Withdraw Menu
0. Quit

and then read a number from the user.  Continue until they choose to quit.

If they enter 1, print out the current value of the Account's balance.

If they enter 2 call depositMenu.  Since you haven't gotten to that part of the code yet, you can write a stub method and call that.

If they enter 3 call withdrawMenu

If they enter a submenu, they remain in it until they choose to return, and then they are back in the main menu again.

At this point, go to your main and call the mainMenu method.  Check that you can print the balance, call the stub methods, and eventually quit from the loop.

In depositMenu you will do much the same as in mainMenu, so you may want to copy the code.

This time the list of options is

1.Deposit
0. Return to main menu.

If they enter 1,  ask them how much to add to balance, read in the number, and deposit that.minus the fee.   (don't forget to use your deposit method!!)

After doing this, also withdraw the fee from their account.   So if they deposit $10 and the fee is $.10, they actually get $9.90 total. When withdrawing the fee, check whether the right amount was successfully withdrawn (the full fee was returned by the withdraw method).  If not, print a complaint and add $.05 to the fee.

Remember to now go back to your main and test this much

  In withdrawMenu do the same, but the list of options is

1. Custom Withdrawal
0. Return to main menu.

If they enter 1,  ask them how much, and withdraw that  from currentAccount, then report how much actually was withdrawn. 

After doing this, we again want to  withdraw the fee.  So if they successfully withdrew $10 and the fee is $.10 they actually withdraw $10.10 total.   We want to do this just the same as we did when depositing

In your main, test that withdraw works for various amounts.  Also, after they have exited the main menu, print the Bank's currentAccount.


Part C

In Account, add an instance variable jointAccount, which is an Account. 

Add a method join which takes an account, and makes each Account the other's jointAccount.  So my jointAccount variable will be set to the one passed in, and the one passed in's jointAccount variable will be set to me.

Go back and change the toString so that if there is a joint account (that is, the variable is not null), we now get something like

$4583.02 ($2273 available in joint account)

This means that if there is not a joint account, we still get the original version.

Add a method transferTo which takes a double amount, and if there is a joint account, withdraws amount from jointAccount and deposits it to this account  If there is no jointAccount,  print a complaint. 

Add a similar method transferFrom which takes a double amount, and  if there is a joint account, moves amount in the other direction, with the same rules.

  In Bank, add to the main menu another option: Tools

And add a method toolMenu, which is like the previous menus, but the list of options is

1. Create Joint Account
0. Return to main menu.

If they enter 1, use the makeAccount method to create a new Account, and use the join method to join the accounts (the currentAccount and the new one just created).

To the deposit menu, add the option: Transfer funds to account

and if they enter 3, transfer from the joint account to the account an amount specified by the user.  After doing this, also take the fee  from the main account.

To the withdrawal menu, add the option: Transfer funds from account

and if they enter 3, ask them how much to transfer, and then use the account's transferFrom method. After doing this, also take the  fee from the joint Account.


Part D

  In Account, add a method mergeAccounts which takes a parameter fee.  It the account has at least fee as balance and there is a joint account, transfer all of the joint account's balance to this account.  Then withdraws the.  Finally set the jointAccount to null

  In Bank, add to the tool menu another option

2. Merge Accounts

If they choose this option, merge the account with its jointAccount, and pass in the current fee.

 


[EC+5] in the makeAccount method, loop until you get a reasonable number -- balances should never be 0 or less

[EC+5] in both the custom deposit and custom withdrawl code, loop until you get a reasonable number -- amount to deposit or withdraw should never be 0 or less..

[EC+10] In withdrawMenu and depositMenu, only show the transfer funds options to the user if the currentAccount has a jointAccount.