must be in java: Create a class named BankAccountPlus Copy of type in the BankAc
ID: 3718965 • Letter: M
Question
must be in java:
Create a class named BankAccountPlus
Copy of type in the BankAccount class from Chapter 3
Add an instance variable named accountName; keep the balance instance variable.
Chance both constructors. On the default constructor (the one that does not have an argument), add a String argument to hold the name of the owner of the account. Call it “an” and use it to initialize accountName.
Add a method that will return the accountName.
Add a method called payFees. This method will charge a monthly fee. The method has no arguments and does not return anything. The method should call the withdraw method to deduct five dollars from the account if the account has a balance less than $2000 or call the withdraw method to deduct ten dollars if the balance is greater than 2,000 and less than 20,000. If the account has a balance greater than 20,000 then do not deduct a fee.
Create a BankAccountPlusTester (this should be the main)
Import Scanner
Create a Scanner object
Create a name and balance local variable.
Create 3 BankAccountPlus objects by:
Ask the user for a name and add it to the variable name.
Ask the user for an opening balance and add it to the variable balance.
Create the account with the information provided from the user.
Don’t forget you many need to add extra in.nextLine() like we did in a previous lab.
Create a fourth BankAccountPlus object that will have the name dummy and no balance.
Call payFees for each account.
Print the name and balance for each account. Be sure and use words to describe what each output means.
bank account code:
public class BankAccount
{
private double balance;
public BankAccount()
{
balance = 0;
}
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
public void deposit(double amount)
{
balance= balance + amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public double getBalance()
{
return balance;
}
}
Explanation / Answer
BankAccountPlus.java
public class BankAccountPlus
{
private String accountName; // instance variable named accountName
private double balance;
public BankAccountPlus()
{
balance = 0;
}
//Constructor
public BankAccountPlus(double initialBalance,String an)
{
balance = initialBalance;
this.accountName=an;
}
public void deposit(double amount)
{
balance= balance + amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public double getBalance()
{
return balance;
}
//Method that returns the account name
public String getAccountName() {
return accountName;
}
//method payFees
/***
* This method will charge a monthly fee.
* The method has no arguments and does not return anything.
* The method should call the withdraw method to deduct five dollars from the account
* if the account has a balance less than $2000 or call the withdraw method to deduct ten dollars if the balance is greater than 2,000 and less than 20,000.
* If the account has a balance greater than 20,000 then do not deduct a fee.
*/
public void payFees()
{
if(getBalance() <2000)
{
withdraw(5);
}
else if(getBalance() > 2000 && getBalance() < 20000)
{
withdraw(10);
}
else if(getBalance() > 20000)
{
//does not deduct any fees
}
}
//To print the account
@Override
public String toString() {
return "BankAccount [accountName=" + accountName + ", balance=" + balance + "]";
}
}
/**
*
*/
BankAccountPlusTester .java
import java.util.Scanner;
/**
* @author your name
*
*/
public class BankAccountPlusTester {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name;
double amount;
// create 3 BankAccountPlus object s
BankAccountPlus account1,account2, account3;
for(int i=1;i<=3;i++)
{
System.out.println("For Account # "+i);
System.out.println("Please enter your name : ");
name=in.nextLine();
System.out.println("Please enter Initial Balance : ");
amount=in.nextDouble();
//clear the Buffer
in.nextLine();
if(i==1)
{
//Create account and call pay fees
account1=new BankAccountPlus(amount, name);
account1.payFees();
System.out.println(account1);
}
if(i==2)
{
//Create account
account2=new BankAccountPlus(amount, name);
account2.payFees();
System.out.println(account2);
}
if(i==3)
{
//Create account
account3=new BankAccountPlus(amount, name);
account3.payFees();
System.out.println(account3);
}
}
}
}
Output:
For Account # 1
Please enter your name :
waseem'
Please enter Initial Balance :
5000
BankAccount [accountName=waseem', balance=4990.0]
For Account # 2
Please enter your name :
pappu pasha
Please enter Initial Balance :
1990
BankAccount [accountName=pappu pasha, balance=1985.0]
For Account # 3
Please enter your name :
Shoeb pasha
Please enter Initial Balance :
3500
BankAccount [accountName=Shoeb pasha, balance=3490.0]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.