Question: For this program, you will be the creator of a class (in Part1) and later the user of the class (in Part2). You will have

For this program, you will be the creator of a class (in Part1) and later the user of the class (in Part2). You will have to be familiar with classes, methods, passing things in and out of methods, constructors, static data/methods, exception handling, interfaces, and aggregate classes. The problem: You are to write a class called BankAccount, which will simulate a users bank account. You can use my Program5Driver.java as the driver that will create instances and tell them to do things (through their methods). Then you will create your own driver called Program5.java to create other instances and tell them to do things. The BankAccount class: this class should implement AccountInterface and have the following: ? Data: it should have the following data. I have suggested variable names to help in understanding what they do and to make the explanations clearer. You can name them anything you want (as long as the name is meaningful). o A double called balance, which will hold the balance for that account. o A String called name, which will hold the name for that account. o A java.util.Date called creationDate, which will contain a reference to a java.util.Date that holds the creation date. You can declare it as a java.util.Date or import java.util.*. Butif you import, be sure that you do not have another Date class in the same directory or the compile will use the one in the directory. o A boolean called frozen, which keeps track of whether or not the account is frozen (cannot be accessed). o A double called limit, which will hold the maximum that can be withdrawn at one time from that account. o A double called MAXLIMIT, which will be a constant (final) set to 500. This will be the maximum that the withdrawal limit can be set to. o An int called accountNumber, which will hold the account number for that account. o A static int called howMany. The BankAccount class will use this to keep track of how many individual accounts have been created. ? Constructors: it should have the following 2 constructors. o A default constructor (which receives no arguments). It should set the accounts name field to classified and the other fields should be set to the same initial values as the other constructor (below). If you want, you can make this work by calling the other constructor with the statement: this(classified); o A parameterized constructor which receives 1 argument the name (a String) of the person who has the account. It should do the following: ? Set the accounts name to the name that is passed in ? Create the creationDate using the default constructor of the java.util.Date class. It will initialize the creationDate to the current time (from the computer clock). ? Set frozen to false; ? Set the limit to 300 (always, does not depend on what is passed in). ? Add 1 to howMany (so the class knows how many accounts are now created) ? Then set the accountNumber to howMany (if there are now 3 accounts, then this will be #3) ? Set the balance to 0. ? Methods: Since BankAccount implements AccountInterface, it will automatically have to have the methods defined in the interface. It should also have the static method shown below. Therefore the following methods should be implemented. o a static method called getNumAccounts(), which receives nothing and returns an int showing how many accounts have been created. o Also implement all of the other methods listed in the AccountInterface. Please see the comments in AccountInterface.java for their descriptions. o Please look at AccountInterfaces comment for the toString() method, as the requirements have changed, Part 1: Write your BankAccount class and compile it (with the java.util.Date class as an aggregate class). To test it, you can download Program5Driver.java from Blackboard and use it as a driver to create accounts and tell them to do things. The expected results from running Program5Driver.java are also on Canvas as expected.txt. Part 2: Once your BankAccount class is working, create another driver program of your own called Program5.java. Have it implement the following functionality inside main. Notice there are 4 sections (create/use 3 accounts and then ask the BankAccount how many accounts have been created). Put each of the 4 sections into a separate trycatch block; for each one make sure that every possible type of exception can be caught (either by having multiple catch blocks or by setting it up to catch every possible type of exception). You can just print the exception if it is caught but make the description match the tester so it passes Hypergrades test ? Create a new account (use any variable name you want) pass in Brad Pitt. ? Tell it to deposit 439.50 ? Tell it to deposit 10 ? Tell it to withdraw 60 ? Tell it to get the balance and print The balance is: and then the balance that was returned. ? Print one blank line ? Create a new account (use any variable name you want) pass in Beyonce ? Tell it to deposit 45.00 ? Tell it to withdraw 20 ? Print the account information (its toString()). ? Print one blank line ? Create a new account (use any variable name you want) pass in Scam Artist ? Tell it to deposit 10 ? Tell it to withdraw 260 ? Print one blank line ? Ask the BankAccount class how many accounts have been created and print the results in this format (for example) Number of accounts: 4 Extra Credit (): The variables that will hold money should be doubles (because of decimals). However, they will not automatically print out like money. After your BankAccount.java is working (and has been submitted on Hypergrade), make a copy of your BankAccount.java (in another folder) and make the following changes to have them print out like money. You will submit this version in the Hypergrade extra credit question. 1. Put the following import statement in your program (both BankAccount and Program5) import java.text.NumberFormat; 2. Somewhere in the class (before you print things as money), put the following line: NumberFormat money = NumberFormat.getCurrencyInstance(); 3. You have created an instance of the NumberFormat class and called it money. Now, every time you want to print out a money variable, you can tell money (your variable) to format it so it looks like currency. Instead of using the variable in the print statement, use money.format(). For example, if you have a variable called theDeposit, you can change it from this: System.out.println(\tTotal income:\t\t + theDeposit); to this: System.out.println( "\tTotal income:\t\t" + money.format(theDeposit)); Also - create a new class called IllegalWithdrawalException. Make it a subclass of RuntimeException. It will not need any data or methods (all will be inherited). It should have one constructor which accepts a single String as an argument. Have that constructor do the same thing as its parents constructor would do with the same String. Once your IllegalWithdrawalException compiles, change the BankAccount code so that it throws a new IllegalWithdrawalException instead of an IllegalArgumentException for any of the bad conditions in the withdraw method. Finally, go back to your Part2 and change the try-catch blocks everywhere so that they will also catch an IllegalWithdrawalException. You can do this either by adding another catch block to the existing code everywhere or by changing the catch blocks so that the same catch block will the IllegalArgumentExceptions, IllegalWithdrawalExceptions, and IllegalStateExceptions.

Can some one help me with both parts of these?

AccountInterface.java is below along with Program5Driver.java

public interface AccountInterface { // deposit - Throws a new IllegalArgumentException() if // --> the deposit amount is negative // Throws a new IllegalStateException() if // --> the account is frozen // THEN (if everything is OK), adds whatever was passed in to the // balance (therefore depositing the money. public void deposit(double theMoney);

// withdraw - Throws a new IllegalArgumentException() if // any of the following conditions occur: // --> the withdrawal amount is negative // --> the account does not have enough money // --> the withdrawal amount is more than the limit for that account // --> the withdrawal amount is not a multiple of 20 // Throws a new IllegalStateException() if // --> the account is frozen // THEN (if everything is OK), subtractS whatever is passed in from the balance // (therefore "withdrawing" the money). It also returns a double representing // the amount that was withdrawn. public double withdraw(double theMoney);

// getBalance - returns double representing the accounts balance. public double getBalance();

// freeze - changes its status to "frozen" public void freeze();

// unfreeze - changes its status to not "frozen" public void unfreeze();

// setLimit - Throws a new IllegalArgumentException() if // any of the following conditions occur: // --> the new limit is negative // --> the new limit is larger than MAXLIMIT // Throws a new IllegalStateException() if // --> the account is frozen // THEN (if everything is OK), changes the limit for the account to // what was passed in. public void setLimit(double newLimit);

// getLimit - returns the current limit for the account public double getLimit();

// toString - returns its representation as a String. // The string will be built up line by line and should contain // the accounts information in this order and format // (starting in the first column): // Account number: (the account number) // Name: (the name on the account) // Balance: (the account's balance) // Withdrawal limit: (the withdrawal limit for the account) // Frozen: (true or false; whether or not the account is frozen) public String toString();

// NOTE: A STATIC METHOD CANNOT BE DEFINED IN AN INTERFACE (because: // an interface lists every method that must be implemented and available // to instances of the class. But static methods go with the class, not with // instances of the class. But ONE IS LISTED HERE TO BE IMPLEMENTED.

// getNumAccounts - a static method that returns how many accounts have been created. //public static int getNumAccounts(); }

//This is a driver for the BankAccount program. It will create BankAccounts and tell them //to deposit, withdraw, get their balance, etc.

public class Program5Driver { public static void main(String args[]) { //declare variables double cash; BankAccount swissBankAccount; //"anonymous" BankAccount myAccount; //with a name

//create a new BankAccount - use default constructor (don't pass anything) System.out.println(" ****** Creating a BankAccount using the default constructor "); swissBankAccount = new BankAccount();

try { System.out.println(" ****** Printing it to see what is in it "); //print it (to see what the constructor put in) System.out.println(swissBankAccount); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

//create a new BankAccount - pass in name to the constructor System.out.println(" ****** Creating another BankAccount using the parameterized constructor "); myAccount = new BankAccount("Bill Gates");

try { //print it (to see what the constructor put in) System.out.println(" ****** Printing it to see what is in it "); System.out.println(myAccount); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

//create a new BankAccount - pass in name to the constructor System.out.println(" ****** Creating another BankAccount using the parameterized constructor "); myAccount = new BankAccount("Larry Fitzgerald");

try { //print it (to see what the constructor put in) System.out.println(" ****** Printing it to see what is in it "); System.out.println(myAccount); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

//tell the account to freeze try { System.out.println(" ****** Telling it to freeze "); myAccount.freeze(); } catch(Throwable ex) { ex.printStackTrace(); }

//tell it to deposit some money (should not let us - it is frozen) try { System.out.println(" ****** Telling it to deposit 40 (it is frozen; should throw an exception) "); myAccount.deposit(40); } catch(IllegalStateException ex) { ex.printStackTrace(); } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

//tell it to unfreeze try { System.out.println(" ****** Telling it to unfreeze "); myAccount.unfreeze(); } catch(Throwable ex) { ex.printStackTrace(); }

try { //tell it to withdraw some money (should not let us - there is none) System.out.println(" ****** Telling it to withdraw some money (empty; should throw an exception) "); cash = myAccount.withdraw(25); System.out.println("The account returned this much: " + cash); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //tell it to deposit some money and then check the balance System.out.println(" ****** Telling it to deposit 1565.25 "); myAccount.deposit(1565.25); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

//tell the account to freeze try { System.out.println(" ****** Telling it to freeze "); myAccount.freeze(); } catch(Throwable ex) { ex.printStackTrace(); }

//tell it to withdraw 40 (should not let us - it is frozen) try { System.out.println(" ****** Telling it to withdraw 40 (it is frozen; should throw an exception) "); cash = myAccount.withdraw(40); } catch(IllegalStateException ex) { ex.printStackTrace(); } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

//tell it to unfreeze try { System.out.println(" ****** Telling it to unfreeze "); myAccount.unfreeze(); } catch(Throwable ex) { ex.printStackTrace(); }

try { //tell it to withdraw 2000 (should not let us - insufficient funds) System.out.println(" ****** Telling it to withdraw 2000 (insufficient funds; should throw an exception) "); cash = myAccount.withdraw(2000); System.out.println("The account returned this much: " + cash); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //tell it to deposit -40 (should not let us - negative). Then check the balance System.out.println(" ****** Telling it to deposit -40 (should throw an exception) "); myAccount.deposit(-40); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //tell it to withdraw -30 (should not let us - negative) System.out.println(" ****** Telling it to withdraw -30 (should throw an exception) "); cash = myAccount.withdraw(-30); System.out.println("The account returned this much: " + cash); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //tell it to withdraw 400 (should not let us - too much) System.out.println(" ****** Telling it to withdraw 400 (too much; thould throw an exception) "); cash = myAccount.withdraw(400); System.out.println("The account returned this much: " + cash); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //tell it to withdraw 210 (should not let us - not a multiple of 20) System.out.println(" ****** Telling it to withdraw 210 (not a multiple of 20; should throw an exception) "); cash = myAccount.withdraw(210); System.out.println("The account returned this much: " + cash); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //tell it to withdraw 100 (should be OK) System.out.println(" ****** Telling it to withdraw 100 (should be OK) "); cash = myAccount.withdraw(100); System.out.println("The account returned this much: " + cash); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

//tell the account to freeze try { System.out.println(" ****** Telling it to freeze "); myAccount.freeze(); } catch(Throwable ex) { ex.printStackTrace(); }

try { //tell it to change its limit to 400 (should not let us - frozen) System.out.println(" ****** Telling it to change its limit to 400 (frozen; should throw an exception) "); myAccount.setLimit(400); } catch(IllegalStateException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

//tell it to unfreeze try { System.out.println(" ****** Telling it to unfreeze "); myAccount.unfreeze(); } catch(Throwable ex) { ex.printStackTrace(); }

try { //tell it to change its limit to 800 (should not let us - too big) System.out.println(" ****** Telling it to change its limit to 800 (too big; should throw an exception) "); myAccount.setLimit(800); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //tell it to change its limit to -50 (should not let us - negative) System.out.println(" ****** Telling it to change its limit to -50 (negative; should throw an exception) "); myAccount.setLimit(-50); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //tell it to change its limit to 400 (should be OK) System.out.println(" ****** Telling it to change its limit to 400 (should be OK) "); myAccount.setLimit(400); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //tell it to withdraw 400 (should be OK now) System.out.println(" ****** Telling it to withdraw 400 (should be OK now) "); cash = myAccount.withdraw(400); System.out.println("The account returned this much: " + cash); System.out.println(" ****** Telling it to return its balance "); System.out.println("The balance is: " + myAccount.getBalance()); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //print it System.out.println(" ****** Printing it "); System.out.println(myAccount); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack }

try { //ask the BankAccount class how many accounts have been created - note that //it is a static method so we ask the class. System.out.println(" ****** Telling BankAccount to return the number of accounts "); System.out.println("BankAccount reports " + BankAccount.getNumAccounts() + " account(s) so far"); } catch(IllegalArgumentException ex) { ex.printStackTrace(); //will print the exception and then the call stack } catch(Throwable ex) { System.out.println("(WRONG EXCEPTION)"); ex.printStackTrace(); //will print the exception and then the call stack } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!