Opening and Closing Accounts
File Account.java (see previous exercise) contains adefinition for a simple bank account class with methods towithdraw, deposit, get the balance and account number, and return aString representation. Save this class to your directory and studyit to see how it works. Then write the following additionalcode:
Suppose the bank wants to keep track of how many accountsexist.
Declare a private static integer variable numAccounts to holdthis value. Like all instance and static
variables, it will be initialized (to 0, since it’s an int)automatically.
Add code to the constructor to increment this variable everytime an account is created.
Add a static method getNumAccounts that returns thetotal number of accounts. Think about why this
method should be static - its information is not related to anyparticular account.
File TestAccounts1.java contains a simple program thatcreates the specified number of bank accounts then uses thegetNumAccounts method to find how many accounts were created. Saveit to your
directory, then use it to test your modified Account class.
Add a method void close() to your Account class. This methodshould close the current account by
appending “CLOSED” to the account name and setting the balanceto 0. (The account number should remain
unchanged.) Also decrement the total number of accounts.
Write a test program TestAccounts2 that prompts forand reads in three names and creates an account
with an initial balance of $100 for each. Print the threeaccounts, then close the first account. Call numAccounts()toprint the number of accounts.
***********// // Account.java // // A bank account class with methods to deposit to, withdraw from, // change the name on, and get a String representation // of the account. //***************: public class Account { private double balance; private String name; private long acctNum; ||----- ||---- public Account(double initBal, String owner) { //Constructor -- initializes balance and owner; generates random //account number balance = initBal; name = owner; acctNum = (int) (Math.random()* Integer.MAX_VALUE); } //-- // Checks to see if balance is sufficient for withdrawal. // If so, decrements balance by amount; if not, prints message. ||------ public void withdraw(double amount) { if (balance >= amount) ***** else balance amount; ***
} System.out.println("Insufficient funds"); } ||---- // Adds deposit amount to balance. ||------ public void deposit(double amount) { balance + amount; } //--- // Returns balance. ||------------ public double getBalance() { return balance; } } //-- // Returns a string containing the name, account number, and balance. //-- public String toString() { return "Name:" + name + "\nAccount Number: " + acctNum + "\nBalance: " + balance;
//******** // TestAccounts1 // A simple program to test the numAccts method of the // Account class. //******** import java.util.Scanner; public class TestAccounts1 { } public static void main(String[] args) { } scan.nextInt (); for (int i=1; i<=num; i++) { Account testAcct; Scanner scan = new Scanner(System.in); System.out.println("How many accounts would you like to create?"); int num = } testAcct = new Account (100, "Name" + i); "1 **** () + **** System.out.println("\nCreated account + testAcct); System.out.println("Now there are + Account.numAccounts accounts");
Opening and Closing Accounts File Account.java (see previous exercise) contains a definition for a simple bank account c
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am