Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jc new #44

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 117 additions & 19 deletions src/main/java/com/seg83/childbank/dao/DepositAccountBillsDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,61 @@
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* DepositAccountBillsDao is a data access object (DAO) for managing deposit account bills.
*/

@Repository
@Slf4j
public class DepositAccountBillsDao extends AbstractArrayDao {
/**
* The DepositAccountDao instance for interacting with deposit account data.
*/
private final DepositAccountDao depositAccountDao;
/**
* The number of elements in the deposit account bills array.
*/
public long ElementCount;

/**
* Constructs a new DepositAccountBillsDao instance.
*
* @param depositAccountDao the DepositAccountDao instance for interacting with deposit account data
*/
@Autowired
public DepositAccountBillsDao(DepositAccountDao depositAccountDao) {
this.depositAccountDao = depositAccountDao;
// 修正 ElementCount 初始化
this.getElementCount();
}

/**
* Retrieves the number of elements in the deposit account bills array.
*/
@Override
void getElementCount() {
log.info("Request deposit account count");
this.ElementCount = this.load().size();
log.debug("Get deposit account count {}", this.ElementCount);
}

/**
* Loads the deposit account bills data in JSON format.
*
* @return a JSON array containing the deposit account bills data
*/
@Override
JSONArray load() {
log.info("Request deposit account data in JSON format");
JSONArray depositAccountBills = depositAccountDao.load().getJSONArray("depositAccountBills");
log.debug("Get deposit account data {}", depositAccountBills);
return depositAccountBills;
}

/**
* Retrieves a deposit account bill by its ID.
*
* @param depositAccountBillId the ID of the deposit account bill to retrieve
* @return the deposit account bill with the specified ID
*/
@Override
DepositAccountBills getElementById(long depositAccountBillId) {
log.info("Request depositAccountBill with id {}", depositAccountBillId);
Expand All @@ -49,7 +74,13 @@ DepositAccountBills getElementById(long depositAccountBillId) {
}
throw new RuntimeException("Invalid Id");
}

/**
* Sets the attribute of a deposit account bill with the specified name and value.
*
* @param attrname the name of the attribute to set
* @param value the value to set the attribute to
* @param depositAccountBillId the ID of the deposit account bill to modify
*/
@Override
void setAttribute(String attrname, Object value, long depositAccountBillId) {
switch (attrname) {
Expand All @@ -72,31 +103,55 @@ void setAttribute(String attrname, Object value, long depositAccountBillId) {
default -> throw new RuntimeException("Invalid attribute name");
}
}

/**
* Sets the deposit account bill amount for the deposit account bill with the specified ID.
*
* @param depositAccountBillId the ID of the deposit account bill to modify
* @param value the new deposit account bill amount
*/
private void setDepositAccountBillAmount(long depositAccountBillId, double value) {
DepositAccountBills bill = this.getElementById(depositAccountBillId);
bill.setDepositAccountBillAmount(value);
updateAccountBill(bill);
}

/**
* Sets the deposit account bill rate for the deposit account bill with the specified ID.
*
* @param depositAccountBillId the ID of the deposit account bill to modify
* @param value the new deposit account bill rate
*/
private void setDepositAccountBillRate(long depositAccountBillId, double value) {
DepositAccountBills bill = this.getElementById(depositAccountBillId);
bill.setDepositAccountBillRate(value);
updateAccountBill(bill);
}

/**
* Sets the deposit account bill expire date for the deposit account bill with the specified ID.
*
* @param depositAccountBillId the ID of the deposit account bill to modify
* @param value the new deposit account bill expire date
*/
private void setDepositAccountBillExpireDate(long depositAccountBillId, String value) {
DepositAccountBills bill = this.getElementById(depositAccountBillId);
bill.setDepositAccountBillExpireDate(value);
updateAccountBill(bill);
}

/**
* Sets the deposit account bill effective date for the deposit account bill with the specified ID.
*
* @param depositAccountBillId the ID of the deposit account bill to modify
* @param value the new deposit account bill effective date
*/
private void setDepositAccountBillEffectiveDate(long depositAccountBillId, String value) {
DepositAccountBills bill = this.getElementById(depositAccountBillId);
bill.setDepositAccountBillEffectiveDate(value);
updateAccountBill(bill);
}

/**
* Updates the deposit account bill with the specified ID.
*
* @param bill the updated deposit account bill
*/
private void updateAccountBill(DepositAccountBills bill) {
List<DepositAccountBills> depositAccountBills = this.load().toJavaList(DepositAccountBills.class);
for (int i = 0; i < depositAccountBills.size(); i++) {
Expand All @@ -108,7 +163,14 @@ private void updateAccountBill(DepositAccountBills bill) {
log.debug("Update DepositAccountBill Array {}", depositAccountBills);
this.depositAccountDao.setAttribute("depositAccountBills", depositAccountBills);
}

/**
* Creates a new deposit account bill with the specified amount, rate, effective date, and expire date.
*
* @param amount the deposit account bill amount
* @param rate the deposit account bill rate
* @param effectiveDate the deposit account bill effective date
* @param expireDate the deposit account bill expire date
*/
public void createDepositAccountBill(double amount, double rate, String effectiveDate, String expireDate) {
log.info("Create DepositAccountBill with date amount {}, rate {}, effectiveDate {}, expireDate {}", amount, rate, effectiveDate, expireDate);
DepositAccountBills newDepositAccountBills = new DepositAccountBills(this.ElementCount + 1, amount, rate, effectiveDate, expireDate);
Expand All @@ -121,7 +183,11 @@ public void createDepositAccountBill(double amount, double rate, String effectiv
log.debug("Set DepositAccountBill Array {}", depositAccountBills);
this.depositAccountDao.setAttribute("depositAccountBills", depositAccountBills);
}

/**
* Deletes the deposit account bill with the specified ID.
*
* @param depositAccountBillId the ID of the deposit account bill to delete
*/
public void deleteDepositAccountBill(long depositAccountBillId) {
log.info("Delete DepositAccountBill with id {}", depositAccountBillId);
List<DepositAccountBills> depositAccountBills = this.load().toJavaList(DepositAccountBills.class);
Expand All @@ -130,7 +196,13 @@ public void deleteDepositAccountBill(long depositAccountBillId) {
this.depositAccountDao.setAttribute("depositAccountBills", depositAccountBills);
this.ElementCount = depositAccountBills.size(); // 更新 ElementCount
}

/**
* Retrieves the attribute of a deposit account bill with the specified name and ID.
*
* @param attrname the name of the attribute to retrieve
* @param depositAccountBillId the ID of the deposit account bill
* @return the value of the attribute
*/
@Override
public Object getAttribute(String attrname, long depositAccountBillId) {
if (depositAccountBillId < 0 || depositAccountBillId > this.ElementCount) {
Expand All @@ -144,38 +216,64 @@ public Object getAttribute(String attrname, long depositAccountBillId) {
default -> throw new RuntimeException("Invalid attribute name");
};
}

/**
* Retrieves the deposit account bill amount for the deposit account bill with the specified ID.
*
* @param depositAccountBillId the ID of the deposit account bill
* @return the deposit account bill amount
*/
private Object getDepositAccountBillAmount(long depositAccountBillId) {
DepositAccountBills bill = this.getElementById(depositAccountBillId);
return bill.getDepositAccountBillAmount();
}

/**
* Retrieves the deposit account bill rate for the deposit account bill with the specified ID.
*
* @param depositAccountBillId the ID of the deposit account bill
* @return the deposit account bill rate
*/
private Object getDepositAccountBillRate(long depositAccountBillId) {
DepositAccountBills bill = this.getElementById(depositAccountBillId);
return bill.getDepositAccountBillRate();
}

/**
* Retrieves the deposit account bill effective date for the deposit account bill with the specified ID.
*
* @param depositAccountBillId the ID of the deposit account bill
* @return the deposit account bill effective date
*/
private Object getDepositAccountBillEffectiveDate(long depositAccountBillId) {
DepositAccountBills bills = this.getElementById(depositAccountBillId);
return bills.getDepositAccountBillEffectiveDate();
}

/**
* Retrieves the deposit account bill expire date for the deposit account bill with the specified ID.
*
* @param depositAccountBillId the ID of the deposit account bill
* @return the deposit account bill expire date
*/
private Object getDepositAccountBillExpireDate(long depositAccountBillId) {
DepositAccountBills bill = this.getElementById(depositAccountBillId);
return bill.getDepositAccountBillExpireDate();
}

/**
* Retrieves a list of all deposit account bills.
*
* @return a list of all deposit account bills
*/
@Override
public List<Object> getAllAttributes() {
List<DepositAccountBills> depositAccountBills = this.load().toJavaList(DepositAccountBills.class);
return List.copyOf(depositAccountBills);
}

/**
* Deletes all deposit account bills.
*/
public void deleteAllDepositAccountBills() {
log.info("Deleting all deposit account bills");
List<DepositAccountBills> depositAccountBills = this.load().toJavaList(DepositAccountBills.class);
depositAccountBills.clear();
this.depositAccountDao.setAttribute("depositAccountBills", depositAccountBills);
this.ElementCount = 0;
}
}
}
46 changes: 45 additions & 1 deletion src/main/java/com/seg83/childbank/dao/DepositAccountDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,32 @@

import java.util.List;


/**
* Data Access Object (DAO) for managing deposit account data.
* Extends {@link AbstractDao} to handle deposit account data in JSON format.
*/
@Repository
@Slf4j
public class DepositAccountDao extends AbstractDao {

private final AccountDao accountDao;

/**
* Constructor for DepositAccountDao.
*
* @param accountDao AccountDao instance for managing account data.
*/
@Autowired
public DepositAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}

/**
* Load deposit account data in JSON format.
*
* @return JSONObject containing deposit account data.
*/
@Override
public JSONObject load() {
log.info("Request DepositAccountDao in JSON format");
Expand All @@ -27,6 +43,12 @@ public JSONObject load() {
return depositAccount;
}

/**
* Set an attribute of a deposit account.
*
* @param attrname Name of the attribute to set.
* @param value Value to set for the attribute.
*/
@Override
void setAttribute(String attrname, Object value) {
DepositAccount modifiedDepositAccount;
Expand All @@ -40,12 +62,24 @@ void setAttribute(String attrname, Object value) {
this.accountDao.setAttribute("depositAccount", modifiedDepositAccount);
}

/**
* Set the deposit account bills.
*
* @param value List of DepositAccountBills objects to set for the deposit account.
* @return DepositAccount object with the updated deposit account bills.
*/
private DepositAccount setDepositAccountBills(List<DepositAccountBills> value) {
DepositAccount depositAccount = this.load().toJavaObject(DepositAccount.class);
depositAccount.setDepositAccountBills(value);
return depositAccount;
}

/**
* Get an attribute of a deposit account.
*
* @param attrname Name of the attribute to get.
* @return Object containing the attribute value.
*/
@Override
public Object getAttribute(String attrname) {
return switch (attrname) {
Expand All @@ -54,16 +88,26 @@ public Object getAttribute(String attrname) {
};
}

/**
* Get the deposit account.
*
* @return DepositAccount object containing the deposit account data.
*/
private List<DepositAccountBills> getDepositAccount() {
log.info("Getting historyActions");
List<DepositAccountBills> depositAccountBills = this.load().getJSONArray("depositAccountBills").toJavaList(DepositAccountBills.class);
log.debug("Get depositAccountBills {}", depositAccountBills);
return depositAccountBills;
}

// TODO implement
/**
* Get all attributes of a deposit account.
*
* @return List of Objects containing all attributes of a deposit account.
*/
@Override
List<Object> getAllAttributes() {
// TODO implement
return null;
}
}
Loading
Loading