Can someone comment in an explanation for each line of code and what it is doing
ID: 3721673 • Letter: C
Question
Can someone comment in an explanation for each line of code and what it is doing?
import java.io.BufferedReader;//Reads text from a character-input stream
import java.io.BufferedWriter;//Writes text to a character-output stream
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Transaction {
private String transId;
private String accountId;
private double amount;
private String description;
private Date date;
private String date_string;
// Parameterized constructor, Constructor is used to initialize values of
// instance variables
public Transaction(String transId, String accountId, double amount, String description, String date_string) {
this.transId = transId;
this.accountId = accountId;
this.amount = amount;
this.description = description;
this.date_string = date_string;
}
// Save Bank Details
public void saveTransaction(String fileName) {
try {
FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(this.getTransId());
bw.write("," + this.getAccountId());
bw.write("," + this.getAmount());
bw.write("," + this.description);
bw.write("," + this.getDate_string());
bw.write(" ");
bw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void getTransactionByDay(String fileName, Date date, Map<String, BankAccount> bankAccounts) {
Map<String, Double> balances = new HashMap<String, Double>();
try {
File file = new File(fileName);
if (file.exists()) {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String details[] = line.split(",");
if (details[0].equals("Transaction ID")) {
continue;
}
DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Date date2 = format.parse(details[4]);
if(date2.compareTo(date) == 0){
if (balances.containsKey((String) details[1])) {
double balance = balances.get((String) details[1]);
double amount = Double.parseDouble(details[2]);
balance += amount;
balances.put(details[1], balance);
} else {
double amount = Double.parseDouble(details[2]);
balances.put(details[1], amount);
}
}
}
br.close();
} else {
System.out.println("Transaction file do not exist");
}
for (String name : bankAccounts.keySet()) {
String key = name.toString();
if(!balances.containsKey(key)){
balances.put(key, 0.0);
}
}
System.out.println(" ----Balances as on Date"+date.toString()+"----");
for (String name : balances.keySet()) {
String key = name.toString();
String value = balances.get(name).toString();
System.out.println(key + " " + value);
}
System.out.println(" ");
} catch (Exception e) {
e.printStackTrace();
}
}
public String toString() {
return (transId + "/" + accountId + "/" + amount + "/" + description + "/" + date);
}
public String displayTransaction() {
String output = "";
output = output + date_string + " ";
output = output + transId + " ";
output = output + amount + " ";
output = output + accountId + " ";
output = output + description + " ";
return output;
}
public String getTransId() {
return transId;
}
public void setTransId(String transId) {
this.transId = transId;
}
public String getAccountId() {
return accountId;
}
public String getDate_string() {
return date_string;
}
public void setDate_string(String date_string) {
this.date_string = date_string;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}// end of transaction class
Explanation / Answer
Hi, If need any other help then comment me freely. Thanks.