Calculating the interest on savings account

It was long time since I wrote here last time. I’m coming back today with interesting and not so simple problem of your savings account and how interest is calculated for the money you put in.

Assumption is that every month you should get monthly interest worked out on the money from savings account. With this task we want to be penny perfect. So we can’t loose any money during the calculation. Also for simplicity account can be open only for fixed amount of years. As for example I use one year as it’s simple and later we’ll see how to handle situation when money is stored for many years.

At first you can think you need list of numbers. But is it really what you have to calculate? List used every month for crediting your main account is just representation. IMO all what you need here are three numbers. Monthly interest, interest for first month and interest for last month. Forth “hidden” number is reminder after dividing annual interest into monthly one. Let’s take an example scenario.

We want to save 10000 with the annual interest rate of 10% for 1 year. We are opening our savings account as I’m writing this post at 27th Oct 2015.

First things first – total amount of money we get – easy: 10000 * 10% = 1000.

Next we can calculate our average monthly interest: 1000 / 12 = 83.33. And here we can see our hidden fourth number because 83.33 * 12 = 999.96. As we talking about money (and as it’s the great explanation why you shouldn’t use floats for money calculations) we have missing 0.04. But as we are aware of this we can fix this on the first or last interest payment. I’ll add it for the last one.

But first let’s calculate the first interest payment. There are 4 days till end of the October so again easy calculation 4 / 31 * 83.33 = 10.75. Again. Money. I’m loosing some as the real result is 10.75225806451613. But that’s what the last interest payment is for :)

I know that last one could be calculated by checking date and proportion of days from the beginning of the month. Sometimes it’s needed but as we have annual plan all we need to do is to subtract first interest payment from average monthly one: 83.33 – 10.75 = 72.58. And we are nearly done. All we need to add here is the reminder we saved from division of annual interest by months: 72.58 + 0.04 = 72.62.

That’s all. Intentionally I’m not writing code to give you a try on that. It’s very interesting problem and great subject for short kata in the evening.

Let me know if you like this kind of articles without the code as I have an idea about focusing blog posts more on everyday problems than fancy code snippets for copy/paste ;)

Update:

After all I’ve created sample Scala Worksheet with the object solving problem I described. Moved the remainder to first payment to show more money on first month ;) Here’s the Gist.

Stateless authentication in Symfony 2

Everyone these days want’s an API. Mostly for some SPAs or for external applications. Common issue here is a security layer where you can’t use standard form and cookies. Other thing about state saved in cookies is f.ex. clickjacking. James Ward wrote about this some time ago.

So we have some requirements:

  • Authentication based on token
  • Token is send as part of each request to make API stateless
  • I want to be able to use Symfony Security layer as I do in “normal” applications
  • Bonus: I want authenticate each device with different token (when someone is using app on PC and phone in the same time)

So I went to Symfony Cookbook and I’ve found solution with my requirement in the title. And it wasn’t what I needed. OK, it’s about keys/tokens and probably I could make it working but I have a lot of free time during holidays so I can do better. So I went one article further to write my my own authentication provider. And there is of course article about it too.

Lets start with UserProvider. What I want is find User entity with given token and device identifier. Simple schema looks like this:

User and Identities

To find user I query by username when authenticating user by token and device on the beginning of each request and once in a while by username and password to create new Identity.

Interface like this is everything I need and it’s consistent with Liskov’s substitution principle (L in SOLID).

When I can find user all I need is follow Cookbook and everything is working great. SecureApiBundle is available on GitHub so you can check how I’ve done this.

Update your composer.json file

enable it

and use secure-api in your security config:

I know it is possible to add paths for registration and session creation to config, but for now I don’t need this so I keep it simple :P Doctrine UserRepository is used here as User Provider with interface I mentioned earlier. Line 28 is marker that we are using our token solution for this firewall.

 

11 reasons why I’ve chosen Scala

As you probably know in the office I am a PHP developer. Last few month I was looking for second language which can give me fun and profit. It took time, but I think now I know that it is Scala.

A little history. Few moths ago I started to learn Ruby and Rails. It was fun, but I’ve started to feel lonely and afraid in code without interfaces, type hinting over interface/class and DI Container. It was also boring after some time – I was doing same thing typing same structures only words was different.

After that my thinking was shifted to Functional Programming. Quite hot topic on HackerNews and in community. After some research I tried Clojure and I knew Lisp syntax is not my thing ;) But after that I knew that FP is the choice.

So Scala it is.

  1. Scala can be functional as much as Object Oriented – so when learning new things I fees safe with good old objects
  2. It’s statically typed. I like this and I’m a bit sad I can’t do this in PHP ;)
  3. It takes a lot from Java so when doing Scala I learn a bit about another environment.
  4. It’s no yet another scripting language for web development …
  5. … but I can do Web
  6. I can finally get out of my thread and do something parallel ;)
  7. Play Framework
  8. Akka
  9. (Play) Non-blocking IO operations without JavaScript – IMHO real (and much better) alternative for NodeJS
  10. LinkedIn and Twitter are using Scala/Play on production
  11. Good community support and courses on Coursera 1, 2

Now I’m doing simple scripts, I’m watching presentations on YouTube and I’m working with “Programming with Scala” book. It’s fun and FP is really great to shift your thinking about programming to other level.

Multiple entities in one Symfony 2 form

Case: User belongs to Account entity and in registration form I want to create Account for him.

First I have simple registration form from FOSUserBundle. To extend it you have to create new form type which contains fields from other entity. In my example I have only name required to create new Account.
Continue reading “Multiple entities in one Symfony 2 form”

Truncate filter in Symfony 2

Fabien Potencier created very useful filter for truncating strings to given number of chars. It’s included in Symfony Standard Edition, but not enabled by default. To fix this place this in any of your service-config files. Continue reading “Truncate filter in Symfony 2”