Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assume that a class called BankAccount has been implemented to represent one persons bank account. A partial declaration of the class is given below: public

Assume that a class called BankAccount has been implemented to represent one persons bank account. A partial declaration of the class is given below:

public class BankAccount {

private int accountNum;

private double balance;

// constructor not shown

// returns the account number

public int getAccountNum() {

return accountNum;

}

// returns the current balance

public double getBalance() {

return balance;

}

// adds the given amount to the current balance

public void doDeposit( double amount ) {

balance += amount;

}

// subtracts the given amount from the current balance

public void doWithdrawal( double amount ) {

balance -= amount;

}

}

Part a:

In order to process the various transactions performed at the bank (either by ATM or bank teller), a Transaction class is needed: A Transaction includes an account number (an integer), the transaction type (a String with a single character of d for deposit or w for withdrawal), and the amount of the transaction (a double for the amount to be deposited or withdrawn). Those values are assigned when the transaction is created and can be accessed but not modified.

Write the complete class declaration for the Transaction class. Include all necessary instance variables and implementations of its constructor and methods.

public class Transaction {

Part b:

An ATMTransaction class (a subclass of Transaction) is also needed to represent one ATM transaction. In addition to the information in a Transaction, an ATMTransaction contains a String that represents the location of the ATM that was used. The value of that location is assigned when the ATMTransaction is created and can be accessed but not modified. Write a complete class declaration for the ATMTransaction class. Include all necessary instance variables and implementations of its constructor and methods.

public class

Part c:

A class Bank will be used to store information about all of the accounts in one bank, and to perform transactions on those accounts. An incomplete declaration of the Bank class is given below.

public class Bank {

private BankAccount [ ] accounts;

// Constructor not shown

// precondition: accountNum is the number of an account in the accounts array

// postcondition: returns the index in the accounts array of the object with the

// given account number

private getIndex ( int accountNum ){

// method not shown

}

// precondition: trans is a transaction for an account in the accounts array

// postcondition: the account for trans has been modified according to the

// specified transaction

public void doOneTransaction ( Transaction trans ){

// implemented in part c

}

}

Write the doOneTransaction method of the Bank class, which has one Transaction parameter. Method doOneTransaction should find the BankAccount with the account number in the given Transaction, and it should deposit or withdraw the amount in the given Transaction from the account as appropriate.

For example, assume that accounts.length is 4 and that the elements of the array have the account numbers and balances shown below:

Index

0

1

2

3

Account #

100

107

102

105

Balance

100.27

57.30

150.00

5.25

Here are some examples of to illustrate what the call doOneTransaction( trans ) should do base one the format trans ( accountNum, transactionType, amount)

trans

Accounts index

Account num

Modified Balance

(107, d, 10.50)

1

107

67.80

(100, w, 100.27)

0

100

0.0

(105, w 6.00)

3

105

-0.75

When writing the method doOneTransaction, you should include call(s) to the method getIndex specified above in the declaration of the Bank class. You may also assume that the BankAccount and Transaction classes work as intended.

Complete the method doOne Transaction below:

// precondition: trans is a transaction for an account in the accounts array

// postcondition: the account for trans has been modified according to the specified transaction

public void doOneTransaction ( Transaction trans ){

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

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions