The gunman who shot and killed three people at a Chicago hospital Monday afternoon fatally shot himself after being shot by police, authorities said.

The gunman – identified as a 32-year-old Juan Lopez – had pulled out a gun and killed Dr. Tamara O’Neal, 38, in the Mercy Hospital parking lot, police said.

Chicago police spokesman Anthony Guglielmi said Lopez and O’Neal had been in a relationship. O’Neal appeared to be the intended target of the attack because she had called off an engagement with the gunman in September, WSL-TV reported.

CHICAGO HOSPITAL SHOOTING THAT LEFT OFFICER, DOCTOR DEAD CAME AFTER FIGHT OVER RING, ‘BROKEN ENGAGEMENT’: REPORTS

Lopez then spotted police and ran into the hospital, according to reports. Once inside he fatally shot a pharmacy resident Dayna Less as she was exiting an elevator, Guglielmi said. Lopez then exchanged gunfire with police and killed Officer Samuel Jimenez.

Lopez was shot in the chest amid the gunfire, then killed himself with the Glock semi-automatic he was carrying, Guglielmi said. Lopez had used only one gun, and had reloaded to continue firing, Guglielmi said. Lopez reportedly fired his gun at least 30 times.

The medical examiner's office ruled all deaths a homicide, KKTV reported.

Lopez had reportedly been kicked out of the city’s fire department academy four years ago after threatening a female cadet, Chicago Fire Department spokesman Larry Merrit said. That same year a woman sought an order or protection against Lopez because he was incessantly texting her, according to reports.

Fox News' Travis Fedschun and The Associated Press contributed to this report.