Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is part 1. Need help with this hw. this is my Account class. need help with

ID: 3913395 • Letter: T

Question

This is part 1.

Need help with this hw. this is my Account class. need help with CheckingAccount as well.

public class Account
{
private int number;
private String name;
private String openDate;
private double balance;

public Account(int number, String name, String openDate, double balance) {
this.number = number;
this.name = name;
this.openDate = openDate;
this.balance = balance;
}

public void withdraw(double amount) {
if(this.balance > amount){
this.balance -= amount;
}
}

public void deposit(double amount) {
this.balance += amount;
}

public void transferTo(Account other, double money){
withdraw(money);
other.deposit(money);
}

@Override
public String toString() {
return "Account{" +
"number=" + number +
", name='" + name + ''' +
", openDate='" + openDate + ''' +
", balance=" + balance +
'}';
}
}

1. Account and CheckingAccount classes

CheckingAccount (5 points)

• This is a child class of Account and implements the transferTo() method as follows.

• public int transferTo(Account account, double amount): the first argument is the beneficiary account, and the second argument is the transfer amount. This method transfers the money from this current account to the beneficiary account, and returns

0: transfer is successful and a $2 transfer fee is not applied

1: transfer is successful and the $2 transfer fee is applied

-1: transfer is unsuccessful because balance is less than transfer amount and $2 transfer fee

-2: transfer is unsuccessful because balance is less than transfer amount

For our projects, the business rules are:

? If the balance of transferring account is $10000 or higher, a $2 transfer fee is waived.

? When making a transfer to a beneficiary account, if the balance of the transferring account is less than $10000, $2 transfer fee is deducted from the balance when the transfer occurs.

? If the balance is not sufficient to cover transfer amount (and the $2 transfer fee if the fee applies), the transfer should not occur.

• public CheckingAccount(int nu, String na, String op, double ba): is a constructor and uses passed-in arguments to initialize instance variables.

In the part 2, you re-use the Account and CheckingAccount classes from the prior part 1. So in addition to these two classes from the part 1 and the provided accounts.txt file, your part 2 project includes the following AccountUtility and TestPart2 classes.

accounts.txt

AccountUtility

-listNumbers: ArrayList <String>

-mapNumAccounts: LinkedHashMap <String, Account>

+AccountUtility()

+readFile(): void

+getListNumbers(): ArrayList<String>

+getMapNumAccounts(): LinkedHashMap <String, Account>

+saveFile(LinkedHashMap): void

1. AccountUtility class

This class has methods to read a provided file, accounts.txt, update the file, and provide data from the accounts.txt for other program(s).

1.1 Constructor (2 points)

public AccountUtility(): The constructor initializes the two instance variables.

1.2 Read the given text file (10 points)

public void readFile(): reads accounts.txt file and populates all account numbers into listNumbers ArrayList, and adds account numbers and corresponding CheckingAccount object as records to the LinkedHashMap object, mapNumAccounts.

1.3 Get account numbers (1 point)

public ArrayList getListNumbers(): returns the instance variable, listNumbers.

1.4 Get LinkedHashMap objects (1 point)

public LinkedHashMap getMapNumAccounts(): returns the instance variable, mapNumAccounts.

1.5 Save content to the text file (6 points)

public void saveFile(LinkedHashMap map): the pass-in parameter is a LinkedHashMap object in which the keys are account numbers and values are CheckingAccount objects. This method writes content from the passed-in LinkedHashMap object to accounts.txt, overwrites the file’s entire old content.

2. TestPart2 class (10 points)

• This class has the main() method.

• You create an AccountUtility object in this class, and use the object to test readFile() and getListNumbers(), and use System.out.println() to clearly display all account numbers retrieved from the accounts.txt file.

• Using the above AccountUtility object to call getMapNumAccounts() method and assign the returned value to a LinkedHashMap object.

• You must modify balances of two different accounts from the LinkedHashMap object by calling deposit(), withdraw(), and transferTo() methods with hard-code values.

• You call the saveFile(LinkedHashMap) method to overwrite the accounts.txt file. The pass-in parameter is the above updated LinkedHashMap object.

Notes:

1) When I test your project, my accounts.txt will follow the same format as yours but have different content.

2) AccountUtility should be the only class in the project that defines methods to read from and write to the accounts.txt file.

Explanation / Answer

//-------------------------------------------------------------------

//

// Class Accounts2 contains the driver of a program that modifies

// the class hierarchy in the original Accounts program.

//

// Methods:

//

// public static void main (String[] args)

//

//-------------------------------------------------------------------

// Creates and uses various bank accounts.

class Accounts2 {

//===========================================================

// Creates several types of bank accounts and performs

// various operations on them.

//===========================================================

public static void main (String[] args) {

Savings_Account savings =

new Savings_Account (4321, 5028.45, 0.02);

Bonus_Saver_Account big_savings =

new Bonus_Saver_Account (6543, 1475.85, 0.02);

Checking_Account checking =

new Checking_Account (9876, 269.93, savings);

savings.deposit (148.04);

big_savings.deposit (41.52);

savings.withdrawal (725.55);

big_savings.withdrawal (120.38);

checking.withdrawal (320.18);

} // method main

} // class Accounts2

