from domain import Address class Service: def __init__(self,repo) -> None: self.repo=repo def add_address(self,id,name,number,x,y): if not id.isdigit(): raise ValueError("Id must be a positive integer") if len(name)<3: raise ValueError("Name must be at least 3 characters") if not number.isdigit() or int(number)>100: raise ValueError("Number must be a positive integer less or equal to 100") try: int(x) except: raise ValueError("X must be an integer") if int(x)>100 or int(x)<-100: raise ValueError("X must be an integer between -100 and 100") try: int(y) except: raise ValueError("Y must be an integer") if int(y)>100 or int(y)<-100: raise ValueError("Y must be an integer between -100 and 100") address=Address(int(id),name,int(number),int(x),int(y)) self.repo.add_address(address) def get_addresses(self): return self.repo.get_addresses_as_string() def get_distance(self,x,y,dist): """ :params: x - str y - str dist - str :return: to_return - list of tuples of Address and int The function verifies the input and calculates the distance between the given position and all adresses and return the adresses with the distance smaller than the one specify """ try: int(x) except: raise ValueError("X must be an integer") if int(x)>100 or int(x)<-100: raise ValueError("X must be an integer between -100 and 100") try: int(y) except: raise ValueError("Y must be an integer") if int(y)>100 or int(y)<-100: raise ValueError("Y must be an integer between -100 and 100") if not dist.isdigit(): raise ValueError("Distance must be a positive integer") data=self.repo.get_addresses() to_return=[] for address in data: distance=((int(x)-address.get_x())**2+(int(y)-address.get_y())**2)**(1/2) if distance<=int(dist): to_return.append((address,distance)) return to_return def get_coordinates(self): n=0 x=0 y=0 data=self.repo.get_addresses() for address in data: n+=1 x+=address.get_x() y+=address.get_y() if n==0: raise ValueError("There are no addresses") return (x/n,y/n)