v many accounts exist. numAccounts to hold this value. Lik omatically. at this variable every time an account hat returns the total number of accour to any particular account.
v many accounts exist. numAccounts to hold this value. Lik omatically. at this variable every time an account hat returns the total number of accour to any particular account.
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
![Opening and Closing Accounts
File Account,java (see previous exercise) contains a definition for a simple bank account class with methods to withdraw,
deposit, get the balance and account number, and return a String representation. Note that the constructor for this class
creates a random account number. Save this class to your directory and study it to see how it works. Then write the following
additional code:
1. Suppose the bank wants to keep track of how many accounts exist.
Declare a private static integer variable numAccounts to hold this value. Like all instance and static variables, it will
be initialized (to 0, since it’s an int) automatically.
а.
b. Add code to the constructor to increment this variable every time an account is created.
Add a static method getNumAccounts that returns the total number of accounts. Think about why this method should
be static - its information is not related to any particular account.
c.
d. File TestAccountsl1.java contains a simple program that creates the specified number of bank accounts then uses the
getNumAccounts method to find how many accounts were created. Save it to your directory, then use it to test your
modified Account class.
2. Add a method void close() to your Account class. This method should close the current account by appending
"CLOSED" to the account name and setting the balance to 0. (The account number should remain unchanged.) Also
decrement the total number of accounts.
3. Add a static method Account consolidate(Account acctl, Account acct2) to your Account class that creates a new
account whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should
be returned. Two important rules of consolidation:
• Only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a new
account number.
Two accounts with the same number cannot be consolidated. Otherwise this would be an easy way to double your
money!
Check these conditions before creating the new account. If either condition fails, do not create the new account or close
the old ones; print a useful message and return null.
4. Write a test program that prompts for and reads in three names and creates an account with an initial balance of $ 100 for
each. Print the three accounts, then close the first account and try to consolidate the second and third into a new account.
Now print the accounts again, including the consolidated one if it was created.
//**********
// 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)
{
Account testAcct;
Scanner scan = new Scanner (System.in);
Chapter 7: Object-Oriented Design
113
System.out.println("How many accounts would you like to create?"); int num =
scan.nextInt();
for (int i=1; i<=num; i++)
{
testAcct = new Account (100, "Name"
System.out.println("\nCreated account
System.out.println("Now there are
+ i);
+ testAcct);
+ Account.numAccounts () +
accounts");
%3D
}
}
}](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6a453cf0-4fa7-465e-ab7f-471dd1e543ba%2F7378adb4-9c7c-4838-a12d-d86739fb7211%2Fgapa1d_processed.jpeg&w=3840&q=75)
Transcribed Image Text:Opening and Closing Accounts
File Account,java (see previous exercise) contains a definition for a simple bank account class with methods to withdraw,
deposit, get the balance and account number, and return a String representation. Note that the constructor for this class
creates a random account number. Save this class to your directory and study it to see how it works. Then write the following
additional code:
1. Suppose the bank wants to keep track of how many accounts exist.
Declare a private static integer variable numAccounts to hold this value. Like all instance and static variables, it will
be initialized (to 0, since it’s an int) automatically.
а.
b. Add code to the constructor to increment this variable every time an account is created.
Add a static method getNumAccounts that returns the total number of accounts. Think about why this method should
be static - its information is not related to any particular account.
c.
d. File TestAccountsl1.java contains a simple program that creates the specified number of bank accounts then uses the
getNumAccounts method to find how many accounts were created. Save it to your directory, then use it to test your
modified Account class.
2. Add a method void close() to your Account class. This method should close the current account by appending
"CLOSED" to the account name and setting the balance to 0. (The account number should remain unchanged.) Also
decrement the total number of accounts.
3. Add a static method Account consolidate(Account acctl, Account acct2) to your Account class that creates a new
account whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should
be returned. Two important rules of consolidation:
• Only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a new
account number.
Two accounts with the same number cannot be consolidated. Otherwise this would be an easy way to double your
money!
Check these conditions before creating the new account. If either condition fails, do not create the new account or close
the old ones; print a useful message and return null.
4. Write a test program that prompts for and reads in three names and creates an account with an initial balance of $ 100 for
each. Print the three accounts, then close the first account and try to consolidate the second and third into a new account.
Now print the accounts again, including the consolidated one if it was created.
//**********
// 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)
{
Account testAcct;
Scanner scan = new Scanner (System.in);
Chapter 7: Object-Oriented Design
113
System.out.println("How many accounts would you like to create?"); int num =
scan.nextInt();
for (int i=1; i<=num; i++)
{
testAcct = new Account (100, "Name"
System.out.println("\nCreated account
System.out.println("Now there are
+ i);
+ testAcct);
+ Account.numAccounts () +
accounts");
%3D
}
}
}
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps with 1 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education