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 the balance with a dollar sign

Add a method deposit which takes a double and adds it to the balance. 

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.  So if we have balance 20, and we withdraw 5, then we return that 5 and leave balance at 15, but if we have balance 15 and withdraw 20, then we return 15 and leave balance at 0.

For both of these methods, if you are given a negative number, print a complaint and do nothing (if a method returns, it must always return something, even if it "does 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

You may either give Bank an instance variable of type Scanner to use in all the methods, or just create a local scanner variable in each method that needs it.  (Remember that the info on using 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 that value set for its balance.

In Bank, create a method starter, which sets up currentAccount,  using the makeAccount method.

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
3. Withdraw
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

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.

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

1. $10 Quick Deposit
2. Custom Deposit
0. Return to main menu.

If they enter 1, deposit 10 in currentAccount. (dont forget to use Account's methods!!!)

If they enter 2, ask them how much to add to balance, read in the number, and deposit that.

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

1. $20 Quick Cash
2. Custom Withdrawal
0. Return to main menu.

If they enter 1, try to withdraw 20 from currentAccount, reporting how much was actually withdrawn.

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

  In your main, create a Bank, call its starter method, then call its mainMenu method and check that this much works.  After they have exited the main menu, print the Bank's current account.


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. 

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

$4583.02 ($2273 available in joint account)

Add a method transferFromJoint 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 transferToJoint 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:  Create Joint Account
If they choose this, use makeAccount to create a new Account, and use the join method to join the accounts.

To the deposit menu, add the option

3. Transfer From Joint Account

and if they enter 3, transfer from the joint account to the account an amount specified by the user.

To the withdrawal menu, add the option

3. Transfer To Joint Account

and if they enter 3, ask them how much to transfer, and then transfer it



[EC] In the makeAccount method, add a loop so you keep asking for the balance until you get a reasonable number -- balances should never be 0 or less.

[EC] Only show the transfer funds options in the menus if the currentAccount has a jointAccount.


 [EC]

 Add to Account a double instance variable penalty.  Each time they try to withdraw more money than they have, add $2 to their penalty. Whenever they deposit an amount, deduct as much of the the penalty as possible from that amount before depositing, and reduce the penalty accordingly.

So, if a user made three withdrawals that incurred the penalty, their penalty would be $6.  If they then deposit $5, they will end up depositing $0, but the penalty will be down to $1, if then they deposit another $5, they will actually deposit $4 and the penalty will be down to $0. 

In the toString, if there is currently a penalty, include it.