****NEED TRANSACTION CLASS*** PLEASE CHECK PROGRAM FOR ERROR! public class BankA
ID: 3834188 • Letter: #
Question
****NEED TRANSACTION CLASS***
PLEASE CHECK PROGRAM FOR ERROR!
public class BankAccount
{
private static double INT_RATE = 10.0;
//This is used to Storee Account Number
private int accountNo;
// This is used to Store Account Holder's Name
private String accountName;
// This is used to stores balance of the account
private double balance;
// Store OverDraft Limit
private double overdraftLimit;
// This is used to store the date that the account was opened on
private Date opened;
// Store a list of Transactions
private ArrayList transactions;
// ******* MEMBER METHODS *******
//constructor
public BankAccount()
{
accountNo = 0;
accountName = "Empty";
balance = 0.0;
overdraftLimit = 0;
opened = new Date();
transactions = new ArrayList();
transactions.add(new Trans(Trans.TRANSTYPE[4],0.0,0.0));
}
// This is called COPY CONSTRUCTOR
public BankAccount(BankAccount p)
{
accountNo = p.accNo;
accountName = p.accName;
balance = p.balance;
overdraftLimit = p.odLimit;
opened = new Date(p.opened);
transactions = new ArrayList(p.transactions);
}
/
// CONSTRUCTOR with 3 arguments
public BankAccount(int no1, String namehere, double bal1)
{
accountNo = no1;
accountName = namehere;
balance = bal1;
overdraftLimit = 0;
opened = new Date();
transactions = new ArrayList();
transactions.add(new Trans(Trans.TRANSTYPE[4],bal1,bal1));
}
// we have created a clone method here
public Object clone()
{
BankAccount z = new BankAccount();
z.accountNo = accountNo;
z.acccountName = accountName;
z.balance = balance;
z.overdraftLimit = overdraftLimit;
z.opened = new Date(opened);
z.transactions = new ArrayList(transactions);
return z;
}
// This is done to compare the onject passed with equals method
public boolean equals(Object other)
{
return accountNo == ((BankAccount)other).accountNo;
}
// toString() method - ALWAYS takes the form public String toString()
// Returns a string representation of the object
public String toString()
{
return accountNo+": "+accountName+": "+balance;
}
// These are the various accessor functions:
public void setAccountName(String name)
{
accountName = name;
}
public void setOverdraftLimit(double newLimit)
{
overdraftLimit = newLimit;
}
public double getBalance()
{
return balance;
}
public String getAccountName()
{
return accountName;
}
public int getAccountNo()
{
return accountNo;
}
// STATIC METHODS are declared once and used between all Objects
// created from this class file.
public static void setINT_RATE(double newIR)
{
INT_RATE = newIR;
}
public static double getINT_RATE()
{
return INT_RATE;
}
// UTILITY METHODS
// To deposit amount into bank->PRECONDITION amount must be positive
public void deposit(double amount)
{
balance = balance + amount;
transactions.add(new Trans(Trans.TRANSTYPE[0],amount,balance));
}
//Tow withdraw money from bank-> Withdraw with error checking
public boolean withdraw(double amount)
{
boolean valid = false;
if (checkWithdraw(amount))
{
balance = balance - amount;
valid = true;
transactions.add(new Trans(Trans.TRANSTYPE[1],-amount,balance));
}
return valid;
}
// Adds interest to the account
public void addInterest()
{
double interest = 0;
interest = balance*(INT_RATE/100);
deposit(interest);
}
// This method is used to check whether the user has sufficient funds
// to be able to withdraw the amount specified
private boolean checkWithdraw(double amount)
{
boolean valid = true;
if((balance-amount) < odLimit)
{
valid = false;
}
return valid;
}
// method to create a String representation of all the
// transactions on the account.
public String createfinalStatement()
{
// create a temporary string to hold the whole statement
String state = "";
// create a reference (pointer) to a Trans object
// called temp
Trans temp;
// loop through all transaction objects in our specific
// BankAccount object.
for(int i=0; i < transactions.size(); i++)
{
temp = (Trans)transactions.get(i);
state = state+" "+temp.toString();
}
return state;
}
// This is the main method for testing
public class BankAccountDriver{
public static void main (String [] args)
{
// This basically used create a new BankAccount object called s1
BankAccount s1 = new BankAccount(10, rohan", 500);
// This is used to checkwhether it was created correctly
System.out.println(s1);
// display a blank lines
System.out.println();
// This is done to perform some transactions on the account
s1.deposit(700);
s1.withdraw(202);
s1withdraw(200);
s1.addInterest();
s1.withdraw(100);
// This is to display a statement to the screen
System.out.println(sg.createfinalStatement());
}
}
Explanation / Answer
PROGRAM CODE:
BankAccountDriver.java
package bankAccount;
public class BankAccountDriver {
public static void main (String [] args)
{
// This basically used create a new BankAccount object called s1
BankAccount s1 = new BankAccount(10, "rohan", 500);
// This is used to checkwhether it was created correctly
System.out.println(s1);
// display a blank lines
System.out.println();
// This is done to perform some transactions on the account
s1.deposit(700);
s1.withdraw(202);
s1.withdraw(200);
s1.addInterest();
s1.withdraw(100);
// This is to display a statement to the screen
System.out.println(s1.createfinalStatement());
}
}
BankAccount.java
package bankAccount;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.SimpleFormatter;
import javax.swing.text.DateFormatter;
public class BankAccount {
private static double INT_RATE = 10.0;
//This is used to Storee Account Number
private int accountNo;
// This is used to Store Account Holder's Name
private String accountName;
// This is used to stores balance of the account
private double balance;
// Store OverDraft Limit
private double overdraftLimit;
// This is used to store the date that the account was opened on
private Date opened;
// Store a list of Transactions
private ArrayList<Trans> transactions;
// ******* MEMBER METHODS *******
//constructor
public BankAccount()
{
accountNo = 0;
accountName = "Empty";
balance = 0.0;
overdraftLimit = 0;
opened = new Date();
transactions = new ArrayList<>();
transactions.add(new Trans(Trans.TRANSTYPE[4],0.0,0.0));
}
// This is called COPY CONSTRUCTOR
public BankAccount(BankAccount p)
{
accountNo = p.accountNo;
accountName = p.accountName;
balance = p.balance;
overdraftLimit = p.overdraftLimit;
String inputString = opened.toString();
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
opened = dateFormat.parse(inputString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transactions = new ArrayList<>(p.transactions);
}
// CONSTRUCTOR with 3 arguments
public BankAccount(int no1, String namehere, double bal1)
{
accountNo = no1;
accountName = namehere;
balance = bal1;
overdraftLimit = 0;
opened = new Date();
transactions = new ArrayList<>();
transactions.add(new Trans(Trans.TRANSTYPE[4],bal1,bal1));
}
// we have created a clone method here
public Object clone()
{
BankAccount z = new BankAccount();
z.accountNo = accountNo;
z.accountName = accountName;
z.balance = balance;
z.overdraftLimit = overdraftLimit;
String inputString = opened.toString();
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
z.opened = dateFormat.parse(inputString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
z.transactions = new ArrayList<>(transactions);
return z;
}
// This is done to compare the onject passed with equals method
public boolean equals(Object other)
{
return accountNo == ((BankAccount)other).accountNo;
}
// toString() method - ALWAYS takes the form public String toString()
// Returns a string representation of the object
public String toString()
{
return accountNo+": "+accountName+": "+balance;
}
// These are the various accessor functions:
public void setAccountName(String name)
{
accountName = name;
}
public void setOverdraftLimit(double newLimit)
{
overdraftLimit = newLimit;
}
public double getBalance()
{
return balance;
}
public String getAccountName()
{
return accountName;
}
public int getAccountNo()
{
return accountNo;
}
// STATIC METHODS are declared once and used between all Objects
// created from this class file.
public static void setINT_RATE(double newIR)
{
INT_RATE = newIR;
}
public static double getINT_RATE()
{
return INT_RATE;
}
// UTILITY METHODS
// To deposit amount into bank->PRECONDITION amount must be positive
public void deposit(double amount)
{
balance = balance + amount;
transactions.add(new Trans(Trans.TRANSTYPE[0],amount,balance));
}
//Tow withdraw money from bank-> Withdraw with error checking
public boolean withdraw(double amount)
{
boolean valid = false;
if (checkWithdraw(amount))
{
balance = balance - amount;
valid = true;
transactions.add(new Trans(Trans.TRANSTYPE[1],-amount,balance));
}
return valid;
}
// Adds interest to the account
public void addInterest()
{
double interest = 0;
interest = balance*(INT_RATE/100);
deposit(interest);
}
// This method is used to check whether the user has sufficient funds
// to be able to withdraw the amount specified
private boolean checkWithdraw(double amount)
{
boolean valid = true;
if((balance-amount) < overdraftLimit)
{
valid = false;
}
return valid;
}
// method to create a String representation of all the
// transactions on the account.
public String createfinalStatement()
{
// create a temporary string to hold the whole statement
String state = "";
// create a reference (pointer) to a Trans object
// called temp
Trans temp;
// loop through all transaction objects in our specific
// BankAccount object.
for(int i=0; i < transactions.size(); i++)
{
temp = (Trans)transactions.get(i);
state = state+" "+temp.toString();
}
return state;
}
}
Trans.java
package bankAccount;
public class Trans {
public static String TRANSTYPE[] = {"Deposit", "Withdraw", "Add Interest", "Open", "New Account"};
String transType;
double transAmount;
double updatedBalance;
public Trans(String type, double amount, double bal) {
transType = type;
transAmount = amount;
updatedBalance = bal;
}
public String getTransType() {
return transType;
}
public void setTransType(String transType) {
this.transType = transType;
}
public double getTransAmount() {
return transAmount;
}
public void setTransAmount(double transAmount) {
this.transAmount = transAmount;
}
public double getUpdatedBalance() {
return updatedBalance;
}
public void setUpdatedBalance(double updatedBalance) {
this.updatedBalance = updatedBalance;
}
@Override
public String toString() {
return "[" + transType +"]: " + " Amount: " + transAmount + " Balance: " + updatedBalance;
}
}
OUTPUT:
10: rohan: 500.0
[New Account]: Amount: 500.0 Balance: 500.0
[Deposit]: Amount: 700.0 Balance: 1200.0
[Withdraw]: Amount: -202.0 Balance: 998.0
[Withdraw]: Amount: -200.0 Balance: 798.0
[Deposit]: Amount: 79.80000000000001 Balance: 877.8
[Withdraw]: Amount: -100.0 Balance: 777.8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.