if islam preaches forgiveness, why do we have a shariah legal system

yes you don’t necessarily have to forgive. but if you forgive its better for you. you should forgive if it will cause more good rather than punishment.