A Flexible Account Class File Account.java contains a definition for a simple bank account class with methods to withdra

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

A Flexible Account Class File Account.java contains a definition for a simple bank account class with methods to withdra

Post by answerhappygod »

A Flexible Account Class
File Account.java contains a definition for asimple bank account class with methods to withdraw, deposit, getthe balance and account number, and return a String representation.Note that the constructor for this class creates a random accountnumber. Save this class to your directory and study it to see howit works. Then modify it as follows:
1. Overload the constructor as follows:
2. Overload the withdraw method with one thatalso takes a fee and deducts that fee from the account when thewithdraw amount is greater than 1000.
File TestAccount.java contains a simpleprogram that exercises these methods. Save it to your directory,study it to see what it does, and use it to test your modifiedAccount class.
//************************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdrawfrom,
// change the name on, and get a String representation
// of the account.
//************************************************************
public class Account
{
private double balance;
private String name;
private long acctNum;
//-------------------------------------------------
//Constructor -- initializes balance,owner, and account number
//-------------------------------------------------
public Account(double initBal, Stringowner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
}
//-------------------------------------------------
// Checks to see if balance issufficient for withdrawal.
// If so, decrements balance byamount; if not, prints message.
//-------------------------------------------------
public void withdraw(doubleamount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficientfunds");
}
//-------------------------------------------------
// Adds deposit amount to balance.
//-------------------------------------------------
public void deposit(double amount)
{
balance += amount;
}
//-------------------------------------------------
// Returns balance.
//-------------------------------------------------
public double getBalance()
{
return balance;
}
//-------------------------------------------------
// Returns a string containing thename, account number, and balance.
//-------------------------------------------------
public String toString()
{
return "Name:" + name +
"\nAccount Number: " + acctNum +
"\nBalance: " + balance;
}
}
//************************************************************
// TestAccount.java
//
// A simple driver to test the overloaded methods of
// the Account class.
//************************************************************
import java.util.Scanner;
public class TestAccount
{
public static void main(String[]args)
{
String name;
double balance;
long acctNum;
Account acct;
Scanner scan = newScanner(System.in);
System.out.println("Enter accountholder's first name");
name = scan.next();
acct = new Account(name);
System.out.println("Account for " +name + ":");
System.out.println(acct);
System.out.println("\nEnter initialbalance");
balance = scan.nextDouble();
acct = new Account(balance,name);
System.out.println("Account for " +name + ":");
System.out.println(acct);
System.out.println("\nEnter accountnumber");
acctNum = scan.nextLong();
acct = newAccount(balance,name,acctNum);
System.out.println("Account for " +name + ":");
System.out.println(acct);
System.out.print("\nDepositing 100into account, balance is now ");
acct.deposit(100);
System.out.println(acct.getBalance());
System.out.print("\nWithdrawing $25,balance is now ");
acct.withdraw(25);
System.out.println(acct.getBalance());
System.out.print("\nWithdrawing $25with $2 fee, balance is now ");
acct.withdraw(25,2);
System.out.println(acct.getBalance());
System.out.println("\nBye!");
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply