Can someone comment in an explanation for each line of code and what it is doing
ID: 3719470 • Letter: C
Question
Can someone comment in an explanation for each line of code and what it is doing? I'm confused on how this works.
import java.util.ArrayList;
public class Account
{
private String accountName;
private double balance;
private ArrayList<Transaction> history;
public Account()
{
accountName = "";
balance = 0;
history = new ArrayList<Transaction>();
}
public Account(String userAccountName, double b)
{
accountName = userAccountName;
balance = b;
history = new ArrayList<Transaction>();
}
//SETTERS
public void setAccountName(String userAccountName)
{
accountName = userAccountName;
}
public void setBalance(double b)
{
balance = b;
}
//getters
public String getAccountName()
{
return accountName;
}
public double getBalance()
{
return balance;
}
public ArrayList<Transaction> getHistory()
{
return history;
}
public void addTransaction(Transaction tran)
{
history.add(tran);
balance = balance + tran.getAmount();
}
public String toString()
{
return (accountName + "/" + balance + "/");
}
public String displayBalance()
{
String output = "";
output = (output + accountName + " ");
output = (output + "Balance: " + balance + " ");
return output;
}
public String displayAccount()
{
String output = "";
output = output + displayBalance();
output = output + " ";
output = output + "Date Category Amount Person ";
for (int i = 0; i < history.size(); i++)
{
Transaction tran = history.get(i);
output = output + tran.displayTransaction();
}
return output;
}
}//end of Account class
Explanation / Answer
I have added comments to your code. Please let me know in comments if you have trouble understanding any part of this.
//Class declaration of Account class
public class Account {
//Variable to hold name of the account
private String accountName;
//variable to hold balance in the account
private double balance;
//A list containing all the transactions happened in the account
private ArrayList<Transaction> history;
//Default Constructor
public Account() {
//Initializing variables to hold default values.
accountName = "";
balance = 0;
history = new ArrayList<Transaction>();
}
//Parametrized constructor with account name and balance as initial values
public Account(String userAccountName, double b) {
//setting initial values to the account variables
accountName = userAccountName;
balance = b;
history = new ArrayList<Transaction>();
}
// SETTERS
//method to set the accountName
public void setAccountName(String userAccountName) {
accountName = userAccountName;
}
//method to set the account balance
public void setBalance(double b) {
balance = b;
}
// getters
//method to get the accountName
public String getAccountName() {
return accountName;
}
//method to get the account balance
public double getBalance() {
return balance;
}
//method to get the list of transactions related to account
public ArrayList<Transaction> getHistory() {
return history;
}
//method to add a transaction to the list of historical transaction
public void addTransaction(Transaction tran) {
//adding transaction to variable history
history.add(tran);
//Adding balance from transaction to already existing balance
//thus updating the balance
balance = balance + tran.getAmount();
}
//method to return account details in String format
public String toString() {
return (accountName + "/" + balance + "/");
}
//Method to display the account balance
public String displayBalance() {
// creating a String with account name and Balance details
String output = "";
output = (output + accountName + " ");
output = (output + "Balance: " + balance + " ");
//returning the created String
return output;
}
//method to display the complete Account information
// this displays the list of transactions and prints them in tabular format
public String displayAccount() {
//CReating and output String to hold the account details
String output = "";
//adding account name and balance to output
output = output + displayBalance();
//moving to next line
output = output + " ";
//adding table headers of the transaction
output = output + "Date Category Amount Person ";
//loop to iterate through list of historical transactions
//at each iteration, we take one transaction and add its details
//to the output variable
for (int i = 0; i < history.size(); i++) {
//getting one transaction per iteration
Transaction tran = history.get(i);
//calling displayTransaction method of Transaction class
// and getting the details of the transaction
// adding the details to the output
output = output + tran.displayTransaction();
}
//complete account details is saved in output variable
//and this variable is returned
return output;
}
}// end of Account class