Object Oriented Programming Practice 2
Requirment
- Create a class in Java that models a transaction (a withdrawal or lodgement of money) on a back account. The Transaction class should have:
A date representing the date of the transaction (represented by a LocalDateTime object);
A type (W or D) to represent the type of transaction (withdraw or deposit);
An amount to represent the amount of the transaction;
A balance to represent the new balance after the transaction;
Add a constructor to construct a Transaction with the specified type, amount, balance and description;
Add a toString() method to return transaction details as a String.
- Create a class in Java that models a BankAccount. The class should have:
A private int data field named id;
A private double data field named balance;
A private data field called name of type String;
An ArrayList of type Transaction (ArrayList<Transaction>) which stores all the transactions for the account;
A private double data field named annualInterestRate that stores the current interest rate. All bank accounts will have the same interest rate.
A private LocalDateTime field named dataCreated that stores the date when the account was created;
A no-arg constructor that creates an account with the default values;
A constructor that creates an account with a specified id and balance;
Accessor and mutator methods for id, balance and annualInterestRate;
An accessor method for dateCreated;
A method getMonthlyInterestRate();
A method named withdraw that withdraws a specified amount from an account. This method should add a transaction to the ArrayList of transactions.
A method named deposit that deposits a specified amount in an account. This method should add a transaction to the ArrayList of transactions.
Solve
Transaction
import java.time.LocalDateTime;
public class Transaction{
LocalDateTime date;
String type;
double amount;
double balance;
Transaction()
{
}
Transaction(String type, double amount, double balance)
{
this.type = type;
this.amount = amount;
this.balance = balance;
date = LocalDateTime.now();
}
public String toString()
{
String info ="";
info+="The Type is : " + this.type + "\n";
info+="The Amount is : " + this.amount + "\n";
info+="The Balance is : " + this.balance + "\n";
info+="The time of operation is : " + this.date + "\n";
return info;
}
}
BankAccount
import java.time.*;
import java.util.*;
public class BankAccount {
private int id;
private double balance;
private String name;
ArrayList<Transaction> transactions;
private double annualInterestRate;
LocalDateTime dateCreated;
BankAccount() {
this.dateCreated = LocalDateTime.now();
this.id = 0;
this.balance = 0;
this.transactions = new ArrayList<Transaction>();
}
BankAccount(int id, double balance, String name) {
this.dateCreated = LocalDateTime.now();
this.id = id;
this.balance = balance;
this.name = name;
this.transactions = new ArrayList<Transaction>();
}
public int getId() {
return this.id;
}
public double getBalance() {
return this.balance;
}
public double getAnnualInterestRate() {
return this.annualInterestRate;
}
public LocalDateTime getDateCreated() {
return this.dateCreated;
}
public void setId(int id) {
this.id = id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterestRate() {
return getAnnualInterestRate()/12;
}
public void withdraw(double amount) {
balance -= amount;
Transaction t = new Transaction("W", amount, this.balance);
transactions.add(t);
}
public void deposit(double amount) {
balance += amount;
Transaction t = new Transaction("D", amount, this.balance);
transactions.add(t);
}
}