Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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: //********** here is the Account. Java that I have. import java.util.Random; public class Account { private double balance; private String name; private long acctNum; //---------------------------------------------- //Constructor -- initializes balance, owner, and account number //---------------------------------------------- public Account(double initBal, String owner, long number) { balance = initBal; name = owner; acctNum = number; } // !!!!!! New Constructor !!!!!! public Account(double initBal, String owner) { balance = initBal; name = owner; acctNum = generateAccountNumber(); } // !!!!!! New Constructor !!!!!! public Account(String owner) { balance = 0; name = owner; acctNum = generateAccountNumber(); } //---------------------------------------------- // 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) balance -= amount; else System.out.println("Insufficient funds"); } // !!!!!! New withdraw() method !!!!!! public void withdraw(double amount, double fee) { double amountWithFee = amount + fee; if (balance >= amountWithFee) balance -= amountWithFee; else 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 + " Account Number: " + acctNum + " Balance: " + balance; } // !!!! NEW PRIVATE HELPER METHOD TO GENERATE ACCOUNT NUMBERS !!!!! //------------------------------------------------------- // Returns a random account number between 10000 - 99999 //-------------------------------------------------------- private long generateAccountNumber() { Random r = new Random(); // Seed the random number genertor with the system time return 10000 + r.nextInt(89999); // .nextInt(89999) will return a value between 0 and 89999) } } 1. Suppose the bank wants to keep track of how many accounts exist. a. Declare a private static integer variable numAccounts to hold this value. Like all instance and static variables, it will be initialized (to 0, since its an int) automatically. b. Add code to the constructor to increment this variable every time an account is created. c. 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. d. File TestAccounts1.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 acct1, 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); 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" + i); System.out.println(" Created account " + testAcct); System.out.println("Now there are " + Account.numAccounts () + " accounts"); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions

Question

8-6 Who poses the biggest security threat: insiders or outsiders?

Answered: 1 week ago