Looking for someone to comment in an explanation to each line of code and what i
ID: 3719518 • Letter: L
Question
Looking for someone to comment in an explanation to each line of code and what it is doing. I don't understand it.
import java.io.FileWriter;
import java.io.IOException;
public class Transaction
{
private int day;
private int month;
private int year;
private String category;
private String person;
private double amount;
public Transaction()
{
day = 0;
month = 0;
year = 0;
category = "";
person = "";
amount = 0;
}
public Transaction(int d, int m, int y, String c, String p, double a)
{
day = d;
month = m;
year = y;
category = c;
person = p;
amount = a;
}
//SETTERS
public void setDay(int d) {
day = d;
}
public void setMonth(int m) {
month = m;
}
public void setYear(int y) {
year = y;
}
public void setCategory(String c) {
category = c;
}
public void setPerson(String p) {
person = p;
}
public void setAmount(double a) {
amount = a;
}
//GETTERS
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
public String getCategory() {
return category;
}
public String getPerson() {
return person;
}
public double getAmount() {
return amount;
}
public String toString() {
return (month + "/" + day + "/" + year + "/" + category + "/" + person + "/" + amount);
}
public String displayTransaction() {
String output = "";
output = output + month + "/" + day + "/" + year + " ";
output = output + category + " ";
output = output + amount + " ";
output = output + person + " ";
return output;
}
public void toFile(String accountHolder, String accountName, String file) {
try {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(accountHolder);
fileWriter.write(" ");
fileWriter.write(accountName);
fileWriter.write(" ");
fileWriter.write(day + " " + month + " " + year + " ");
fileWriter.write(category + " " + person + " " + amount);
fileWriter.write(" ");
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}//end of transaction class
Explanation / Answer
//Importing FileWrite and IOException class
import java.io.FileWriter;
import java.io.IOException;
//Class decleration with name Transaction
public class Transaction
{
//Instance variables day, mont etc
private int day;
private int month;
private int year;
private String category;
private String person;
private double amount;
//No argument constructor
public Transaction()
{
day = 0;
month = 0;
year = 0;
category = "";
person = "";
amount = 0;
}
//Parameterized constructor, Constructor is used to initiualise values of instance variables
public Transaction(int d, int m, int y, String c, String p, double a)
{
day = d;
month = m;
year = y;
category = c;
person = p;
amount = a;
}
//Different Setters
//SETTERS
//Setter for day
public void setDay(int d) {
day = d;
}
//Setter for month
public void setMonth(int m) {
month = m;
}
//Setter for year
public void setYear(int y) {
year = y;
}
//Setter for category
public void setCategory(String c) {
category = c;
}
//Setter for person
public void setPerson(String p) {
person = p;
}
?//Setter for amount
public void setAmount(double a) {
amount = a;
}
//Getters will return the values, setter is used to set values
//GETTERS
//getter for day
public int getDay() {
return day;
}
//getter for month
public int getMonth() {
return month;
}
//getter for year
public int getYear() {
return year;
}
//getter for category
public String getCategory() {
return category;
}
//getter for person
public String getPerson() {
return person;
}
//getter for amount
public double getAmount() {
return amount;
}
//toString method that will return values of instance variables all consolidated in string format
public String toString() {
return (month + "/" + day + "/" + year + "/" + category + "/" + person + "/" + amount);
}
//A method displayTransaction
public String displayTransaction() {
String output = "";
//month day and year concatenated
output = output + month + "/" + day + "/" + year + " ";
//category concatenated in output
output = output + category + " ";
//amount concatenated in output
output = output + amount + " ";
//person concatenated in output
output = output + person + " ";
return output;
}
//Write data to File
public void toFile(String accountHolder, String accountName, String file) {
try {
//Open fileWrite object to write to the file passed as argument
FileWriter fileWriter = new FileWriter(file);
//write accountHolder into file
fileWriter.write(accountHolder);
//Tab is added (3 space)
fileWriter.write(" ");
//write accountNumber into file
fileWriter.write(accountName);
fileWriter.write(" ");
//write day, month , year
fileWriter.write(day + " " + month + " " + year + " ");
fileWriter.write(category + " " + person + " " + amount);
fileWriter.write(" ");
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}//end of transaction class
===================================================
Added MY COMMENTS
Thanks, let me know if there is any concern. PLEASE UPVOTE if helpful
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.