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

145 lines
4.2 KiB
Java

package bank;
import bank.exceptions.TransactionAttributeException;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import java.lang.reflect.Type;
/**
* The Transfer class represents a financial transfer between two parties,
* including the date, amount, description, sender, and recipient.
*/
public class Transfer extends Transaction {
/**
* private variables
* initialized as empty Strings to avoid null-errors
*/
private String sender = "";
private String recipient = "";
/**
* Sets the amount by checking if condition is valid
* @param newAmount The new amount for the payment.
*/
@Override
public void setAmount(double newAmount) throws TransactionAttributeException{
if(newAmount >= 0)
this.amount = newAmount;
else
throw new TransactionAttributeException("Amount cannot be negative");
}
/**
* Sets the sender of the transfer.
*
* @param newSender The new sender for the transfer.
*/
public void setSender(String newSender) {
this.sender = newSender;
}
/**
* Gets the sender of the transfer.
*
* @return The sender of the transfer.
*/
public String getSender() {
return sender;
}
/**
* Sets the recipient of the transfer.
*
* @param newRecipient The new recipient for the transfer.
*/
public void setRecipient(String newRecipient) {
this.recipient = newRecipient;
}
/**
* Gets the recipient of the transfer.
*
* @return The recipient of the transfer.
*/
public String getRecipient() {
return recipient;
}
/**
* Constructor for creating a transfer with date, amount, and description
* based on abstract class
*
* @param newDate The date of the transfer.
* @param newAmount The amount of the transfer.
* @param newDescription The description of the transfer.
*/
public Transfer(String newDate, double newAmount, String newDescription) throws TransactionAttributeException {
super(newDate, newAmount, newDescription);
}
@Override
public double calculate() {
return this.getAmount();
}
/**
* Constructor for creating a transfer with date, amount, description,
* sender, and recipient based on abstract class
*
* @param newDate The date of the transfer.
* @param newAmount The amount of the transfer.
* @param newDescription The description of the transfer.
* @param newSender The sender of the transfer.
* @param newRecipient The recipient of the transfer.
*/
public Transfer(String newDate, double newAmount, String newDescription, String newSender, String newRecipient) throws TransactionAttributeException {
super(newDate, newAmount, newDescription);
this.setSender(newSender);
this.setRecipient(newRecipient);
}
/**
* Copy constructor to create a new Transfer object based on an existing one.
*
* @param t The Transfer object to copy from.
*/
public Transfer(Transfer t) throws TransactionAttributeException {
this(t.getDate(), t.getAmount(), t.getDescription(), t.getSender(), t.getRecipient());
}
/**
* Prints the transfer details to the console.
*/
@Override
public String toString() {
return ("\n------------Transfer-----------" +
super.toString()+
"\n Sender: " + this.getSender() +
"\n Recipient: " + this.getRecipient() +
"\n-------------------------------\n"
);
}
/**
* 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 Transfer) {
Transfer t = (Transfer) obj;
return(
super.equals(t) &&
this.getSender().equals(t.getSender()) &&
this.getRecipient().equals(t.getRecipient())
);
}
return false;
}
}