I'd like to convert a program that I have into OOP with the following class:
class Person:
name: None
sent: None
recieved: None
totalNum = None
# Getters and setters are assumed
def __init__(self, name, sent, recieve):
self.name = name
self.sent = sent
self.recieve = receive
def totalMovements(self):
return self.sent+self.receive
I have a file with a number of lines, and I'm only interested in the very first and second elements of each row.
The file structure is:
'BOB' 'SAMANTHA'
'JOHN' 'TOM'
'MAT' 'JEN'
'SIMON' 'BOB'
'TOM' 'CHARLIE'
'BOB' 'CHARLIE'
The frequency in name appearance for each column, defines the number of the amount of sent (1st column) and recieves (2nd column) by each person.
So as an example, the final output should be:
output:
NAME SENT RECIEVE TOTALNUM
BOB 2 1 3
TOM 1 1 2
Since only BOB and TOM are on both columns.
So far I've tried to create each object inside the same for loop when reading the file and storing each object person in a list but I can't get it to work when trying to check if the object is already in the list.
I know I could create the object just by:
person_List = []
with open(fileName) as file:
for line in file:
words = line.split(';')
sent = words[0]
recieve = words[1]
if sent not in person_List:
person_List.append(Person(sent, 1, 0)
else:
# would retrieve the person existing in the list and setSent(1)
But something I'm doing wrong because is not working. I'm wondering if I could manage all the chekings of an object Person already existing in the list and adding the number of sent's and recieve's to it.
Any hints?