Files
OOS/src/main/java/bank/Transaction.java
2024-12-01 01:19:55 +01:00

115 lines
2.9 KiB
Java

package bank;
import bank.exceptions.TransactionAttributeException;
import com.google.gson.*;
public abstract class Transaction implements CalculateBill{
/**
* protected vars, which must be available throughout the whole package
*/
protected String date;
protected double amount;
protected String description;
/**
* Sets the amount of the payment.
*
* @param newAmount The new amount for the payment.
*/
public void setAmount(double newAmount) throws TransactionAttributeException {
this.amount = newAmount;
}
/**
* Gets the amount of the payment.
*
* @return The amount of the payment.
*/
public double getAmount() {
return amount;
}
/**
* Sets the description of the payment.
*
* @param newDescription The new description for the payment.
*/
public void setDescription(String newDescription) {
this.description = newDescription;
}
/**
* Gets the description of the payment.
*
* @return The description of the payment.
*/
public String getDescription() {
return description;
}
/**
* Sets the date of the payment.
*
* @param newDate The new date for the payment.
*/
public void setDate(String newDate) {
this.date = newDate;
}
/**
* Gets the date of the payment.
*
* @return The date of the payment.
*/
public String getDate() {
return date;
}
/**
* Constructor for creating a transaction with date, amount, and description
*
* @param newDate The date of the transfer.
* @param newAmount The amount of the transfer.
* @param newDescription The description of the transfer.
*/
public Transaction(String newDate, double newAmount, String newDescription) throws TransactionAttributeException {
this.setAmount(newAmount);
this.setDescription(newDescription);
this.setDate(newDate);
}
/**
* Prints the transaction details to the console.
* Amount is printed in the extended class, because of
* different conditions to be checked
*/
@Override
public String toString() {
return ("\n Date: " + this.getDate() +
"\nDescription: " + this.getDescription() +
"\n Amount: " + this.calculate()
);
}
/**
* Compares obj with current transaction
*
* @param obj The object to compare with
* @return Result of the comparison
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof Transaction) {
Transaction other = (Transaction) obj;
return (
this.getDate().equals(other.getDate()) &&
this.getAmount() == other.getAmount() &&
this.getDescription().equals(other.getDescription())
);
}
return false;
}
}