**11.8 ( New Account class ) An Account class was specified in Programming Exerc
ID: 3790845 • Letter: #
Question
**11.8 (New Account class) An Account class was specified in Programming
Exercise 9.7. Design a new Account class as follows:
Add a new data field name of the String type to store the name of the
customer.
Add a new constructor that constructs an account with the specified name, id,
and balance.
Add a new data field named transactions whose type is ArrayList
that stores the transaction for the accounts. Each transaction is an instance
of the Transaction class. The Transaction class is defined as shown in
use this Account file I used with Exercise 9.7
//Account.java
//Account represents the object of Account class
import java.util.Date;
public class Account {
/**
* @param args
*/
private int id=0;
private double balance=0;
private double annualIntrestRate=0;
private Date dateCreated;
public Account() {
super();
}
public Account(int id, double balance) {
super();
this.id = id;
this.balance = balance;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualIntrestRate() {
return annualIntrestRate;
}
public void setAnnualIntrestRate(double annualIntrestRate) {
this.annualIntrestRate = annualIntrestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public double getMonthlyInterestRate()
{
return (this.getAnnualIntrestRate()/12);
}
public double getMonthlyInterest()
{
return (getBalance() *getMonthlyInterestRate()/100);
}
public double withDraw(double balance)
{
this.setBalance(this.getBalance()-balance);
return this.getBalance();
}
public double diposite(double balance)
{
this.setBalance(this.getBalance()+balance);
return this.getBalance();
}
public double totalBalance()
{
balance =balance + getMonthlyInterest();
return balance;
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Account {
/**
* @param args
*/
private int id = 0;
private double balance = 0;
private double annualIntrestRate = 0;
private Date dateCreated;
private String customerName;
// why i make it transaction is static ,because to track every record
private static List<Transaction> transactionsList = null;
public List<Transaction> getTransactionsList() {
if (transactionsList == null) {
transactionsList = new ArrayList<Transaction>();
return transactionsList;
}
return transactionsList;
}
public Account(int id, double balance, String customerName) {
this.id = id;
this.balance = balance;
this.customerName = customerName;
}
@Override
public String toString() {
return "Account [id=" + id + ", balance=" + balance
+ ", annualIntrestRate=" + annualIntrestRate
+ ", customerName=" + customerName + "]";
}
public Account() {
id = 0;
balance = 0;
annualIntrestRate = 0;
dateCreated = null;
customerName = null;
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualIntrestRate() {
return annualIntrestRate;
}
public void setAnnualIntrestRate(double annualIntrestRate) {
this.annualIntrestRate = annualIntrestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public double getMonthlyInterestRate() {
return (this.getAnnualIntrestRate() / 12);
}
public double getMonthlyInterest() {
return (getBalance() * getMonthlyInterestRate() / 100);
}
public double withDraw(double balance) {
double transactionAmount = balance;
this.setBalance(this.getBalance() - balance);
Date transactiondate = new Date();
char transactionType = 'w';
double remainingbalance = this.getBalance();
String description = "i withdraw the money $" + transactionAmount;
Transaction transaction = new Transaction(transactiondate,
transactionType, transactionAmount, remainingbalance,
description);
List<Transaction> list = getTransactionsList();
list.add(transaction);
return this.getBalance();
}
public double deposite(double balance) {
double transactionAmount = balance;
this.setBalance(this.getBalance() + balance);
Date transactiondate = new Date();
char transactionType = 'd';
double remainingbalance = this.getBalance();
String description = "i deposited the money with $" + transactionAmount;
Transaction transaction = new Transaction(transactiondate,
transactionType, transactionAmount, remainingbalance,
description);
List<Transaction> list = getTransactionsList();
list.add(transaction);
return this.getBalance();
}
public double totalBalance() {
balance = balance + getMonthlyInterest();
return balance;
}
}
import java.util.Date;
public class Transaction {
protected Date transactionDate;
protected char type;
protected double amount;
protected double balance;
protected String description;
public Transaction(Date transactionDate, char type, double amount,
double balance, String description) {
this.transactionDate = transactionDate;
this.type = type;
this.amount = amount;
this.balance = balance;
this.description = description;
}
@Override
public String toString() {
return "Transaction [transactionDate=" + transactionDate + ", type="
+ type + ", amount=" + amount + ", balance=" + balance
+ ", description=" + description + "]";
}
}
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter the customer name");
String customerName = scanner.next();
Random random = new Random();
int id = random.nextInt(1000);
System.out.println("enter the balance in your account");
double balance = scanner.nextDouble();
System.out.println("enter the annualinterest rate");
double annualIntrestRate = scanner.nextDouble();
Account account = new Account(id, balance, customerName);
account.setAnnualIntrestRate(annualIntrestRate);
System.out.println("enter the money to deposit");
double depositedAmount = scanner.nextDouble();
double amount = account.deposite(depositedAmount);
System.out.println("enter the money withdraw");
double withdrawAmount = scanner.nextDouble();
double amount1 = account.withDraw(withdrawAmount);
System.out.println("the account information is");
System.out.println(account.toString());
System.out.println("the all transacions are");
for (Transaction transaction : account.getTransactionsList()) {
System.out.println(transaction.toString());
}
}
}
output
enter the customer name
george
enter the balance in your account
2500
enter the annualinterest rate
2
enter the money to deposit
2500
enter the money withdraw
350
the account information is
Account [id=119, balance=4650.0, annualIntrestRate=2.0, customerName=george]
the all transacions are
Transaction [transactionDate=Sun Feb 12 13:59:25 IST 2017, type=d, amount=2500.0, balance=5000.0, description=i deposited the money with $2500.0]
Transaction [transactionDate=Sun Feb 12 13:59:37 IST 2017, type=w, amount=350.0, balance=4650.0, description=i withdraw the money $350.0]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.