Reputation: 1
i have built a RESTful server with a database with Spring/hibernate/mySQL.
i have a table of a bank savings account that has 3 savings balance columns.
for example:
account_id | a_savings | b_savings | c_savings
1 | 100 | 200 | 300
i want that each day (or month) , each savings account will add 0.01%(or other amount) to its value automatically according to the server/current time.
how can i do it?
Upvotes: 0
Views: 31
Reputation: 1
Solved it with scheduling tasks:
@Component
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Autowired
private SavingsAccountDao savingsAccountDao;
/* each day at 8 am */
@Scheduled(cron = "0 0 8 * * *")
public void increaseSavingsAccount() {
log.info("Savings Accounts Updated", dateFormat.format(new Date()));
/* get all savings accounts and increase balance according to the interest */
List<SavingsAccount> savingsAccountList = savingsAccountDao.findAll();
savingsAccountList.forEach((sa) -> {
/* 39% interest in 12 months */
sa.setASavingsBalance(sa.getASavingsBalance().multiply(new BigDecimal(1.0009)));
});
}
}
Upvotes: 0
Reputation: 36
Write a method that pauses for an amount of time than call a method that
value = value+(value*0.01)
then use SQL to update the value.For pause in java use TimeUnit.MINUTES.sleep(2);
of java.util.concurrent.TimeUnit
here 2
stands for 2 Minutes
. You can also use DAYS/HOURS TimeUnit.DAYS.sleep(1);
Upvotes: 1