Develop a class BankAccount with six methods: a. init - constructor, is able to
ID: 3661716 • Letter: D
Question
Develop a class BankAccount with six methods:
a. init - constructor, is able to construct a bank account with a given balance, or, if no balance is specified the balance defaults to 0. (see runs below)
b. repr - converts BankAccount to a str for display, see runs below.
c. set which takes a numeric amount as a parameter and sets the account balance to that parameter
d. withdraw - which takes a numeric amount as a parameter and withdraws the amount specified by the parameter from the balance on the account
e. deposit - which takes a numeric amount as a parameter and deposits the amount specified by the parameter from the balance on the account
f. balance - which takes no parameters and returns the current balance on the account
# constructor/repr
>>> b = BankAccount(100)
>>> b
BankAccount(100)
>>> b = BankAccount()
>>> b
BankAccount(0)
>>> [b] # check that str is returned, not printed
[BankAccount(0)]
# methods
>>> b.deposit(50.25)
>>> b
BankAccount(50.25)
>>> b.withdraw(17.50)
>>> b
BankAccount(32.75)
>>> b.balance()
32.75
>>> b.balance()==32.75 # check balance is returned, not printed
True
Explanation / Answer
package com.bank;
import java.text.NumberFormat;
import com.bank.BankAccount.Customer;
public class BankAccount
{
private double balance;
public Customer cust;
/**
* constructor
* * pre: none
* * * post: An account cree * customer data initia: */
public BankAccount(double bal, String fName, String lName, String str, String city, String st, String zip)
{
balance = bal;
cust = new Customer(fName, lName, str, city, st, zip);
}
/**
* Returns the current balance
* pre: none
* post: The account balance has been returned
*/
public double getBalance()
{
return (balance);
}
/**
* A deposit is made to the account
* pre: none
* post: The balance has been increased by the amount of the deposit.
*/
public void deposit(double amt)
{
balance += amt;
}
/**
* A withdrawal is made from the account if there is enough money
* pre: none
* post: The balance has been decreased by the amount withdrawn.
*/
public void withdrawal(double amount)
{
if (amount <= balance)
balance -= amount;
else
System.out.println("Not enough money in account.");
}
/**
* Returns a String that represents the Account object.
* pre: none
* post: A string representing the Account object has been returned.
*/
public String toString()
{
String accountString;
NumberFormat money = NumberFormat.getCurrencyInstance();
accountString = cust.toString();
accountString += "Current balance is " + money.format (balance);
return (accountString);
}
// The Customer class is implemented below
class Customer
{
private String firstName, lastName, street, city,state, zip;
/**
* constructor
* pre: none
* post: A Customer object has been created.
* Customer data has been initialized with parameters
*/
public Customer(String fName, String lName, String str,
String c, String s, String z)
{
firstName = fName;
lastName = lName;
street = str;
city = c;
state = s;
zip = z;
}
@Override
public String toString() {
return "Customer [firstName=" + firstName + ", lastName=" + lastName
+ ", street=" + street + ", city=" + city + ", state=" + state
+ ", zip=" + zip + "]";
}
}
}
package com.bank;
import java.util.Scanner; import java.text.NumberFormat;
public class BankTest
{
public static void main(String[] args)
{
BankAccount bankAccount=new BankAccount(120, "om", "prakash", "dady street", "veinece", "spain", "243786");
Scanner input = new Scanner(System.in);
double data;
NumberFormat money = NumberFormat.getCurrencyInstance();
System.out.println(bankAccount);
System.out.print ( "Enter deposit amount: ");
data = input.nextDouble();
bankAccount.deposit(data);
System.out.println ( "Balance is: " + money.format(bankAccount.getBalance()));
System.out.print ("Enter withdrawal amount: ");
data = input.nextDouble();
bankAccount.withdrawal (data);
System.out.println ( "Balance is: " + money. format(bankAccount.getBalance()));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.