[Solved]-Understand Class Problem Bankaccount Class Represents Basic Bank Account Contains Instance Q37165745
Understand the Class and Problem The BankAccount classrepresents a basic bank account. It contains instance datarepresenting the account’s number, current balance, and name ofowner. Note that instance data can be an object reference variable(not just a primitive type), such as the account owner’s name,which is a reference to a String object. The constructor of theBankAccount class accepts three parameters that are used toinitialize the instance data. The deposit and withdraw methodsperform the basic transactions on the account, adjusting thebalance based on the parameters.
The UML diagram for the BankAccount class is as follows:BankAccount – balance: double – owner: String – accountNumber: int+ BankAccount (initBal:double, name:String, number:int) +BankAccount (initBal:double, name:String): + BankAccount(owner:String) + deposit(amount:double): void + withdraw(amount:double):void + getBalance(): double + getOwner(): String +getAccountNumber(): int + setAccountNumber(newAccountNumber:String): void + setOwner(name: String): void
Part 1: Bank Account Class
Design a BankAccount class that represents a basic bank account.The class constructor should accept the account’s number, currentbalance, and name of owner. The class should also have methods forsubtracting the amount of a withdrawal and adding the amount of adeposit. Write code to do the following:
Public Class Static Constants (declare to be final): MIN_LENGTH= 2; MAX_LENGTH = 40; String DEFAULT_NAME = “nobody”; intMIN_BALANCE = 0; int DEFAULT_NUMBER = 0; int MIN_NUMBER = 0; intMAX_NUMBER = 999999;
Private Member Data Include three members: balance double; ownerString; accountNumber int; Instance (Public) Methods You shouldprovide the following instance methods: ▪ BankAccount (doubleinitBal, String name, int number) –
The constructor should initialize the balance, account number,and owner as specified. ▪ Overload the constructor as follows: •BankAccount (double initBal, String name) – initializes the balanceand owner as specified; randomly generates the account number. •BankAccount(String name) – initializes the owner as specified; setsthe initial balance to MIN_BALANCE and randomly generates theaccount number.
void deposit(double amount) – A method that accepts an argumentfor the deposit amount. The method should add the argument to theaccount balance. include appropriate value checks whenapplicable
▪ void withdraw(double amount) – A method that accepts anargument for the amount of the withdrawal. The method shouldsubtract the argument from the balance.
▪ void withdraw(double amount, double fee) – Overload thewithdraw method with one that also takes a fee and deducts that feefrom the account. include appropriate value check.
▪ Accessors for each field (instance member).
▪ Mutators for the account number and owner fields.
▪ void display() – A method that clearly shows the bank’sdata.
▪ void close() – A method that closes the current account byappending “CLOSED” to the account name and setting the balance to0. (The account number should remain unchanged.) This method alsodecrements the total number of accounts.
Private Static Helper Methods private static validation helpersto filter client parameters. These will support your publicmethods. The class should only allow String names that have between2 and 40 characters. It will only allow bank account numbers thatare between the numbers 0 and 999999. It will also only allow apositive balance. Any other value will be considered a User Errorand the client will be not allowed to set such values.
▪ boolean isValidName( String name) – A helper method thatmutators can use to determine whether a String is legal. Thismethod returns true if the string is not null and its length isbetween MIN_LENGTH and MAX_LENGTH (inclusive). It returns false,otherwise.
boolean isValidBalance(double initBalance) – A helper methodthat mutators can use to determine whether a balance is legal. Thismethod returns true if the balance is greater than 0. It returnsfalse, otherwise.
▪ boolean isValidNumber(int acctNum) – A helper method thatmutators can use to determine whether an account number is legal.This method returns true if the account number is betweenMIN_NUMBER and MAX_NUMBER (inclusive). It returns false,otherwise.
Counting Transactions:
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 beinitialized (to 0, since it is an int) automatically.
▪ Add code to the constructor to increment this variable everytime an account is created.
▪ Add a static method getNumAccounts that returns the totalnumber of accounts. Think about why this method should be static -its information is not related to any particular account.
▪ Add four private static variables to the BankAccount class,including one to keep track of each value above (number and totalamount of deposits, number and total of withdrawals). intnumAccounts; int numDeposits; int numWithdrawals; int amtDeposits;int amtWithdrawals; Note that, since these variables are static,all of the BankAccount objects share them. This contrasts with theinstance variables that hold the balance, name, and number of theaccount; each Account has its own copy of these. Recall thatnumeric static and instance variables are initialized to 0 bydefault.
▪ Add public methods to return the values of each of thevariables you just added:
• static int getNumDeposits() – A method that returns the totalnumber of deposits into the account.
• static int getNumWithdrawals() – A method that returns thetotal number of withdrawals from the account.
• static int getAmtDeposits() – A method that returns the totalamount of deposits into the account.
• static int getAmtWithdrawals() – A method that returns thetotal amount of withdrawals from the account.
• static void resetCounters() – A method that resets thevariables holding the numbers and amounts of withdrawals anddeposits to the Account class. ▪ Modify the withdraw and depositmethods to update the appropriate static variables at eachwithdrawal and deposit.
Part 2: Process Transactions (15 points) File Bank.java containsa program that creates and initializes three BankAccount objectsand enters a loop that allows the user to enter transactions foreither account until asking to quit. Modify this program asfollows: ▪ After the loop, print the total number of deposits andwithdrawals and the total amount of each. You will need to use theBankAccount methods that you wrote above. Test yourprogram. ▪Imagine that this loop contains the transactions for a single day.Embed it in a loop that allows the transactions to be recorded andcounted for many days. ▪ At the beginning of each day, print thesummary for each account then have the user enter the transactionsfor that day. When all transactions have been entered, print thetotal numbers and amounts (as above) then reset these values to 0and repeat for the next day.
import java.util.Scanner;
public class Bank {
public staticvoid main(String[] args) {
BankAccount account1, account2, account3; // three testaccounts
String keepGoing = “y”; // more transactions?
String action; // deposit or withdraw
double amount; // how much to deposit orwithdraw
int acctNumber; // which account to access
Scanner scan = newScanner(System.in);
// Create two accounts
account1 = new BankAccount(1000.00, “MatthewJohnson”, 12345);
account2 = new BankAccount(1000.00, “SueAlexander”, 45678);
account3 = new BankAccount(1000.00, “JamesWilliam”, 34567);
System.out.println(“The followingaccounts are available:n”);
account1.display();
System.out.println();
account2.display();
System.out.println();
account3.display();
while (keepGoing.equalsIgnoreCase(“y”)) {
// get account number, what to do, and amount
System.out.print(“nEnter the numberof the account you would like to access: “);
acctNumber = scan.nextInt();
System.out.print(“Would you like tomake a deposit (D) or withdrawal (W)? “);
action = scan.next();
System.out.print(“Enter the amount:”);
amount = scan.nextDouble();
if (amount > 0)
if (acctNumber ==account1.getAccountNumber())
if (action.equalsIgnoreCase(“w”))
account1.withdraw(amount);
else if(action.equalsIgnoreCase(“d”))
account1.deposit(amount);
else
System.out.println(“Sorry, invalidaction.”);
else if (acctNumber ==account2.getAccountNumber())
if (action.equalsIgnoreCase(“w”))
account2.withdraw(amount);
else if(action.equalsIgnoreCase(“d”))
account2.deposit(amount);
else
System.out.println(“Sorry, invalidaction.”);
else if (acctNumber ==account3.getAccountNumber())
if (action.equalsIgnoreCase(“w”))
account3.withdraw(amount);
else if(action.equalsIgnoreCase(“d”))
account3.deposit(amount);
else
System.out.println(“Sorry, invalidaction.”);
else
System.out.println(“Sorry, invalidaccount number.”);
else
System.out.println(“Sorry, amount mustbe > 0.”);
System.out.print(“nMore transactions?(y/n)”);
keepGoing = scan.next();
}
// Print number of deposits
// Print number of withdrawals
// Print total amount of deposits
// Print total amount of withdrawals
keepGoing = “y”;
}
}
Expert Answer
Answer to Understand the Class and Problem The BankAccount class represents a basic bank account. It contains instance data repres… . . .
OR

