New Account Class

1 minute read

The code has been modified slightly and now uploaded to the site.

Question

Question

Question

<— Return Home

Main.java

public class Main{
    public static void main(String[] args) {
        Account account = new Account("George", 1122, 1000);
        account.getAnnualInterestRate(1.5);
        account.deposit(30);
        account.deposit(40);
        account.deposit(50);
        account.withdraw(5);
        account.withdraw(4);
        account.withdraw(2);
        System.out.println(account);
    }
}

<— Return Home

Account.java

import java.util.ArrayList;

public class Account{
    private int id;
    private double balance;
    private double annualInterestRate;
    private java.util.Date dateCreated;
    private String name;
    private ArrayList<Transaction> transactions = new ArrayList<Transaction>();
    private int counter = -1;

    public Account(){
        id = 0;
        balance = 0;
        annualInterestRate = 0;
    }

    public Account(int id, double balance){
        this.id = id;
        this.balance = balance;
    }

    public Account(String name, int id, double balance){
        this.name = name;
        this.id = id;
        this.balance = balance;
    }

    public int getId(){
        return id;
    }

    public double getBalance(){
        return balance;
    }

    public double getAnnualInterestRate(double value){
        annualInterestRate += value;
        return annualInterestRate;
    }

    public java.util.Date getDateCreated(){
        return dateCreated;
    }

    public double getMonthlyInterestRate(){
        return annualInterestRate / 12;
    }

    public double getMonthlyInterest(){
        return balance * (annualInterestRate / 12);
    }

    public Transaction withdraw(double value){
        balance -= value;
        transactions.add(new Transaction('W', value, balance, "Thank you for withdrawing!"));
        counter++;
        return transactions.get(counter);
    }

    public Transaction deposit(double value){
        balance += value;
        transactions.add(new Transaction('D', value, balance, "Thank you for depositing!"));
        counter++;
        return transactions.get(counter);
    }

    @Override
    public String toString(){
        return "Account Summary:\nAccount Holder: " + name + "\nAnnual Interest Rate: " + annualInterestRate + "%\nBalance: $" + balance + "\nTransactions:\n" + transactions;
    }
}

<— Return Home

Transaction.java

public class Transaction{
    private java.util.Date date;
    private char type;
    private double amount;
    private double balance;
    private String description;

    public Transaction(char type, double amount, double balance, String description){
        this.type = type;
        this.amount = amount;
        this.balance = balance;
        this.description = description;
    }

    public java.util.Date getDate(){
        return date;
    }

    public char getType(){
        return type;
    }

    public double getAmount(){
        return amount;
    }

    public double getBalance(){
        return balance;
    }

    public String getDescription(){
        return description;
    }

    

    @Override
    public String toString(){
        return "\nType: " + type + " Amount: $" + amount + " Balance: $" + balance + " Description: " + description;
    }
}

<— Return Home