The goal of this assignment is to give you experience writing classes and using
ID: 3839004 • Letter: T
Question
The goal of this assignment is to give you experience writing classes and using objects based on the class you created.
Requirements:
You need to create a BankAccount class with methods for operating on bank accounts.
Your class must have the following elements with the exact names as described below, so I can test it when grading the assignment.
BankAccount Class Requirements:
The name of the class should be BankAccount.
The BankAccount class should store a person’s name, account number, pin, and balance.
These data stores should be private fields (class variables) and only accessed using getter and setter methods.
Create Getter and Setter methods to access the data (instance variables).
getName() used to return the value stored in the name field.
setName(value) used to set the value of the name field.
getAccountNumber() used to return the value stored in the accountNumber field.
setAccountNumber(value) used to set the value of the accountNumber field.
getPIN() used to return the value stored in the pin field.
setPIN(value) used to set the value of the pin field.
getBalance() used to return the value stored in the balance field.
setBalance(value) used to set the value of the balance field.
You need to create constructors for the class.
One constructor should take an int parameter for the bank account number and an int for the PIN number to set the values for those fields when constructing a new object.
You need to create a default constructor that takes no parameters and gives each field an initial startup value when constructing a new object.
Example:
BankAccount myAccount = new BankAccount(5263234552100, 1234);
BankAccount anotherAccount = new BankAccount(); //default constructor
You need to create methods for working with bank accounts.
withdrawal(amount) used to decrease an account’s balance by an amount.
deposit(amount) used to increase an account’s balance by an amount.
transferFunds (amount, toBankAccount) used to transfer funds from one bank account to another bank account.
This is the trickiest method because it accepts another BankAccount object as a parameter to the method. The object that the method is being called on is one object, which can be reference using the this keyword, and the toBankAccount object passed into the method is another object that will have funds transferred to it.
Create a toString() method in the BankAccount class that returns a string representation of a BankAccount object. It’s common to convert an object with data to a string for data communication over a network, or data storage for storing in a text file or the database.
Suggested string format:
{id:12345, name:some name, pin:1234, balance:$123.56}
Make sure your BankAccount class methods do some level of checking for invalid values. For example, the setBalance(value) method shouldn’t set the balance to a negative value. Think about this when creating all your methods. There are a few methods that may not work properly when the user passes invalid data. Hence, the reason a number of the methods need some value checking.
Test Program Requirements:
Create a simple BankAccountTest class that creates several BankAccount objects, stores values in these objects, test to make sure the methods operate properly on these values.
Test you methods thoroughly to ensure the methods are working properly.
HELP BIG TIME!!
Explanation / Answer
Bank Account .java :
package sample;
import java.util.ArrayList;
public class BankAccount {
private String name;
private int accno;
private int pin;
private double balance;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAccno() {
return accno;
}
public void setAccno(int accno) {
this.accno = accno;
}
public int getPin() {
return pin;
}
public void setPin(int pin) {
this.pin = pin;
}
public BankAccount(int accno, int pin) {
super();
this.name ="";
this.accno = accno;
this.pin = pin;
this.balance =0.0;
}
public BankAccount(){}
@Override
public String toString() {
return "BankAccount {name:" + name + ", accno:" + accno + ", pin:" + pin + ", balance:$" + balance + "}";
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
if(balance>=0.0)
this.balance = balance;
else
System.out.println(" Balance cannot be negative");
}
public void withdrawal(double amount){
this.setBalance(this.getBalance()-amount);
}
public void deposit(double amount){
this.setBalance(this.getBalance()+amount);
}
public void transferFunds(double amount,int toBankAccount,ArrayList<BankAccount> list){
for(int i=0;i<list.size();i++)
{
if(list.get(i).getAccno()==toBankAccount){
list.get(i).setBalance(list.get(i).getBalance()+amount);
this.setBalance(this.getBalance()-amount);
break;
}
}
}
}
Bank Account Tester .java :
package sample;
import java.util.ArrayList;
public class BankAccountTester {
public static void main(String args[]){
ArrayList<BankAccount> acc_list=new ArrayList<BankAccount>();
BankAccount myAccount1 = new BankAccount(123456,1234);
BankAccount myAccount2 = new BankAccount();
acc_list.add(myAccount1);
acc_list.add(myAccount2);
myAccount1.setBalance(1000.00);
myAccount1.setName("SAN");
myAccount2.setAccno(234567);
myAccount2.setBalance(2000.00);
myAccount2.setName("MAN");
myAccount2.setPin(2345);
System.out.println(myAccount1.toString());
System.out.println(myAccount2.toString());
myAccount1.setBalance(2500.00);
myAccount2.setBalance(2600.00);
myAccount2.transferFunds(50.00,123456,acc_list);
System.out.println(myAccount1.getBalance());
System.out.println(myAccount2.getBalance());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.