Stratified dust grains in the interstellar medium – I. An accurate computational method for calculating their optical properties