//-------------------------------------------------------------------

//

// Class Bank_Account represents a generic bank account with the

// ability to make deposits and withdrawals.

//

// Constructors:

//

// public Bank_Account (int account_num, double initial_balance)

//

// Methods:

//

// public void deposit (double amount)

// public boolean withdrawal (double amount)

//

//-------------------------------------------------------------------

class Bank_Account {

protected int account;

protected double balance;

//===========================================================

// Sets up a bank account the specified account number and

// initial balance.

//===========================================================

public Bank_Account (int account_num, double initial_balance) {

account = account_num;

balance = initial_balance;

} // constructor Bank_Account

//===========================================================

// Adds the specified amount to the account balance.

//===========================================================

public void deposit (double amount) {

balance += amount;

System.out.println ("Deposit into account " + account);

System.out.println ("Amount: " + amount);

System.out.println ("New balance: " + balance);

System.out.println ();

} // method deposit

//===========================================================

// Withdraws the specified amount from the account,

// printing a message if the balance is not high enough.

//===========================================================

public boolean withdrawal (double amount) {

boolean result = false;

System.out.println ("Withdrawl from account " + account);

System.out.println ("Amount: " + amount);

if (amount > balance)

System.out.println ("Insufficient funds.");

else {

balance -= amount;

System.out.println ("New balance: " + balance);

result = true;

}

System.out.println();

return result;

} // method withdrawal

} // class Bank_Account

//-------------------------------------------------------------------

//

// Class Checking_Account represents an account with overdraft

// protection.

//

// Constructors:

//

// public Checking_Account (int account_num, double initial_balance,

// Savings_Account protection)

//

// Methods:

//

// public boolean withdrawal (double amount)

//

//-------------------------------------------------------------------

class Checking_Account extends Bank_Account {

private Savings_Account overdraft;

//===========================================================

// Sets up a checking account using the specified values.

//===========================================================

public Checking_Account (int account_num, double initial_balance,

Savings_Account protection) {

super (account_num, initial_balance);

overdraft = protection;

} // constructor Checking_Account

//===========================================================

// Withdraws the specified amount from the checking

// account, using the overdraft savings account if needed.

// Overrides the withdrawal method in Bank_Account.

//===========================================================

public boolean withdrawal (double amount) {

boolean result = false;

if ( ! super.withdrawal (amount) ) {

System.out.println ("Using overdraft...");

if ( ! overdraft.withdrawal (amount-balance) )

System.out.println ("Overdraft source insufficient.");

else {

balance = 0;

System.out.println ("New balance on account " +

account + ": " + balance);

result = true;

}

}

System.out.println ();

return result;

} // method withdrawal

} // class Checking_Account

//-------------------------------------------------------------------

//

// Class Savings_Account represents a single savings account with

// the ability to make deposits and withdrawals. The account earns

// interest.

//

// Constructors:

//

// public Savings_Account (int account_num, double initial_balance,

// double interest_rate)

//

// Methods:

//

// public void add_interest ()

//

//-------------------------------------------------------------------

class Savings_Account extends Bank_Account {

protected double rate;

//===========================================================

// Sets up a savings account using the specified values.

//===========================================================

public Savings_Account (int account_num, double initial_balance,

double interest_rate) {

super (account_num, initial_balance);

rate = interest_rate;

} // constructor Savings_Account

//===========================================================

// Adds interest to the account balance.

//===========================================================

public void add_interest () {

balance += balance * rate;

System.out.println ("Interest added to account: " + account);

System.out.println ("New balance: " + balance);

System.out.println();

} // method add_interest

} // class Savings_Account

//-------------------------------------------------------------------

//

// Class Bonus_Saver_Account represents a special type of savings

// account that earns extra interest, but has a penalty for

// withdrawals.

//

// Constructors:

//

// public Bonus_Saver_Account (int account_num,

// double initial_balance, double interest_rate)

//

// Methods:

//

// public boolean withdrawal (double amount)

// public void add_interest ()

//

//-------------------------------------------------------------------

class Bonus_Saver_Account extends Savings_Account {

private final int PENALTY = 25;

private final double BONUS_RATE = 0.03;

//===========================================================

// Sets up a bonus account using the specified values.

//===========================================================

public Bonus_Saver_Account (int account_num,

double initial_balance, double interest_rate) {

super (account_num, initial_balance, interest_rate);

} // constructor Super_Saver_Account

//===========================================================

// Withdraws the specified amount, plus the penalty for

// withdrawing from a bonus account. Overrides the

// withdrawal method of the Bank_Account class, but uses

// it to perform the actual withdrawal operation.

//===========================================================

public boolean withdrawal (double amount) {

System.out.println ("Penalty incurred: " + PENALTY);

return super.withdrawal (amount+PENALTY);

} // method withdrawal

//===========================================================

// Adds interest to the balance of the account, including

// the bonus rate. Overrides the add_interest method of

// the Savings_Account class.

//===========================================================

public void add_interest () {

balance += balance * (rate + BONUS_RATE);

System.out.println ("Interest added to account: " + account);

System.out.println ("New balance: " + balance);

System.out.println();

} // method add_interest

} // class Bonus_Saver_Account

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote