Question: Hi. I need some help testing some functions written within a class. I'm pretty sure they all should work but I need to test them
Hi. I need some help testing some functions written within a class.
I'm pretty sure they all should work but I need to test them before moving on and could use help testing and editing the code.
Here is what I wrote :
class Country: def __init__(self,name,pop,area,continent): self._name = str(name) self._name = name self._pop = pop self._area = area self._continent = continent def setPop(self, pop): self._pop = int(pop) def setArea (self,area): self._area = int(area) def setContinent(self, continent): self._continent = str(continent) def getName (self): return self._name def getPopulation (self): return self._pop def getArea (self): return self._area def getContinent (self): return self._continent def __repr__(self): return self._name + " (pop: " + str(self._pop) + ", size: " + str(self._area) + ") in " + self._continent class CountryCatalouge: def __init__(self, data): self._data = open(data, "r") self._cDictionary = {} self._countryCat = {} self._lines = self._data.readlines()[1:] #skipping the first line with header for line in self._lines: line = line.split("|") line[2] = line[2].replace(",", "") line[3] = line[3].replace(",", "") self._cDictionary[line[0]]= line[1] self._countryCat[line[0]] = Country(line[0], int(line[2]), float(line[2].strip()), self._cDictionary[line[0]]) self._data.close() def __repr__ (self): s = '' for country in self._countryCat.values(): s += country.__repr__() + ' ' return s def setPopulationOfCountry (self, country, population): country = country.title() if country in self._countryCat.keys(): self._countryCat[country].setPopulation(population) return True else: return False def setAreaOfCountry (self, country, area): country = country.title() if country in self._countryCat.keys(): self._countryCat[country].setArea(area) return True else: return False def findCountry (self, country): if country.title() in self._countryCat.keys(): result = self._countryCat[country] return result else: return None def addCountry (self, country, population, area, continent): country = country.title() if country not in self._cDictionary.keys(): self.countryCat[country] = Country(country, int(population), int(area), continent) return True else: return False def printCountryCatalouge (self): for country in self._countryCat.keys(): print(self._countryCat[country]) def saveCountryCatalouge(self, fname): file_object = open (fname, "w") countryList = [] for country in self._countryCat.keys(): countryList.append(country) countryList = sorted(countryList) count = 0 for country in countryList: countryName = self._countryCat[country].getName() population = self._countryCat[country].getPopulation() area = self._countryCat[country].getArea() continent = self._countryCat[country].getContinent() file_object.write(countryName + "|" + str(population) + "|" + str(area) +"|" +" ") count = count + 1 file_object.close() return count data = CountryCatalouge("data.txt") print(data)
I have already tested the class Country and it seems to work fine.
I need help testing the functions within country catalouge
Here is what each of the main functions are supposed to do :
findCountry : when given a country, it checks to see if the country object is in country class. It returns the country object if it is contained in countryCat , if it is not it returns null object
addCountry : when given the name, population, area and continent, it adds the new country to the cateloge if it does not already exisit
It returns true if country was sucessfully addes and false if it was not.
printCountry : makes use of the repr method in Country class to print a list of all the countries and their information
saveCountryCatalouge : saves all the country information into a file. the countries should be sorted alphabetically
if successful it should return the value of the number of items written (counter) and if not successful it should return -1 (i am having trouble with this part)
The format of the printed file should be
Country|Continent|Population|Area
The file being used as input is data.txt
Country|Continent|Population|Area Brazil|South America|193,364,000|8,511,965 Canada|North America|34,207,000|9,976,140 China|Asia|1,339,190,000|9,596,960 Colombia|South America|48,654,392|1,090,909 Egypt|Africa|93,383,574|1,000,000 France|Europe|64,668,129|541,656 Indonesia|Asia|260,581,100|1,809,590 Italy|Europe|59,801,004|300,000 Japan|Asia|127,380,000|377,835 Mexico|North America|128,632,004|1,969,230 Nigeria|Africa|186,987,563|912,134 South Africa|Africa|54,978,907|1,222,222 South Korea|Asia|50,503,933|98,076 United States of America|North America|309,975,000|9,629,091
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
