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

Test shouldbe reverse #36

Closed
wants to merge 2 commits into from
Closed
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
62 changes: 49 additions & 13 deletions src/main/java/com/seg83/childbank/dao/DepositAccountBillsDao.java
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@

import java.util.List;


@Repository
@Slf4j
public class DepositAccountBillsDao extends AbstractArrayDao {
@@ -17,9 +18,17 @@ public class DepositAccountBillsDao extends AbstractArrayDao {
@Autowired
public DepositAccountBillsDao(DepositAccountDao depositAccountDao) {
this.depositAccountDao = depositAccountDao;
// 修正 ElementCount 初始化
this.getElementCount();
}

@Override
void getElementCount() {
log.info("Request deposit account count");
this.ElementCount = this.load().size();
log.debug("Get deposit account count {}", this.ElementCount);
}

@Override
JSONArray load() {
log.info("Request deposit account data in JSON format");
@@ -28,13 +37,6 @@ JSONArray load() {
return depositAccountBills;
}

@Override
void getElementCount() {
log.info("Request deposit account count");
this.ElementCount = this.load().size();
log.debug("Get deposit account count {}", this.ElementCount);
}

@Override
DepositAccountBills getElementById(long depositAccountBillId) {
log.info("Request depositAccountBill with id {}", depositAccountBillId);
@@ -63,6 +65,10 @@ void setAttribute(String attrname, Object value, long depositAccountBillId) {
log.info("Set depositAccountBillExpireDate to {}", value);
this.setDepositAccountBillExpireDate(depositAccountBillId, (String) value);
}
case "depositAccountBillEffectiveDate" -> {
log.info("Set depositAccountBillEffectiveDate to {} ", value);
this.setDepositAccountBillEffectiveDate(depositAccountBillId, (String) value);
}
default -> throw new RuntimeException("Invalid attribute name");
}
}
@@ -85,6 +91,12 @@ private void setDepositAccountBillExpireDate(long depositAccountBillId, String v
updateAccountBill(bill);
}

private void setDepositAccountBillEffectiveDate(long depositAccountBillId, String value) {
DepositAccountBills bill = this.getElementById(depositAccountBillId);
bill.setDepositAccountBillEffectiveDate(value);
updateAccountBill(bill);
}

private void updateAccountBill(DepositAccountBills bill) {
List<DepositAccountBills> depositAccountBills = this.load().toJavaList(DepositAccountBills.class);
for (int i = 0; i < depositAccountBills.size(); i++) {
@@ -97,18 +109,28 @@ private void updateAccountBill(DepositAccountBills bill) {
this.depositAccountDao.setAttribute("depositAccountBills", depositAccountBills);
}

public void createDepositAccountBill(double amount, double rate, String expireDate) {
log.info("Create DepositAccountBill with date amount {}, rate {}, expireDate {}", amount, rate, expireDate);
DepositAccountBills newDepositAccountBills = new DepositAccountBills(this.ElementCount + 1, amount, rate, expireDate);
this.ElementCount++;
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);
log.debug("this.ElementCount = {}", this.ElementCount);
log.debug("DepositAccountBill created {}", newDepositAccountBills);

List<DepositAccountBills> depositAccountBills = this.load().toJavaList(DepositAccountBills.class);
depositAccountBills.add(newDepositAccountBills);
this.ElementCount = depositAccountBills.size(); // 更新 ElementCount
log.debug("Set DepositAccountBill Array {}", depositAccountBills);
this.depositAccountDao.setAttribute("depositAccountBills", depositAccountBills);
}

public void deleteDepositAccountBill(long depositAccountBillId) {
log.info("Delete DepositAccountBill with id {}", depositAccountBillId);
List<DepositAccountBills> depositAccountBills = this.load().toJavaList(DepositAccountBills.class);
depositAccountBills.removeIf(bill -> bill.getDepositAccountBillId() == depositAccountBillId);
log.debug("Set DepositAccountBill Array after deletion {}", depositAccountBills);
this.depositAccountDao.setAttribute("depositAccountBills", depositAccountBills);
this.ElementCount = depositAccountBills.size(); // 更新 ElementCount
}

@Override
public Object getAttribute(String attrname, long depositAccountBillId) {
if (depositAccountBillId < 0 || depositAccountBillId > this.ElementCount) {
@@ -117,6 +139,7 @@ public Object getAttribute(String attrname, long depositAccountBillId) {
return switch (attrname) {
case "depositAccountBillAmount" -> this.getDepositAccountBillAmount(depositAccountBillId);
case "depositAccountBillRate" -> this.getDepositAccountBillRate(depositAccountBillId);
case "depositAccountBillEffectiveDate" -> this.getDepositAccountBillEffectiveDate(depositAccountBillId);
case "depositAccountBillExpireDate" -> this.getDepositAccountBillExpireDate(depositAccountBillId);
default -> throw new RuntimeException("Invalid attribute name");
};
@@ -132,14 +155,27 @@ private Object getDepositAccountBillRate(long depositAccountBillId) {
return bill.getDepositAccountBillRate();
}

private Object getDepositAccountBillEffectiveDate(long depositAccountBillId) {
DepositAccountBills bills = this.getElementById(depositAccountBillId);
return bills.getDepositAccountBillEffectiveDate();
}

private Object getDepositAccountBillExpireDate(long depositAccountBillId) {
DepositAccountBills bill = this.getElementById(depositAccountBillId);
return bill.getDepositAccountBillExpireDate();
}

@Override
List<Object> getAllAttributes() {
public List<Object> getAllAttributes() {
List<DepositAccountBills> depositAccountBills = this.load().toJavaList(DepositAccountBills.class);
return List.copyOf(depositAccountBills);
}
}

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;
}
}
Original file line number Diff line number Diff line change
@@ -16,6 +16,9 @@ public class DepositAccountBills {
@JSONField(name = "depositAccountBillRate")
private double depositAccountBillRate;

@JSONField(name = "depositAccountBillEffectiveDate")
private String depositAccountBillEffectiveDate;

@JSONField(name = "depositAccountBillExpireDate")
private String depositAccountBillExpireDate;
}
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ public Double checkCurrentAccountBalance() {
*
* @param amount The amount to deposit.
*/
public void depositCurrentAccount(int amount) {
public void depositCurrentAccount(double amount) {
double currentAmount = (Double) currentAccountDao.getAttribute("currentAccountAmount");
double newAmount = currentAmount + amount;
currentAccountDao.setAttribute("currentAccountAmount", newAmount);
@@ -87,4 +87,3 @@ public String toUiContent() {
return "$" + String.format("%.2f", balance);
}
}

54 changes: 48 additions & 6 deletions src/main/java/com/seg83/childbank/service/DepositService.java
Original file line number Diff line number Diff line change
@@ -3,42 +3,84 @@
import com.seg83.childbank.dao.DepositAccountDao;
import com.seg83.childbank.dao.DepositAccountBillsDao;
import com.seg83.childbank.entity.DepositAccountBills;
import com.seg83.childbank.utils.StringDateConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DepositService {
private final DepositAccountDao depositAccountDao;
private final CurrentService currentService;
private final DepositAccountBillsDao depositAccountBillsDao;
private final HistoryService historyService;
private final StringDateConvert convert;

@Autowired
public DepositService(DepositAccountDao depositAccountDao, DepositAccountBillsDao depositAccountBillsDao) {
public DepositService(DepositAccountDao depositAccountDao, DepositAccountBillsDao depositAccountBillsDao, HistoryService historyService, CurrentService currentService, StringDateConvert convert) {
this.depositAccountDao = depositAccountDao;
this.currentService = currentService;
this.depositAccountBillsDao = depositAccountBillsDao;
this.historyService = historyService;
this.convert = convert;

}

public Object[][] generateDepositList() {
log.info("Generating Deposit List...");
List<DepositAccountBills> depositAccountBillsList = (List<DepositAccountBills>) depositAccountDao.getAttribute("depositAccount");

// For swing JTable
Object[][] data = new Object[depositAccountBillsList.size()][4];
Object[][] data = new Object[depositAccountBillsList.size()][5];

for (int i = 0; i < depositAccountBillsList.size(); i++) {
data[i][0] = i + 1;
data[i][1] = depositAccountBillsDao.getAttribute("depositAccountBillAmount", i + 1);
data[i][2] = depositAccountBillsDao.getAttribute("depositAccountBillRate", i + 1);
data[i][3] = depositAccountBillsDao.getAttribute("depositAccountBillExpireDate", i + 1);
data[i][3] = depositAccountBillsDao.getAttribute("depositAccountBillEffectiveDate", i + 1);
data[i][4] = depositAccountBillsDao.getAttribute("depositAccountBillExpireDate", i + 1);
}
return data;
}

public void createDepositAccountBill(double amount, double rate, String expireDate) {
log.info("Creating depositAccountBill @ {} for {} {}", amount, rate, expireDate);
depositAccountBillsDao.createDepositAccountBill(amount, rate, expireDate);
public void createDepositAccountBill(double amount, double rate, String effectiveDate, String expireDate) {
log.info("Creating depositAccountBill @ {} for {} (from {} to{})", amount, rate, effectiveDate, expireDate);
depositAccountBillsDao.createDepositAccountBill(amount, rate, effectiveDate, expireDate);
}

public void depositFixAccount(double amount, double rate, String effectiveDate, String expireDate){
currentService.withdrawCurrentAccount((int)amount);
createDepositAccountBill(amount, rate, effectiveDate, expireDate);
log.info("Deposit fix {}", amount);
historyService.createOperationHistory(amount, "Fix deposit");
}

public void processMaturedDeposits() {
log.info("Processing matured deposits...");
List<DepositAccountBills> depositAccountBillsList = (List<DepositAccountBills>) depositAccountDao.getAttribute("depositAccount");
LocalDate today = LocalDate.now();

for (DepositAccountBills bill : depositAccountBillsList) {
LocalDate expireDate = LocalDate.parse(bill.getDepositAccountBillExpireDate(), DateTimeFormatter.ISO_DATE);
if (!expireDate.isAfter(today)) {
double amount = bill.getDepositAccountBillAmount();
double rate = bill.getDepositAccountBillRate();
long days = convert.calculateDaysBetween(bill.getDepositAccountBillEffectiveDate(), bill.getDepositAccountBillExpireDate());
double interest = amount * rate * days / 365;
double totalAmount = amount + interest;

currentService.depositCurrentAccount(totalAmount);
historyService.createOperationHistory(totalAmount, "Matured fixed deposit");
depositAccountBillsDao.deleteDepositAccountBill(bill.getDepositAccountBillId());
log.info("Processed matured deposit with amount: {}, interest: {}, total: {}", amount, interest, totalAmount);
}
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/seg83/childbank/utils/StringDateConvert.java
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
@@ -41,4 +43,17 @@ public String DateToString(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
return dateFormat.format(date);
}

/**
* Calculates the number of days between two dates represented as strings.
*
* @param startDate The start date string in the format "yyyy-MM-dd".
* @param endDate The end date string in the format "yyyy-MM-dd".
* @return The number of days between the start date and end date.
*/
public long calculateDaysBetween(String startDate, String endDate) {
LocalDate start = LocalDate.parse(startDate, DateTimeFormatter.ISO_DATE);
LocalDate end = LocalDate.parse(endDate, DateTimeFormatter.ISO_DATE);
return java.time.temporal.ChronoUnit.DAYS.between(start, end);
}
}
5 changes: 3 additions & 2 deletions src/main/resources/data_template.json5
Original file line number Diff line number Diff line change
@@ -16,8 +16,9 @@
"depositAccountBillId": 1,
"depositAccountBillAmount": 100.0,
"depositAccountBillRate": 0.5,
"depositAccountBillExpireDate": "2020-01-01",
},
"depositAccountBillEffectiveDate": "2019-01-01",
"depositAccountBillExpireDate": "2025-01-01",
}
],
},
"currentAccount": {
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.seg83.childbank.TestService;

import com.seg83.childbank.dao.DepositAccountBillsDao;
import com.seg83.childbank.service.CurrentService;
import com.seg83.childbank.service.DepositService;
import com.seg83.childbank.service.HistoryService;
import com.seg83.childbank.utils.StringDateConvert;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@@ -22,12 +27,26 @@ class TestDepositService {
private DepositService depositService;
@Autowired
private DepositAccountBillsDao depositAccountBillsDao;
@Autowired
private CurrentService currentService;
@Autowired
private HistoryService historyService;
@Autowired
private StringDateConvert stringDateConvert;

@BeforeAll
static void setup() {
System.setProperty("java.awt.headless", "false");
}

@BeforeEach
void setUp() {
// 删除所有现有的存款账单
depositAccountBillsDao.deleteAllDepositAccountBills();
// 创建初始测试数据
depositService.createDepositAccountBill(100.0, 0.5, "2019-01-01", "2025-01-01");
}

@AfterEach
void restoreTestJson() {
try {
@@ -40,6 +59,9 @@ void restoreTestJson() {

@Test
void generateDepositList() {
depositService.createDepositAccountBill(200, 0.1, "2023-08-03", "2024-05-01");
depositService.createDepositAccountBill(200, 0.1, "2023-08-03", "2024-05-01");
depositService.createDepositAccountBill(200, 0.1, "2023-08-03", "2024-05-01");
Object[][] depositList = depositService.generateDepositList();
assertNotNull(depositList, "Deposit list should not be null");
assertTrue(depositList.length > 0, "Deposit list should not be empty");
@@ -50,7 +72,7 @@ void generateDepositList() {
void createDepositAccountBill() {
long initialElementCount = depositAccountBillsDao.ElementCount;

depositService.createDepositAccountBill(100, 0.1, "2024-08-03");
depositService.createDepositAccountBill(100, 0.1, "2023-08-03", "2024-08-03");

long newElementCount = depositAccountBillsDao.ElementCount;
assertEquals(initialElementCount + 1, newElementCount, "Element count should have incremented by 1");
@@ -63,8 +85,60 @@ void createDepositAccountBill() {
assertNotNull(rate, "Deposit account bill rate should not be null");
assertEquals(0.1, rate, "Deposit account bill rate should be 0.1");

Object effectiveDate = depositAccountBillsDao.getAttribute("depositAccountBillEffectiveDate", newElementCount);
assertNotNull(effectiveDate, "Deposit account bill effective date should not be null");
assertEquals("2023-08-03", effectiveDate, "Deposit account bill effective date should be '2023-08-03'");

Object expireDate = depositAccountBillsDao.getAttribute("depositAccountBillExpireDate", newElementCount);
assertNotNull(expireDate, "Deposit account bill expire date should not be null");
assertEquals("2024-08-03", expireDate, "Deposit account bill expire date should be '2024-08-03'");
}

@Test
void depositFixAccount() {
double initialBalance = currentService.checkCurrentAccountBalance();

depositService.depositFixAccount(100, 0.1, "2023-08-03", "2024-08-03");

double newBalance = currentService.checkCurrentAccountBalance();
assertEquals(initialBalance - 100, newBalance, "Current account balance should be decreased by 100");

long newElementCount = depositAccountBillsDao.ElementCount;
assertEquals(2, newElementCount);

Object amount = depositAccountBillsDao.getAttribute("depositAccountBillAmount", newElementCount);
assertNotNull(amount, "Deposit account bill amount should not be null");
assertEquals(100.0, amount, "Deposit account bill amount should be 100.0");
}

@Test
void processMaturedDeposits() {
double initialBalance = currentService.checkCurrentAccountBalance();

// 创建一个已到期的定期存款账单
depositService.createDepositAccountBill(200, 0.1, "2023-08-03", "2024-05-01");
assertEquals(initialBalance, currentService.checkCurrentAccountBalance());

depositService.processMaturedDeposits();

double newBalance = currentService.checkCurrentAccountBalance();
double expectedInterest = 200 * 0.1 * 272 / 365; // Assuming the deposit has been active for 273 days
double expectedBalance = initialBalance + 200 + expectedInterest;
assertEquals(expectedBalance, newBalance, 0.01, "Current account balance should include principal and interest");

List<Object> bills = depositAccountBillsDao.getAllAttributes();
assertFalse(bills.isEmpty(), "There should be no deposit account bills after processing matured deposits");
}

@Test
void testCalculateDaysBetween() {
long days = stringDateConvert.calculateDaysBetween("2023-08-03", "2024-05-01");
assertEquals(272, days, "Days between 2023-08-03 and 2024-05-01 should be 272");

days = stringDateConvert.calculateDaysBetween("2023-01-01", "2023-12-31");
assertEquals(364, days, "Days between 2023-01-01 and 2023-12-31 should be 364"); // 2023 is not a leap year

days = stringDateConvert.calculateDaysBetween("2020-01-01", "2020-12-31");
assertEquals(365, days, "Days between 2020-01-01 and 2020-12-31 should be 365"); // 2020 is a leap year
}
}