Charging and Discharging Algorithms for Electric Vehicles in Smart Grid Environment